【发布时间】:2020-11-13 16:27:26
【问题描述】:
所以我一直在学习一些 js 和 discord.js,所以我正在创建我的机器人。由于权限,我似乎对 KICK/BAN 命令有疑问。现在,当我邀请机器人到服务器时,我已经给了他所有权限,并且我还在服务器的角色层次结构中将他的角色向上移动。会有什么问题?
这是代码:
if (message.member.hasPermission(['KICK_MEMBERS', 'BAN_MEMBERS'])) {
if(message.content.startsWith(prefix + 'ban')) {
let member = message.mentions.members.first();
if (member)
member.ban().then((member) => {
message.channel.send(":name_badge: " + member.displayName + " has been banned!")})
else
message.channel.send("Make sure to @mention who you want to be banned!");
}
}
这是错误:
UnhandledPromiseRejectionWarning: DiscordAPIError: Missing Permissions
at RequestHandler.execute (D:\DiscordBot\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:7468) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either
by throwing inside of an async function without a catch block, or by rejecting a promise which was
not handled with .catch(). To terminate the node process on unhandled promise rejection,
use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)
有什么想法吗?
【问题讨论】:
-
这取决于您要禁止谁。如果您尝试禁止的用户是管理员或服务器所有者,那么它将无法正常工作,并且您会收到与此处一样的错误。
-
是的!但我怎么能检查呢?显然,我尝试这个的人不是管理员或所有者,但在层次结构角色中,他是最高的,但没有 BAN 或 KICK 权限.. 嗯
-
您的机器人是管理员吗?还是它只有禁止/踢的权限?如果它是管理员,那么只要您尝试踢出的用户不是管理员或所有者,它在层次结构角色中的位置就无关紧要。但是,如果它不是管理员并且只有所需的权限,那么它的角色将需要高于它试图踢的任何人。我不相信有任何方法可以检查机器人是否能够踢用户,您只需在您的承诺链中添加一个 catch() 来禁止和处理它无法踢他们的情况在那里。
标签: javascript discord bots discord.js