简体   繁体   English

Discord JS Bot 识别特定消息上的特定反应添加和反应删除事件

[英]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.我正在开发一个 Discord.js 机器人,除其他外,我希望它能够发布消息并跟踪添加了多少 :thumbsup: 表情符号作为反应。 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.也就是说,我希望它专门计算 :thumbsup: 表情符号,仅此而已,并且我希望它在每次删除 :thumbsup: 表情符号作为反应时从计数器中删除 1 点。

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.我将不胜感激 stackoverflow 在我努力按预期完成以下工作时提供的任何帮助。

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.在这种情况下,msgChat 被定义(正确,据我所知)到我希望机器人发布的频道。 Everything else in this snippet is constructed as best as I could from the online documentation of Discord.js.这个片段中的所有其他内容都是尽可能从 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.编辑:我根据以下建议对我的代码进行了一些更改,并将我的 Discord.js 版本更新为现代版本。 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 != .使用👍而不是thumbsup=== (如果reaction.emoji.name等于👍则返回true )而不是!= v13 example from docs and v12 example from docs 来自 docs 的 v13 示例和来自 docsv12 示例

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");
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM