简体   繁体   中英

How do I wait for a message to be sent in a channel with DiscordJS?

I'm making an application bot and it will have options for multiple choice and short/long responses. The responses system uses direct messages and needs to wait for a message to be sent before continuing, but MessageChannel#awaitMessages isn't working for me. Here is my code (two separate parts):

// borrowed from another stackoverflow question
const waitUntil = (condition, checkInterval = 100) => {
  return new Promise(resolve => {
    let interval = setInterval(() => {
      if (!condition()) return;
      clearInterval(interval);
      resolve();
    }, checkInterval)
  })
}
let shouldStop = false
var content = 1
var contentArrived = false
const filter = (m) => m.author.id == userId // userId is the ID of the user in DMs
interaction.channel.awaitMessages({ filter, max: 1, maxProcessed: 1, idle: 10_000, errors: ['idle'] })
    .then(collected => {
          console.log(`${collected.size} is pain and suffering`)
          content = collected.first().content
          contentArrived = true
     })
     .catch(() => {
          message.channel.send('You did not respond to the question so your application was canceled.')
          shouldStop = true
          currentlyApplying.set(userId, null)
     })
await waitUntil(() => {
    return contentArrived || shouldStop
}, 500)

I tried with awaitMessages with examples from the documentation and from other StackOverflow questions, and none of them worked. They all simply got stuck and did not detect any message, even though the filter was set to detect all non-bot messages. They would all then throw an error and be caught by the .catch() .

Solution was to include the message partial and enable the DirectMessages Intent when creating the Client.

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