【问题标题】:How do I check if a user has a certain role discord.js v12? [duplicate]如何检查用户是否具有特定角色 discord.js v12? [复制]
【发布时间】:2020-12-25 06:54:09
【问题描述】:

我刚刚开始创建我的 discord 机器人,我想发出警告命令,但我只希望具有主持人角色的人能够使用它。有没有办法检查用户是否具有特定角色?谢谢。到目前为止我的代码:

module.exports = {
    name: 'warn',
    description: "The bot will warn the mentioned user",
    execute(message, args){
        var warnedone = message.mentions.first()
        if(author doesnt have a role with ID 712229762742878321) {
             return message.reply('can not use this command')
        }
    }
}

【问题讨论】:

    标签: javascript discord.js


    【解决方案1】:

    如果你想检查一个角色,你可以这样做:

    if (!message.member.roles.cache.has('712229762742878321')) {
        return message.reply('can not use this command')
    };
    

    但是,我建议您检查权限,因为这会使该命令无法用于具有管理员角色的人。您可以像这样检查权限:

    if (!message.member.permissions.has('MANAGE_ROLES')) {
        return message.reply(`you don't have manage roles permissions that are required to execute this command.`);
    }
    

    这样,所有拥有Manage Roles 权限的人(通常是模组及以上)都可以使用该命令。 另外,我会建议你改变

    var warnedone = message.mentions.first()
    

    var warnedone = message.mentions.members.first()
    

    而且,如果您想通过 ID 警告人们,您可以这样做:

    var warnedone = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
    

    让我知道这是否有效:)

    【讨论】:

    • 更好的标志是KICK_MEMBERS
    【解决方案2】:

    给你。

    module.exports = {
        name: 'warn',
        description: "The bot will warn the mentioned user",
        execute(message, args){
            var warnedone = message.mentions.first()
            if(!message.member.roles.cache.find(r => r.id === "712229762742878321")) {
                 return message.reply('can not use this command')
            }
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 2021-10-25
      • 2023-03-03
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2010-12-22
      • 2019-06-16
      • 2021-07-07
      相关资源
      最近更新 更多