Reputation: 85
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?
Upvotes: 7
Views: 98102
Reputation: 19
As of now, 2020, this Code works fine, and its easy to implement since its only based on the message object
let role = message.member.guild.roles.cache.find(role => role.name === "your role");
if (role) message.guild.members.cache.get(message.author.id).roles.add(role);
Upvotes: 0
Reputation: 49
For me (right now) this works only:
var role = message.member.roles.cache.find(role => role.name === "role name");
if (!role) return;
message.member.guild.roles.add(role);
Upvotes: 0
Reputation: 610
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.
Upvotes: 18
Reputation: 4497
You can do this with:
var role = message.guild.roles.find(role => role.name === "MyRole");
message.member.addRole(role);
Upvotes: 11