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

292
Views
discord js obtiene argumentos del mensaje del usuario y usarlos en variables se muestra como indefinido

Estoy usando la entrada del usuario de los mensajes que comienzan con el prefijo para ejecutar comandos, pero no estoy definido cuando trato de usar los valores dentro de mi código

 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); } })

también usar la matriz de argumentos en una función como replyWithInvite() dará como resultado que todos los valores de los argumentos se muestren como indefinidos, ¿cómo puedo usar estos valores?

EDITAR:

respuesta del servidor a mi mensaje: ingrese la descripción de la imagen aquí

Este es mi código completo, estoy tratando de generar una cierta cantidad de invitaciones de discordia de acuerdo con el segundo número de argumento

 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!") }

Codifiqué el valor de conteo en la función para obtener un resultado, pero no puedo encontrar una manera de usar argumentos, estoy usando Heroku como host

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Esto se debe a que cuando llamas a la función, ya cambiaste la matriz. Dado que las matrices están indexadas en 0, ¡debe usar args[0] en su lugar!

 async function replyWithInvite(message) { //... console.log("args1: ", args[0]) //should give value //... }

Por si hay alguna duda sobre a dónde se desplazó, aquí se desplazó

 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!