查明某人是否有角色

Find out if someone has a role(查明某人是否有角色)

本文介绍了查明某人是否有角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为服务器制作了一个简单的报价机器人,但管理员只希望 mod+ 的人能够添加报价以避免垃圾邮件.我去了文档并做了所有事情,但我无法让它工作.这是我所拥有的:

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);            
        }   
    }
}

非常挑剔.有时,如果用户具有 mod 角色,它会起作用,但大多数时候它不会.如果我这样做了

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));

两者都将输出为 false,但会起作用吗?老实说,我现在不知道.

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

推荐答案

discord.js api 已更新,由于 .exists() 已被弃用,因此有更好的方法.

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')) {}

这比 .find() 好,因为 .find() 返回角色对象(或未定义),然后将其转换为布尔值..some() 方法默认返回一个布尔值.

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.

这篇关于查明某人是否有角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:查明某人是否有角色

基础教程推荐