我正在尝试添加静音命令
这是我当前的代码:
if (message.member.roles.cache.some(role => role.name === 'Admin')) {
let target = message.mentions.members.first;
let role = message.member.guild.roles.cache.find(role => role.name === "muted");
target.roles.add(role);
message.channel.send('${user} has been muted by ${message.author.username}');
}
我收到此错误:
<块引用>(node:11052) UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“add”
答案 0 :(得分:0)
Roles#has
来搜索成员角色if (message.member.roles.cache.has('role-id') { ... }
Collection#first
是方法,不是属性,添加 ()
到 first
let target = message.mentions.members.first();
您收到 Cannot read property 'add' of undefined
是因为您没有正确访问第一次提及
这里是所有固定和组合在一起的代码:
if (message.member.roles.cache.has('ROLE-ID-HERE')) {
let target = message.mentions.members.first();
let role = message.member.guild.roles.cache.find(role => role.name === "muted");
target.roles.add(role);
message.channel.send('${user} has been muted by ${message.author.username}');
}