如何禁用踢高角色的成员?

时间:2021-04-13 08:48:39

标签: javascript discord.js

所以我目前正在创建一个 kick command 并且遇到了一个问题:

所有成员都可以踢其他成员,即使他们的角色更高,例如"normal" 用户可以踢 adminmod。但我希望机器人返回一个 message 喜欢

<块引用>

“您不能踢出具有同等或更高角色的成员!”

这是我当前的代码:

const { MessageEmbed, User } = require('discord.js');
const config = require('../../configs/config.json');

module.exports = {
    config: {
        name: "kick",
    },
    run: async (client, message, args) => {
        if (!message.member.hasPermission('KICK_MEMBERS', 'ADMINISTRATOR')) {
            return message.channel.send({
                embed: {
                    title: 'You are unable to kick members',
                    color: config.embedcolor
                }
            })
        }
        if (!args[0]) {
            return message.channel.send({
                embed: {
                    title: 'PLease mention a user',
                    color: config.embedcolor
                }
            })
        }
        if (message.member.permissions.has("MANAGE-MESSAGES", "ADMINISTRATOR"))
            return message.channel.send({
                embed: {
                    title: "I can't kick staff members!",
                    color: config.embedcolor
                }
            })

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

        try {
            await member.kick();
            await message.channel.send({
                embed: {
                    title: `${member} has been kicked!`,
                    color: config.embedcolor
                }
            })
        } catch (e) {
            return message.channel.send({
                embed: {
                    title: `User isn't in this server!`,
                    color: config.embedcolor
                }
            })
        }

    }
}

1 个答案:

答案 0 :(得分:0)

试试这个方法:

if(message.member.roles.highest.comparePositionTo(message.mentions.members.first().roles.highest) > 0){
    //member has higher role then first mentioned member
    if(member.kickable){  //Just in case
        message.mentions.members.first().kick();
    }
}else{
    message.reply("You can't kick your senpai °^°");
}

编辑:在最新的 discord.js 版本中,highestRole 已被替换为 roles.highest