Bot无权执行KICK / BAN命令

时间:2020-07-23 20:42:25

标签: javascript discord bots discord.js

所以我一直在学习js和discord.js,所以我正在创建我的机器人。由于权限问题,我似乎对KICK / BAN命令有问题。现在,当我邀请bot进入服务器时,我已授予他所有权限,并且还将他的角色在服务器的角色层次结构中上移了。怎么了?

这是代码:

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)

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以捕获错误并发送一条消息,使用户知道该人无法被踢:

//...
member.kick(reason)
  .then(() => {
    //...
  })
  .catch(err => {
    //...
  });

您还可以检查用户是否是管理员:

if (message.guild.member(member).hasPermission("ADMINISTRATOR")) {
  //...
}