I am trying to make a /quote command where it saves the discord id of the person mentioned and the quote itself. I want to save it to the specific guild so that I can use it later for a command like /quote-list, but I am getting the error interaction.guild.push is not a function. My code is:
async execute(interaction) {
let inputuser = interaction.options.getUser('user');
let quote = interaction.options.getString('quote');
function QuoteObjectCheck(){
if (!interaction.guild.quotes){
var quoteObject = []
interaction.guild.push(quoteObject);
}
console.log(interaction.guild.quotes);
}
QuoteObjectCheck();
const newQuote = { userid: `${inputuser.id}`, quote: `${quote}`};
interaction.guild.quotes.push(newQuote)
console.log(interaction.guild.quotes);
interaction.followUp(`Succesfully saved new quote from ${inputuser}. Do /quote-list to get a random quote`)
},
I am new to discord.js and javascript in general btw and I would appreciate any help.
CodePudding user response:
So this isn't going to work at all. You're writing a value to the local interaction parameter from your handler function. That's not persistent. If you want to save a list of things you either need a database hooked up to your bot, store in a local file that you read/write to/from, or you need to store it for the runtime of the bot.
Also, you shouldn't just freely define functions inside other functions like you did.
For example, you'd write this instead:
async execute(interaction) {
let inputuser = interaction.command.options.getUser('user');
let quote = interaction.command.options.getString('quote');
const newQuote = { userid: `${inputuser.id}`, quote: `${quote}`};
//Your code to save it to a database or other location
//Console log the quotes from wherever you stored them
console.log()
interaction.followUp(`Successfully saved new quote from ${inputuser}. Do /quote-list to get a random quote`)
}
I can't really help you write the database part because it's up to you and what database you want to use and how you want to implement it. However, just remember that you can't randomly append properties to objects in JS and expect them to stick around. The JS lifecycle doesn't allow for that. I'm not sure which other languages you know if any, but take some time to read some basic JS tutorials so you know how the language fundamentally works before diving into the rest of your discord bot.
CodePudding user response:
You can't store the quotes in a guild. You need to save them in either a json file or a database. I recommend creating a database like MongoDB, follow the steps here: https://www.mongodb.com/basics/create-database https://www.mongodb.com/docs/atlas/tutorial/connect-to-your-cluster/