Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find out if someone has a role

I made a simple quote bot for a server, but the admin only wants mod+ people to be able to add quotes to avoid spam. I went to the documentation and did everything, but I can't get this to work. Here's what I have:

//other code
else if (command === "addquote" && arg) {
    let adminRole = message.guild.roles.find("name", "Admin");
    let modRole = message.guild.roles.find("name", "Mod");

    if(message.member.roles.has(adminRole) || message.member.roles.has(modRole)){
        const hasArr = arr.some((el) => {
            return el.toLowerCase().replace(/\s/g, '') === arg.toLowerCase().replace(/\s/g, '');
        });

        if(hasArr){
            message.channel.send(arg.replace(/\s+/g,' ').trim() + " is already a Quote");
        } else {
            fs.appendFileSync('./Quotes.txt', '\r\n' + arg);
            message.channel.send("Quote added: " + arg);
            arr.push(arg);            
        }   
    }
}

It's very finicky. Sometimes it will work if the user has the mod role, most of the times it wont. If I do

console.log(message.memeber.roles.has(adminRole));
console.log(message.memeber.roles.has(modRole));

both will output to false, but will work? Honestly, I have no idea at this point.

like image 822
R. Gillie Avatar asked Jul 26 '17 04:07

R. Gillie


People also ask

How do I check if a user has a role?

If you have the role ID , you can check if the . roles Collection on a GuildMember object includes it, using .has() . Should you not know the ID and want to check for something like a "Mod" role, you can use . some() .

How do you find out who has a role on discord?

If there is a dedicated text channel for the role, its easy to see how many (and who) has that specific role given the perms for the channel on the right side of the application. There is also the option of going into Server Settings > Members and sorting by role.

How do you check if a person has a role discord JS?

To know if one of a member's roles has a permission enabled, you can use the .has() method on GuildMember#permissions open in new window and provide a permission flag, array, or number to check for.


1 Answers

The discord.js api has been updated and there is a better way since .exists() has been deprecated.

if (message.member.roles.cache.some(role => role.name === 'Whatever')) {}

This is better than .find() because .find() returns the role object (or undefined) which is then converted into a boolean. The .some() method returns a boolean by default.

like image 197
R. Gillie Avatar answered Oct 11 '22 13:10

R. Gillie