JavaScript Discord Bot Multiple Reaction Issue

I’m working on a Discord bot using JavaScript and running into a problem with reaction handling. The bot should respond when users click on reactions, but it’s triggering multiple times when there are several messages with reactions present.

My bot is designed to roll dice and let users reroll by clicking a reaction emoji. Everything works fine with one message, but when multiple messages have the same reaction emoji, the bot processes all of them instead of just the one the user clicked.

Here’s my main handler:

bot.on('message', msg => {
  if(!msg.content.startsWith(PREFIX) || msg.author.bot) return;
  let params = msg.content.substring(PREFIX.length).split(" ");
  
  switch (params[0].toLowerCase()) {
    case "dice": case "d":
      if(!params[1] || params[1].indexOf("d") == -1) return msg.channel.send('Bad dice format');
      bot.commands.get('gamebot').HandleDice(msg, params, bot);
      break;
  }
});

And here’s the dice rolling module:

module.exports = {
  name: 'gamebot',
  description: 'Dice system for RPG',
  async HandleDice(msg, params, botClient) {
    const targetChannel = 'YYYYYYYYYYYYYYYYY';
    const rollAgain = '🎲';
    
    if(!params[2]) {
      var result = RollCalculator(params[1]);
    } else if (params[2] == "s" || params[2] == "S") {
      var result = RollCalculator(params[1], true);
    }
    
    let diceMessage = await msg.channel.send(result);
    diceMessage.react(rollAgain);
    
    botClient.on('messageReactionAdd', async (reactionObj, userObj) => {
      if(reactionObj.message.partial) await reactionObj.message.fetch();
      if(reactionObj.partial) await reactionObj.fetch();
      if(userObj.bot) return;
      if(!reactionObj.message.guild) return;
      
      if(reactionObj.message.channel.id == targetChannel) {
        if(reactionObj.emoji.name === rollAgain) {
          var freshRoll = RollCalculator(params[1]);
          await msg.channel.send(freshRoll);
          await reactionObj.users.remove(userObj);
        }
      }
    });
  }
}

How can I fix this so the bot only responds to reactions on the specific message instead of all messages with that emoji? I’m pretty new to Discord bot development so any help would be great.

You’re registering a new event listener every time HandleDice runs, but they never get cleaned up. All those listeners pile up and respond to reactions on any message in that channel. You need message-specific reaction handling. Ditch the global messageReactionAdd event and use a reaction collector on the individual message instead. Replace your botClient.on(‘messageReactionAdd’) section with this: javascript const filter = (reaction, user) => { return reaction.emoji.name === rollAgain && !user.bot; }; const collector = diceMessage.createReactionCollector({ filter, time: 60000 }); collector.on('collect', async (reaction, user) => { var freshRoll = RollCalculator(params[1]); await msg.channel.send(freshRoll); await reaction.users.remove(user); }); This collector only listens to reactions on that specific dice message and expires after 60 seconds. No more duplicate triggers from other messages.

Yeah, your event listener placement is the problem. Each dice roll creates a new messageReactionAdd handler without removing the old ones, so they pile up. That’s why you’re getting multiple reactions - you’ve got like 10 listeners all responding to the same thing. Move the listener outside HandleDice and just check if the message ID matches your dice message instead of creating one per command.

You’re creating a new messageReactionAdd listener every time someone runs the dice command. That’s why you’re getting multiple responses - all those listeners fire when any reaction gets added.

Don’t put the event listener inside your HandleDice function. Instead, register it once when your bot starts up, then check if the reaction belongs to a dice message.

Here’s how to fix it: Store dice message IDs in a Set or Map. Move your messageReactionAdd listener to your main bot file with your other handlers. When you create a dice message, add its ID to your collection. In the reaction handler, check if the message ID exists in your collection before doing anything.

This way you’ll only respond to reactions on actual dice messages and stop the duplicate responses.