首页 > 解决方案 > 如何从表情符号中仅删除一个用户的反应

问题描述

我的代码:

client.on("messageReactionAdd", (reaction, user) => {
  if (reaction.message.id === "<message ID>")
    reaction.message.reactions.cache.forEach((reaction) =>
      reaction.remove(user.id)
    );
});

我的问题:

上面的代码大部分都有效。每当有人对指定的消息做出反应时,它就会删除他们的反应。但是,它会同时消除其他所有人反应(对那个表情符号)。如何仅从表情符号中删除一个特定用户的反应?

标签: javascriptnode.jsdiscord.js

解决方案


来自Discord.js 文档

const userReactions = message.reactions.cache.filter(reaction => reaction.users.cache.has(userId));
try {
    for (const reaction of userReactions.values()) {
        await reaction.users.remove(userId);
    }
} catch (error) {
    console.error('Failed to remove reactions.');
}

推荐阅读