我该如何兑现这个承诺



这是我的代码:

collector.on('collect', async (reaction, user) => {
console.log(reaction);
if (!user.bot) {
let role = reaction.message.guild.roles.cache.find(role => role.name === ra[emojiarray.indexOf(reaction)]);
await reaction.message.guild.members.fetch(user).roles.add(role);
}
});

除了我得到错误UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'add' of undefined。有人告诉我要兑现承诺,我试过了,但我想我做得不对。我该怎么解决这个问题?

谢谢!

您可能需要await获取用户,然后将角色添加到用户。

collector.on('collect', async (reaction, user) => {
console.log(reaction);
if (!user.bot) {
let role = reaction.message.guild.roles.cache.find(role => role.name === ra[emojiarray.indexOf(reaction)]);
let userMember = await reaction.message.guild.members.fetch(user);
userMember.roles.add(role);
}
});

最新更新