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

153
Views
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 answers
Answer question

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 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!