問題描述
我正在制作自己的 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模板網!