简体   繁体   English

为用户添加角色 discord.js

[英]add a role to a user discord.js

I am trying to make a command that gives a role to a member after I type power.我正在尝试创建一个命令,在我键入 power 后赋予成员角色。 I already have a mute command, and that one works completely fine, But if I copy that code and change the name of the command and the role it has to give, it gives the error:我已经有一个静音命令,并且它工作得很好,但是如果我复制该代码并更改命令的名称和它必须赋予的角色,它会给出错误:

TypeError: Cannot read property of 'roles' of undefined TypeError:无法读取未定义的“角色”的属性

my code is:我的代码是:

if(message.content.startsWith("power")) {
    let role =  message.guild.roles.cache.find(r => === "Role_ID");
    let member = message.mentions.members.first();
    member.roles.add(role)
  }

You can try:你可以试试:

if(message.content.startsWith("power")) {
    let role =  message.guild.roles.cache.find(r => === "Role_ID");
    let member = message.mentions.members.first();
    member.addRole(role)
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM