I'm using user input from messages starting with prefix to execute commands but get undefined when I try to use the values inside my code
let args;
client.on('message', async message => {
if(!message.content.startsWith(prefix) || message.author.bot) {
return;
}
args = message.content.slice(prefix.length).split(/ +/);
console.log("args: ", args); // logs out correctly
let x = args[1];
console.log("args[1]: ", x) // this will give undefined
const command = args.shift().toLowerCase();
if(command === 'generate') {
console.log("generate command used");
await replyWithInvite(message);
}
})
also using args array in a function like replyWithInvite() will result in all the args values showing up as undefined, how can I use these values?
EDIT:
server response to my message:

This is my whole code, I'm trying to generate a certain number of discord invites according to the second argument number
let args;
client.on('message', async message => {
if(!message.content.startsWith(prefix) || message.author.bot) {
return;
}
console.log("this did register");
args = message.content.slice(prefix.length).split(/ +/);
console.log("args: ", args); // here i get values
const command = args.shift().toLowerCase();
if(command === 'generate') {
console.log("generate command used");
await replyWithInvite(message);
}
})
async function replyWithInvite(message) {
let invite = [];
console.log("args1: ", await args[1]); // here I get undefined
count = 1;
message.reply("Here are your 1 time use invites, which expire in 10 minutes: ")
for(let i = 0; i < count; i++) {
invite.push(await message.channel.createInvite(
{
maxAge: 10 * 60 * 1000, // maximum time for the invite, in milliseconds
maxUses: 1 // maximum times it can be used
}
))
message.channel.send(invite ? `${invite[i]}` : "There has been an error during the creation of the invite.");
}
message.channel.send("That's it!")
}
I hardcoded the count value in the function so that I get a result, but I can't find a way to use args, I'm using Heroku as a host
This is because by the time you call the function, you already shifted the array. Since arrays are 0-indexed, you have to use args[0] instead!
async function replyWithInvite(message) {
//...
console.log("args1: ", args[0]) //should give value
//...
}
In case there are any questions about where it was shifted, here it was shifted
const command = args.shift().toLowerCase();