1
votes

I have a command where my bot is supposed to give a person that I mention in my message a role, the bot gives the user the role just fine. However when I run the command on someone who already has the role it doesn't stop and say "user already has role", it continues and says it added the role to the user.

Here's my code:

module.exports = {
    name: 'pm',
    description: 'sets pm',
    permissions: ['ADMINISTRATOR'],
    execute(client, message, args, Discord){
        try {
            
            let role = message.guild.roles.cache.get("role id");
            let member = message.mentions.members.first();
            if(!member) return message.channel.send('***Error:*** Please specify a user.');
            
            if(member.roles.cache.has(role)) {
                message.channel.send(`***Error:*** ${member} is already a PM.`)
            } else {
                member.roles.add(role);
                message.channel.send(`Added ${member}`)
            }


        } catch (e) {
            message.channel.send(`***Error:*** ${e}`)
            console.log(e)
        }
    }
}
1

1 Answers

1
votes

You were really close. All you have to do is swap out member.roles.cache.has(role) with member.roles.cache.has(role.id).

Reference: https://anidiots.guide/understanding/roles