littleBitsman
littleBitsman

Reputation: 31

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().

Upvotes: 1

Views: 265

Answers (1)

littleBitsman
littleBitsman

Reputation: 31

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

Upvotes: 1

Related Questions