Search code examples
node.jseventsdiscorddiscord.jsbots

In discord.js, reactionCollector doesn't stop after calling stop() method


I'm working on a bot in discord.js and am currently trying to do the following: By triggering a command named tournament prepare, the bot waits for user inputs with a reactionCollector, collecting the reactions of the users that want to participate to the tournament.

Later, when the user that started the tournament uses the command tournament begin, the bot should close the collector, and output a list with the names of the players that joined the event.

This is what I've tried:

// initialise the collector
var collector = null;

if(message.content.startsWith(`${prefix}prepare`)) {
  message.channel.send('A message')
    .then(async function (message) {
      await message.react('⚔')

      const filter = (reaction, user) => {
        return reaction.emoji.name === '⚔' && user.id != 705462496646922311
      };
      collector = message.createReactionCollector(filter);
      collector.on('collect', (reaction, user) => {
        // code here
      });
    })
    .catch(function() {
      message.channel.send('Error while adding the reaction. Try again')
    });

}

if(message.content.startsWith(`${prefix}begin`)) {
  if(collector == null) {
    message.channel.send('Error message');
    return;
  }

  collector.stop();
  collector.on('end', collected => {
    // code here
  });
}

Everything works fine until the end event. The documentation says this about the stop() method: Stops this collector and emits the end event.

I'm sure that the collector is stopped after using the stop() method, because I've debugged it, but for some reasons the end event never gets called. Any help?


Solution

  • I think, that its because you first stop it and than connect the event, so it will not fire, because collector already ended.