简体   繁体   中英

Bot not replying correctly in a private message

So, this code block is part of a code that player A (challenger) issues a challenge to player B (target) for a game. The bot sends a private message to player B to tell them they were challenged and to ask if they accept or decline the challenge.
The following code doesn't seem to respond to what player B replies with.

if (message.channel.id === '541736552582086656') return target.send("Do you accept the challenge? Please reply with 'accept' or 'deny'.")
  .then((newmsg) => {
    newmsg.channel.awaitMessages(response => response.content, {
      max: 1,
      time: 150000,
      errors: ['time'],
    }).then((collected) => {
      if (collected === 'accept') {
        newmsg.channel.send("You have ***accepted*** the challenge. Please wait while your battlefield is made...");
      } else if (collected === 'deny') {
        newmsg.channel.send("You have ***denied*** the challenge.")
      }
    }).catch(() => {
      newmsg.channel.send('Please Accept or Deny the challenge.');
    });
  });
}

Prior to this code block, I set up logging a message to a channel on the server, sending the challenger and the target the challenge info. The bot successfully contacts the target that they were challenged via pm, but the reply (even when replying with "accept" will still think it was declined.
Thanks for any and all help!

Expanding upon @Stock Overflaw 's answer, awaitMessages will always return a collection of fetched messages, meaning collected === 'accepted' won't work. You're checking if a collection object is the same as a string.

What you need is to grab the first (and in your case only) message from the collection and check its content against a string. Below you will find your .then(...) statement re-written. Give it a go and let me know what the result is.

Ps your collection filter won't work as you might expect. The filter only checks if a message will be added to the collection or not. Since your 'filter' is response => response.content , it will only check if the response.content is not empty, null or undefined

.then((collected) => {
  // Grabs the first (and only) message from the collection.
  const reply = collected.first();

  if (reply.content === 'accept'){
    reply.channel.send("You have ***accepted*** the challenge. Please wait while your battlefield is made...");
  } else if (reply.content === 'deny') {
    reply.channel.send("You have ***denied*** the challenge.") 
  } else {
    reply.channel.send("Your response wasn't valid.");
    /*
     * Code here to handle invalid responses
     */
  }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM