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

Bib*_*ael 5 javascript bots discord discord.js

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

我不久前为这个功能编写了最初的代码片段,但我为这个功能编写的代码不再有效,如果它确实完全有效的话。我相信我永远无法让反应消除事件正常工作。然而,最近我发现更重要的是,所有表情符号都会触发反应添加事件,而不仅仅是我在这里提到的表情符号。我将不胜感激任何 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");
  });
});
Run Code Online (Sandbox Code Playgroud)

在这种情况下,msgChat 被定义(据我所知是正确的)到我希望机器人发布的频道。此代码片段中的其他所有内容都是我根据 Discord.js 的在线文档尽可能构建的。在我看来,从我读到的内容来看,当前版本中可能不再存在“删除”事件?但我在寻找如何替换其功能方面经历了一段糟糕的时光。

编辑:我根据下面的建议对我的代码进行了一些更改,并将我的 Discord.js 版本更新为现代版本。我很高兴地报告,删除事件现在似乎按预期工作,但仅应考虑竖起大拇指表情符号的过滤器仍然无法工作。同样,任何反应去除事件都会通过过滤器。

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");
            });
        });
Run Code Online (Sandbox Code Playgroud)

小智 1

使用代替thumbsupand (如果等于则===返回)代替。文档中的 v13 示例文档中的 v12 示例truereaction.emoji.name!=

您不需要检查事件侦听器内的表情符号,因为您已经过滤了它们。

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");
    });
});
Run Code Online (Sandbox Code Playgroud)