Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

298
Views
discord js getting args from user message and using them in variables shows up as undefined

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: enter image description here

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!