我正在制作自己的 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 禁止/踢命令可供所有用户使用.我怎样才能解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!