如何检查成员是否可以在discord.js v12中被踢/禁?

时间:2020-10-28 09:34:39

标签: node.js discord.js

我创建了一个禁止命令,该命令可以正常工作,除非您尝试踢一个比机器人更高角色的人。有没有简单的方法可以检查用户是否可以被踢?我尝试检查客户端是否具有比该人员更高的角色,但是如果您尝试踢服务器的所有者(如果他没有角色),则会失败。我的代码在这里:

        var User = message.guild.member(message.mentions.members.first()) || message.guild.members.cache.get(args[0]);
        if (!User) return message.reply("please mention the one who should be banned")
        if(User.id === message.author.id) {
            return message.reply('can not allow self-harm')
        };
        if (message.member.roles.highest.comparePositionTo(User.roles.highest) <= 0) {
            return message.reply('you can not ban a user with the same (or higher) permissions as you')
        };
        var banReason = args
        banReason.shift()
        banReason = banReason.toString()
        banReason = banReason.replace(/,/g, ' ')

        if (!banReason) {
            banReason = "No reason provided"
        };
        var banReason2 = `Banned by ${message.author.tag} -` + ' ' + banReason
        User.ban({reason: banReason2}).catch(error => {
            message.channel.send(`Failed to ban <@${User.id}>`)
            return console.log(error)
        })

        message.reply(`you have succesfully banned <@${User.id}> with the reason **${banReason}** <a:excitedwave:758043553573372117>`);

        const embed = new Discord.MessageEmbed()

        .setColor('#FF0000')
        .setTitle(`You have been banned from ${message.guild.name}`)
        .setDescription(`**Banned by:** <@${message.author.id}>\n**Ban reason:** ${banReason}`)
        .setTimestamp()

        User.user.send(embed).catch(error => {
            message.channel.send(`Failed to DM <@${User.id}> the info about this action`)
            console.log(error)
        })

预先感谢

1 个答案:

答案 0 :(得分:2)

您应该尝试使用bannablekickable属性。 如果结果为FALSE,则无法踢或禁止用户。

用法:

mentionnedUser.bannable // Should return TRUE or FALSE
mentionnedUser.kickable // Should return TRUE or FALSE