无法读取null discord bot的属性“角色”

时间:2020-08-02 08:33:13

标签: javascript node.js discord discord.js

我有一个脚本,如果用户被静音,它将取消静音,但是我找不到具有静音角色的成员。

我的代码:

if (message.guild.member(message.mentions.users.id).roles.has(role => role.name === "Muted")) {
    message.guild.member(message.mentions.users.id).roles.remove(role => role.name === "Muted")
} else if (!message.guild.member(message.mentions.users.id).roles.has(role => role.name === "Muted")) {
    message.guild.member(message.mentions.users.id).roles.add(role => role.name === "Muted")
}

1 个答案:

答案 0 :(得分:0)

您可以搜索成员的角色,以查看他们是否具有 Mute 角色:

const member = message.mentions.members.first();
if (!member) return message.reply('You didn't mention a user to (un)mute.');

if (member.roles.cache.find(role => role.name === 'Muted')) {
  member.roles.remove('Muted');
} else {
  member.roles.add('Muted');
}

如果要查找某些对象的属性和方法,请选中Discord.js docs