javascript - 如何从表情符号中仅删除一个用户的反应
问题描述
我的代码:
client.on("messageReactionAdd", (reaction, user) => {
if (reaction.message.id === "<message ID>")
reaction.message.reactions.cache.forEach((reaction) =>
reaction.remove(user.id)
);
});
我的问题:
上面的代码大部分都有效。每当有人对指定的消息做出反应时,它就会删除他们的反应。但是,它会同时消除其他所有人的反应(对那个表情符号)。如何仅从表情符号中删除一个特定用户的反应?
解决方案
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.');
}
推荐阅读
- pyspark - pyspark中的左外连接后删除功能不起作用
- excel - VBA使用msoFolderPicker获取目录路径,如何在另一段代码中使用该值
- powerbi - 在 Power BI 中使用联接的不同行数
- raku - 如何通过克隆填充 Callable 属性?
- javascript - 如何让 webpack 生成包含所需块的单个 JS 文件?
- sql - 从多个表中选择(由另一个选择传递)
- apache - mod_security 基于发布数据的关闭规则
- asynchronous - 等待而不将函数声明为异步
- r - 使用外部列表从行中删除数据
- java - 我可以一次声明一个数组并在整个代码中再次声明它吗?