简体   繁体   中英

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. 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

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)
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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