delete role 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 find role by name

let role = message.guild.roles.cache.find(role => role.name === "Rolename");

Example 3: discord.js remove every role a user has

case "suspend":
  if (message.member.hasPermission('MANAGE_ROLES')) {
    let rMember = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0])); //Gets the user
    if (!rMember) return message.reply("That user does not exist.");
    let gRole = message.guild.roles.find('name', 'SUSPENDED'); //Gets the SUSPENDED role

    rMember.removeRoles(rMember.roles).then(console.log).catch(console.error); //Removes all roles
    rMember.addRole(gRole.id); //Adds suspended Role


    message.channel.send("User was suspended."); //Messages the channel that the user was suspended

    try {
      await rMember.send("You have been suspended."); //Tries to DM User
    } catch (e) {
      message.channel.send("We tried to DM the user to let them know, but their DM's are locked."); //Announces that their DMs are locked
    }


  } else {
    message.channel.send("You do not have permission to use this command.");
  }
  break;