我正在尝试从反应中添加角色。我收到此错误。 Cannot read property 'add' of undefined.
下面是我的代码。
client.on('message', message => {
const listen = message.content;
if(message.author.bot) {
if(message.embeds) {
const embedMsg = message.embeds.find(msg => msg.title === 'Choose your favorite team!');
if(embedMsg) {
message.react('797575748307255316')
.catch(err => console.error)
}
}
return;
}
if(listen.toLowerCase() === '!claim roles1') {
const embed = new Discord.MessageEmbed()
.setTitle("Choose your favorite team!")
.setColor("BLUE")
.setDescription("<:76ers:797575748307255316> - Philadeplhia 76ers\n"); +
message.channel.send(embed)
return;
}
})
client.on('messageReactionAdd', (reaction, user) => {
if(user.bot) return;
var roleName = reaction.emoji.name;
var role = reaction.message.guild.roles.cache.find(role => role.name.toLowerCase() === roleName.toLowerCase());
var member = reaction.message.guild.members.cache.find(member => member.id === user.id);
reaction.message.guild.members.fetch(user).then(m => m.roles.add(role).then(member => {
console.log('Added ' + member.user.username + ' to the ' + role.name + ' role.');
}))
if(member.guild.roles.cache.has(role.id)) {
reaction.message.guild.members.fetch(user).then(m => m.roles.remove(role).then(member => {
console.log('Removed ' + member.user.username + ' from the ' + role.name + ' role.');
}))
}
})
我使用 Discord.MessageEmbed 来很好地显示它,它应该让用户通过他们对反应做出反应来获得角色。
答案 0 :(得分:0)
我认为这是因为您使用的 .some()
只返回 true
或 false
。在两种情况下(.find()
和 role
)都使用 member
。
编辑:
试试这个:
client.on('messageReactionAdd', (reaction, user) => {
if(user.bot) return;
var roleName = reaction.emoji.name;
var role = reaction.message.guild.roles.cache.find(role => role.name.toLowerCase() === roleName.toLowerCase());
reaction.message.guild.members.fetch(user).then(m => m.roles.add(role).then(member => {
console.log('Added ' + member.user.username + ' to a role.');
}))
})