Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

296
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda