Find out if someone has a role
Asked Answered
C

9

23

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.

Cloutier answered 26/7, 2017 at 4:9 Comment(0)
C
33

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.

Cloutier answered 18/1, 2019 at 15:43 Comment(1)
@JackNicholson if (!message.member.roles.some(role => role.name === 'Whatever')) seems like it would work for that.Gadfly
P
22

message.member.roles is a collection. Instead of getting the roles object, then looking for it, just look for the role directly in the collection. Try this:

else if (command === "addquote" && arg) {
    if(message.member.roles.find(r => r.name === "Admin") || message.member.roles.find(r => r.name === "Mod")){
        // The rest of your code.
    }

Note, the role name must be the name you put in the find method, including any emojis if there's any in the role name.

Previse answered 26/7, 2017 at 5:51 Comment(0)
D
21

This worked for me with version 12.2.0

if(message.member.roles.cache.find(r => r.name === "Admin")) {
    // Your code
}

You can also use r.id to check with the role id

Dudley answered 2/5, 2020 at 10:20 Comment(1)
This is the up-to-date answer.Parrnell
C
10

The Map.has method checks for keys, not values, so you'll have to use the roles' ids instead.

message.member.roles.has(adminRole.id)

message.member.roles.has(modRole.id)

Curate answered 26/7, 2017 at 11:55 Comment(1)
Ahh, thank you, but when I made the correction and added the .id part I got an error that said "Can't find property of null" when doing the if statementCloutier
L
5

Discord.js Version: v12 (stable).

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().

member.roles.cache.has('role-id-here');
// returns true if the member has the role

member.roles.cache.some(role => role.name === 'Admin');
// returns true if any of the member's roles is exactly named "Admin"

Example IF;

const roleById = member.roles.cache.has('123456...');
//or if you want in message
const rolebyIdMessage = message.member.roles.cache.has('12346....');

if(member.roles.cache.has(roleById) return message.reply("Member With Role!");
Lexicography answered 6/5, 2021 at 22:28 Comment(0)
B
4

I'm probably wayy to late with this, but I just had the same problem, and the way i solved it is to search for the role, and then check if the variable has anything on it. Say i wanna check for if the author of the message has admin role:

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

     adminrole = message.member.roles.cache.find(role => role.name == "Admin");
     if(adminrole != null){memberIsAdmin = true;}else{memberIsAdmin = false;}

}

Maybe this will help someone, note that I'm not very experienced with js tho :) Have a nice day y'all

Bureau answered 23/10, 2020 at 9:29 Comment(0)
O
2

if(message.guild.roles.find(role => role.name === "VIP"))

Osmund answered 15/1, 2019 at 8:33 Comment(1)
This will return the role from the server, but does not verify if it has been applied to a user.Gadfly
T
2

That wouldn't work in the newest version of Discord.js which is v12. You need to add if(message.guild.roles.cache.find(roles => role.name === 'VIP'))

Tehuantepec answered 27/2, 2021 at 19:32 Comment(0)
C
0

This might not be traditional or approved, but I've been stuck on this question for a while and the solution in the accepted answer always returned true for me. Anyway, what worked for me was:

message.mentions.members.first()._roles.includes(role.id)

This returns true and false correctly.

Catalano answered 8/1, 2022 at 17:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.