问题是messageReactionAdd有两个参数;消息反应作为第一个,应用表情符号的用户作为第二个。当你写的时候reaction_orig, message, user
, reaction_orig
是反应(这是正确的),但是message
是做出反应的用户,因为它是第二个参数。这user
变量将是undefined
.
另一个问题是reaction_orig.users
返回一个反应用户管理器没有一个id
财产。幸运的是,user
已经传递给您的回调,因此您可以使用它的 ID。
Also, reaction_orig
has a message
属性,该反应引用的原始消息,以便您可以从中获取其作者 ID。
您可以将代码更改为这样才能工作:
client.on('messageReactionAdd', (reaction_orig, user) => {
if (reaction_orig.message.author.id === user.id) {
// the reaction is coming from the same user who posted the message
return;
}
manageBoard(reaction_orig);
});
但是,上面的代码仅适用于缓存的消息,即连接机器人后发布的消息。对旧消息做出反应不会触发messageReactionAdd
事件。如果您还想听取对旧消息的反应,您需要启用部分结构MESSAGE
, CHANNEL
and REACTION
实例化您的客户端时,如下所示:
const client = new Discord.Client({
partials: ['MESSAGE', 'CHANNEL', 'REACTION'],
});
您可以通过例如检查消息是否被缓存检查是否是author
财产不是null
。如果它是null
, 你可以获取消息。现在,您已经有了消息作者和做出反应的用户,因此您可以比较他们的 ID:
// make sure it's an async function
client.on('messageReactionAdd', async (reaction_orig, user) => {
// fetch the message if it's not cached
const message = !reaction_orig.message.author
? await reaction_orig.message.fetch()
: reaction_orig.message;
if (message.author.id === user.id) {
// the reaction is coming from the same user who posted the message
return;
}
// the reaction is coming from a different user
manageBoard(reaction_orig);
});