add user to role with newest discord.js

2020-04-19 05:39发布

问题:

i'm using newest discord.js in node.js and i'm trying to add user to role, but it seems bot.addUserToRole() was removed.

How can I do it when I know only rank name, not it's ID?

回答1:

You can do this with:

var role = message.guild.roles.find(role => role.name === "MyRole");
message.member.addRole(role);


回答2:

Here's what worked for me, hope this helps!

var role= member.guild.roles.cache.find(role => role.name === "role name");
member.roles.add(role);

Here is the official documentation on it.



回答3:

var role = message.guild.roles.find(role => role.name === "theroleimade");
                                ^

TypeError: message.guild.roles.find is not a function

why does it say that?