Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

295
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda