Cristian LMAO
Cristian LMAO

Reputation: 11

If a player have a role (discord.js)

So i made a discord bot, and i want to make a command that works only if the person who used the command have a role called "colorm"

this is this code

client.on('message', msg => {

  if (msg.content === '!setupcolors'){
    if(msg.guild.roles.find(role => role.name === "colorm")){
        console.log("cmd: !setupcolors >> Command used.");
        msg.reply(String('Hi! this command is under constuction'));
    }
    else{
        msg.reply(String('Hi! you need to have a rank called "colorm" to acces this command.'));
        console.log("Error[cmd: !setupcolors] >> No perms.");
    }
  }
});

thanks! :D

Upvotes: 1

Views: 2698

Answers (2)

Toasty
Toasty

Reputation: 1880

You can check whether a member has a specific role as follows:

msg.member.roles.find(r => r.name === "colorm")

Alternatively, you can use the ID of the role to check:

// First you need to retrieve the role:
const role = guild.roles.cache.find(r => r.name === "colorm")

// You can now check if the user has the role with:
member.roles.cache.has(role.id)

// (Optional)
// If 'role' isn't undefined and the user has the appropriate role you can continue
if (role && member.roles.cache.has(role.id)) {
    // Do your stuff here, if the user has the role
}

Upvotes: 1

Roshan Kanwar
Roshan Kanwar

Reputation: 271

Try using this - if(!msg.member.roles.cache.some(role => role.name === 'colorm'))

Upvotes: 0

Related Questions