Find out if someone has a role

R. Gillie picture R. Gillie · Jul 26, 2017 · Viewed 71.4k times · Source

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.

Answer

Wright picture Wright · Jul 26, 2017

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 => rname === "Mod")){
        //Rest of your code
    }

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