Discord bot 添加角色

时间:2021-02-16 15:53:31

标签: discord discord.js bots

我正在尝试添加静音命令

这是我当前的代码:

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”

1 个答案:

答案 0 :(得分:0)

  1. 我建议您使用 Roles#has 来搜索成员角色
if (message.member.roles.cache.has('role-id') { ... }
  1. 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}');
}