且构网

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

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

更新时间:2021-08-06 00:54:55

"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());
    }
}

try catch 的原因可确保,如果漫游器没有踢踢或禁止该用户的权限,则不会导致错误.

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