我无法在“messageReactionAdd”中添加角色(添加时出错)。请用于私人不和谐机器人。谢谢
我的代码:
bot.on('messageReactionAdd', async (reaction, user) => {
const member = reaction.message.guild.members.fetch(user.id)
if (reaction.partial) {
// If the message this reaction belongs to was removed, the fetching might result in an API error which should be handled
try {
await reaction.fetch();
} catch (error) {
console.error('Something went wrong when fetching the message: ', error);
// Return as `reaction.message.author` may be undefined/null
return;
}
}
// Now the message has been cached and is fully available
if(reaction.message.id === '853679407499313162'){
const role = reaction.message.guild.roles.cache.find(r => r.name === '?')
console.log(reaction._emoji.name)
if(reaction._emoji.name === '?'){
member.roles.add(role.id)
} /*else if(reaction.name === '?'){
us
}*/
//reaction.users.remove(user.id)
}
});
控制台返回:
TypeError: Cannot read property 'add' of undefine
有什么想法吗?
答案 0 :(得分:2)
您的问题是 fetch
返回一个承诺,所以解决这个承诺,它应该可以工作
这里有一份关于 Promise 的指南,以防你不知道如何解决它们
在 mdn 上另外一个
discord.js docs on fetch method
只是一个建议:你应该使用 await
而不是 then
,因为你当前的代码会更干净