'How do I make a message collector in discord.js to have multiple responses according to the message the user sent?

I'm trying to make a start command for a currency bot. I'm trying to make a message collector, which asks for collecting for this certain user. And depending on the user's response, it would have different responses.

  • If the user replies with "yes", the bot will send "cool".
  • If the user replies with "no", the bot will send "not cool".
  • If the user doesn't reply in 15 seconds or replies something else than "yes" and "no", the bot will send "bruh".
message.channel.send("Yes or no?")
        .then(function (message) {
            const filter = m => m.author.id === message.author.id,;
            const collector = message.channel.createMessageCollector(filter, { max: 1, time: 15000, errors: ['time'] })
            collector.on('collect', m => {
                if (m == "yes") {
                  message.channel.send("cool")
                } else if (m == "no") {
                  message.channel.send("not cool")
                } else {
                    message.channel.send("bruh")
                } 
                
              })
          })
    }

But whatever I say, the bot doesn't respond at all, even in the console.
Thanks for any help!



Solution 1:[1]

The message parameter of the MessageCollector returns a message object. What you are currently attempting to do is comparing an object (The parameter m that represents the object) with a string - Something that can obviously not be done. If you would like to compare the collected message to a certain string, you would have to compare its content property, that obviously returns the content of the collected message.

Final Code:

message.channel.send("Yes or no?")
        .then(function (message) {
            const filter = m => m.author.id === message.author.id,;
            const collector = message.channel.createMessageCollector(filter, { max: 1, time: 15000, errors: ['time'] })
            collector.on('collect', m => {
                if (m.content == "yes") {
                  message.channel.send("cool")
                } else if (m.content == "no") {
                  message.channel.send("not cool")
                } else {
                    message.channel.send("bruh")
                } 
                
              })
          })
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Itamar S