简体   繁体   中英

Discord JS Bot to recognize specific reaction add and reaction remove events on specific message

I am working on a Discord.js bot and, among other things, I want it to be able to post a message and keep track of how many :thumbsup: emojis are added as reactions. That is to say, I want it to count :thumbsup: emojis specifically, nothing else, and I want it to remove from the counter 1 point every time a :thumbsup: emoji is removed as a reaction.

I wrote the initial snippet for this feature some time ago, and the code I wrote for this feature no longer works, if indeed it ever completely did. I believe I was never able to get the reaction removal events to work properly. Recently I've discovered that even more than that, however, the reaction add event is being triggered for ALL emojis, not just the one I name here. I would be grateful for any assistance stackoverflow could offer in my efforts to make the following work as intended.

msgChat.send("MESSAGE CONTENT").then(msgElement => {
  const thumbFilter = (reaction) => { return reaction.emoji.name != 'thumbsup' };
  const thumbCounter = msgElement.createReactionCollector(thumbFilter, {dispose: true, time: 28800000 });
  var thumbCount = 0;
  thumbCounter.on('collect', (r, c) => {
    if (r.emoji.name == "thumbsup") {
      thumbCount++;
      if (thumbCount == 6) {
        msgChat.send("Six thumbs were gathered");
        thumbCounter.stop();
      }
    }
  });
  thumbCounter.on('remove', (r, c) => {
    if (r.emoji.name == "thumbsup") {
      thumbCount--;
    }
  });
  thumbCounter.on('end', (r, c) => {
    console.log("Collected " + thumbCount + " thumbs before closing");
  });
});

msgChat in this case is defined (correctly, as far as I can tell) to the channel in which I want the bot to post. Everything else in this snippet is constructed as best as I could from the online documentation of Discord.js. It seems to me, from what I've read, that the 'remove' event may no longer exist in the current version? But I have had a terrible time finding out how to replace its functionality.

EDIT: I have made some changes to my code according to advice below, and updated my Discord.js version to a modern version. I am happy to report that the remove event now appears to work as intended, but the filter, by which only thumbs up emojis should be considered, is still not working. Likewise, any reaction removal event is passing the filter.

msgChat.send("MESSAGE CONTENT").then(msgElement => {
            const thumbFilter = (reaction) => { return reaction.emoji.name === '👍' };
            const thumbCounter = testWarning.createReactionCollector({thumbFilter, dispose: true, time: 28800000 });
            var thumbCount = 0;
            thumbCounter.on('collect', (r) => {
                console.log("Collected thumbsup");
                thumbCount++;
                if (thumbCount == 6) {
                    msgChat.send("Six thumbs were gathered");
                    thumbCounter.stop();
                }
            });
            thumbCounter.on('remove', (r) => {
                console.log("removing thumbsup");
                thumbCount--;
            });
            thumbCounter.on('end', (thumbs) => {
                console.log("Collected " + thumbCount + " thumbs");
            });
        });

Use 👍 instead of thumbsup and === (return true if reaction.emoji.name is equal to 👍 ) instead of != . v13 example from docs and v12 example from docs

You don't need to check emojis inside the event listeners because you already filtered them.

msgChat.send("MESSAGE CONTENT").then(msgElement => {
    const thumbFilter = ( reaction ) => reaction.emoji.name === '👍';
    const thumbCounter = msgElement.createReactionCollector(thumbFilter, { dispose: true, time: 28800000 });
    var thumbCount = 0;
    thumbCounter.on('collect', () => {
        thumbCount++;
        if (thumbCount === 6) {
            msgChat.send("Six thumbs were gathered");
            thumbCounter.stop();
        }
    });
    thumbCounter.on('remove', () => {
        thumbCount--;
    });
    thumbCounter.on('end', () => {
        console.log("Collected " + thumbCount + " thumbs before closing");
    });
});

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