为什么 messageReactionAdd 什么都不做 discord.js

Why messageReactionAdd do nothing discord.js

我正在尝试使用 node.js 编写一个 discord 机器人,但我对 messageReactionAdd 有问题我现在不知道为什么当我对表情符号做出反应时机器人什么都不做。

我的代码:

bot.on('messageReactionRemove', (reaction, user) => {
console.log("that work 1");
if(reaction.emoji.name === "white_check_mark") {
    console.log("that work 2");
}})

你正在做反应删除,你需要使用 Unicode 表情符号 - 你可以在网上找到这些并复制它们

事件 messageReactionAddmessageReactionRemove 仅适用于缓存的消息。您需要将原始事件添加到代码中以触发任何消息。 https://github.com/AnIdiotsGuide/discordjs-bot-guide/blob/master/coding-guides/raw-events.md

您应该收听 messageReactionAdd 事件。
另请记住,ReactionEmoji.name 是该表情符号的 Unicode:您可以通过在表情符号前写一个反斜杠来获取 Unicode 符号,例如 \:joy::white_check_mark: 的 Unicode 是 ✅.

这应该是您的代码:

bot.on('messageReactionAdd', (reaction, user) => {
  console.log("first check");
  if (reaction.emoji.name === "✅") {
    console.log("second check");
  }
});

这将适用于每条缓存的消息,如果您希望它仅适用于特定消息,请尝试使用 Message.awaitReactions() or Message.createReactionCollector()

消息必须缓存后才能生效。在您的机器人打开时收到的任何消息都会自动缓存,但如果是特定的旧消息,您可以使用:

client.channels.get(CHANNELID).fetchMessage(MESSAGEID);

缓存它。执行此操作后(每次您的脚本是 运行),您将收到该消息的反应事件。

2022 年更新。您可能需要为 GUILD_MESSAGESGUILD_MESSAGE_REACTIONS 添加带有缓存消息的意图才能使其正常工作。

参考:https://discordjs.guide/popular-topics/reactions.html#listening-for-reactions-on-old-messages

const client = new Client({
    intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS],
    partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
});