get all roles of a user discord.js code example

Example 1: how to give roles discord.js

let role = message.guild.roles.find(r => r.name === "Role Name");

// Let's pretend you mentioned the user you want to add a role to (!addrole @user Role Name):
let member = message.mentions.members.first();

// or the person who made started the command: let member = message.member;

//adds the role
member.roles.add(role)

Example 2: discord.js get all members with role

//outputs the IDs of all members with the specified role as an array

// === discord.js v11 ===
message.guild.roles.get('ROLE-ID').members.map(m=>m.user.id);

// === discord.js v12 ===
message.guild.roles.cache.get('ROLE-ID').members.map(m=>m.user.id);