Skip to content

Message collector responds to other people’s messages

I’m trying to make a collector which will collect the mentioned user’s message. But even with filter my bot respond to it’s own message and other peoples messages! Here is my test.js file code:

const mentioned = message.mentions.users.first();

const filter1 = (msg) => {
  return msg.author.id === mentioned.id
}

const collector1 = await message.channel.createMessageCollector({ filter1, max: 1, time: 120000 })

collector1.on('collect', message => {
  console.log(message.content)
})

collector1.on('end', (collected) => {
  if (collected.size === 0) return message.channel.send("Mentioned user did not respond in time!")

  collected.forEach((message) => {
    if (message.content.toLowerCase() == 'accept') {
      message.channel.send(`${mentioned} accepted!`)
    }
    if (message.content.toLowerCase() == 'cancel') return message.channel.send(`${mentioned} declined!`)
  })
})

I was changing my filter many times, but I still can’t fix this problem, so what am I doing wrong? Also I use djs v13

Answer

The problem is you’re trying to use Short-Hand Property Assignment to assign the filter option. However, you pass in “filter1” which results in {filter1: filter1}. Since this does not resolve to a filter option for TextChannel#createMessageCollector() the method disregards the unknown option and therefor your collector has no filter.

Change your filter1 variable to filter

const filter = (msg) => {
  return msg.author.id === mentioned.id
}

const collector1 = await message.channel.createMessageCollector({ filter, max: 1, time: 120000 })