Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
311 views
in Technique[技术] by (71.8m points)

javascript - 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', '
' + 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...