提问者:小点点

找出某人是否有角色


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

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

这非常挑剔。有时如果用户拥有mod角色,它会起作用,但大多数情况下它不会。如果我这样做了

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

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


共3个答案

匿名用户

discord. js api已更新,并且有更好的方法,因为。存在()已被弃用。

if(消息.成员.角色.缓存.一些(角色=

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

匿名用户

message.成员.角色是一个集合。与其获取角色对象,然后查找它,不如直接在集合中查找角色。试试这个:

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.
    }

注意,角色名称必须是您在查找方法中输入的名称,如果角色名称中有表情符号,则包括任何表情符号。

匿名用户

这适用于我的版本12.2.0

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

也可以使用r.id检查角色id