Discord.js ban/kick commands available to all users. How can I fix this?(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 禁止/踢命令可供所有用户使用.我怎样才能解决这个问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Discord.js 禁止/踢命令可供所有用户使用.我怎样才


- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01