GuyWhoDoThings
GuyWhoDoThings

Reputation: 85

add user to role with newest discord.js

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

Answers (4)

PersonWhoLikesCPP
PersonWhoLikesCPP

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

Axwabo
Axwabo

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

Y. Georgiev
Y. Georgiev

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

André
André

Reputation: 4497

You can do this with:

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

Upvotes: 11

Related Questions