Discord.js禁止/踢命令可用于所有用户.我怎样才能解决这个问题? [英] Discord.js ban/kick commands available to all users. How can I fix this?

查看:55
本文介绍了Discord.js禁止/踢命令可用于所有用户.我怎样才能解决这个问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作自己的Discord Bot,因为我不信任较大的机器人(Dyno,Hime,NosoBot等),而我的机器人已经完成了很多工作.唯一的问题是我的代码允许所有成员使用这些命令.我只希望人们能够使用他们拥有权限的功能.该代码有效,但是如何使它只允许具有踢脚/禁止权限的人使用?

I'm making my own Discord Bot because I don't trust the bigger ones (Dyno, Hime, NosoBot, etc.) And my bot is pretty much done. The only problem is that my code allows all members to use these commands. I only want people to be able to use the functions they have permissions to. The code works, but how can I make it allow only people with permission to kick/ban?

if (msg.content.startsWith("$kick ")) {
    if (msg.mentions.members.first()) {
        msg.mentions.members.first.kick().then((member) => {
            msg.channel.send(":wave: " + member.displayName + " has been successfully kicked :point_right: ");
        }).catch(() => {
            msg.channel.send("I do not have permissions to do this");
        });
    }
}else if (msg.content.startsWith("$ban ")) {
   if (!message.member.hasPermission("MANAGE_MESSAGES")) return;
    if (msg.mentions.members.first()) {
        msg.mentions.members.first.ban().then((member) => {
            msg.channel.send(":wave: " + member.displayName + " has been successfully banned :point_right: ");
        }).catch(() => {
            msg.channel.send("I do not have permissions to do this");
        });
    }
}

推荐答案

"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

这篇关于Discord.js禁止/踢命令可用于所有用户.我怎样才能解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆