You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Discord.js Ban命令时出现Missing Permissions错误的求助

Discord.js Ban命令权限错误(DiscordAPIError[50013]: Missing Permissions)解决办法

问题背景

开发Discord.js的ban命令时,点击确认封禁按钮后触发权限错误DiscordAPIError[50013]: Missing Permissions。已为机器人分配管理员权限,调整interactionCreate事件文件、添加GatewayIntentBits.AutoModerationExecution意图后问题仍存在,ESLint未检测到代码语法问题。

核心解决方案

1. 检查机器人角色层级优先级

机器人的角色必须高于目标用户的角色层级。即使机器人拥有管理员权限,若目标用户的角色在角色列表中排在机器人角色上方,Discord会限制机器人执行封禁操作。

  • 进入服务器设置 → 角色面板
  • 将机器人的角色拖动到所有需要封禁的用户角色上方

2. 修正目标用户的获取方式

原代码使用getMentionable获取目标,该方法可能返回角色对象(而非用户对象),导致ban()调用失败:

// 原错误代码
const targetuser = interaction.options.getMentionable('target-user', true);

修改为仅获取用户对象,并同步更新命令定义:

// 命令定义部分替换addMentionableOption为addUserOption
data: new SlashCommandBuilder()
    .setName('ban')
    .setDescription('Bans a member')
    .addUserOption(option =>
        option
            .setName('target-user')
            .setDescription('The user you want to ban.')
            .setRequired(true))
// ...其余代码不变

// 执行部分替换获取targetuser的代码
const targetuser = interaction.options.getUser('target-user', true);

3. 提前验证机器人与用户的权限

在执行封禁操作前,添加权限校验逻辑,提前排查权限问题:

if (confirmation.customId === 'confirm') {
    // 验证机器人自身是否有封禁权限
    if (!interaction.guild.members.me.permissions.has(PermissionFlagsBits.BanMembers)) {
        return confirmation.reply({content: "我没有封禁成员的权限!", ephemeral: true});
    }
    // 验证发起命令的用户是否有封禁权限
    if (!interaction.member.permissions.has(PermissionFlagsBits.BanMembers)) {
        return confirmation.reply({content: "你没有封禁成员的权限!", ephemeral: true});
    }
    // 后续封禁逻辑...
}

4. 移除无关意图

GatewayIntentBits.AutoModerationExecution与ban命令功能无关,无需添加,可从index.js中移除该意图配置。

5. 验证频道发送权限(可选)

若封禁后发送嵌入消息的步骤触发错误,需确认机器人在当前频道拥有「发送消息」和「嵌入链接」权限。

内容的提问来源于stack exchange,提问作者darkmenacing

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 08:43:10