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

155
Vistas
How to make discord.js read reply

I'm currently developing a discord.js bot on the latest version. I'm currently trying to get the following bit of code to send a message giving instructions with what to say, then once I get a response, send another message. however, I can't get it working. any help would be appreciated!

client.on('messageCreate', message => {
  //check if message is from self
  if (message.author === client.user) {
    //if it is. do nothing
    return
  } else {
    //if its not, read message
    if (message.content === prefix + "start") {
      let filter = m => m.author.id === message.author.id
      message.channel.send("Testing.. Type YES or NO").then(() => {
        message.channel
          .awaitMessages(filter, {
            max: 1,
            time: 30000,
            errors: ['time']
          })
          .then(message => {
            message = message.first()
            if (message.content.toUpperCase() == 'YES' || message.content.toUpperCase() == 'Y') {
              message.channel.send(`Hello World!`);
            } else if (message.content.toUpperCase() == 'NO' || message.content.toUpperCase() == 'N') {
              message.channel.send(`Ping`);
            } else {
              message.channel.send(`Terminated: Invalid Response`);
            }
          })
          .catch(collected => {
            message.channel.send('Timeout');
          });
      })
    }
  }
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Scoping problems. The result of your awaitMessages() promise is called message. However, the outer scope of the function itself has a variable named message as well, hence you are referencing different things:

        // from client.on("messageCreate", message)
        message.channel.awaitMessages(filter, {
              max: 1,
              time: 30000,
              errors: ['time']
            })
            // not the same; from awaitMessages() instead
            .then(message => {
              message = message.first()
            })

You can fix it by doing this:

message.channel.awaitMessages(filter, {
    max: 1,
    time: 30000,
    errors: ['time']
})
// we call it 'collected' as we essentially collect a message
.then(collected => {
    // redeclare a new variable named 'msg'
    const msg = collected.first()
    if (msg.content.toUppercase() == "YES" || msg.content.toUppercase() == "Y") {
        msg.channel.send(`Hello World!`);
    }
    else if (...) 

You get the gist here.

about 4 years ago · Juan Pablo Isaza 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