且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Discord.js 禁止/踢命令可供所有用户使用.我怎样才能解决这个问题?

更新时间:2021-11-11 00:29:24

KICK_MEMBERS"权限告诉您他们是否有权踢成员,因此得名.

The "KICK_MEMBERS" permission tells you if they have the permission to kick members, hence the name.

BAN_MEMBERS"权限告诉您他们是否有权禁止成员,因此名称.

The "BAN_MEMBERS" permission tells you if they have the permission to ban members, hence the name.

你的踢腿命令:

if (msg.member.hasPermission("KICK_MEMBERS")) {
    if (msg.members.mentions.first()) {
        try {
            msg.members.mentions.first().kick();
        } catch {
            msg.reply("I do not have permissions to kick " + msg.members.mentions.first());
        }
    } else {
        msg.reply("You do not have permissions to kick " + msg.members.mentions.first());
    }
}

你的禁令命令:

if (msg.member.hasPermission("BAN_MEMBERS")) {
    if (msg.members.mentions.first()) {
        try {
            msg.members.mentions.first().ban();
        } catch {
            msg.reply("I do not have permissions to ban" + msg.members.mentions.first());
        }
    } else {
        msg.reply("You do not have permissions to ban" + msg.members.mentions.first());
    }
}

trycatch 的原因确保如果机器人没有权限踢或禁止该用户,它不会导致错误.

The reason for the try and catch ensures that if the bot does not have permissions to kick or ban that user, it will not cause an error.

另一个说明:

您不必创建另一个 bot.on('message') 事件.相反,只需使用 elseif

You do not have to create another bot.on('message') event. Instead just use an elseif