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

Discord机器人报403 Forbidden(50013)错误:缺失权限求助

Discord机器人修改昵称提示权限缺失问题

问题描述

运行Discord机器人时触发如下错误:

Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\core.py", line 229, in wrapped
    ret = await coro(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\PC\OneDrive\Namizje\VS CODE\discord-bot\cemess.py", line 22, in reset
    await member.edit(nick=None) # 重置其昵称
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\PC\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\member.py", line 880, in edit
    data = await http.edit_member(guild_id, self.id, reason=reason, **payload)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\PC\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\http.py", line 738, in request
    raise Forbidden(response, data)
discord.errors.Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\PC\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\bot.py", line 1349, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\PC\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\core.py", line 1023, in invoke
    await injected(*ctx.args, **ctx.kwargs)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\PC\AppData\Local\Programs\Python\Python311\Lib\site-packages\discord\ext\commands\core.py", line 238, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

尝试重置服务器成员昵称,已给机器人分配管理员权限并将其角色移至角色列表顶部,本人是服务器所有者,但仍提示权限缺失。

初始代码(已隐藏TOKEN):

import discord.ext
import discord
from discord.ext import commands

intents = discord.Intents.all()

bot = commands.Bot(command_prefix='#', intents = intents)

@bot.event
async def on_ready():
    print(f'We have logged in as {bot.user}')

@bot.command() # 命令装饰器
@commands.has_permissions(change_nickname=True) # 检查修改昵称权限
async def reset(ctx):
    for member in ctx.guild.members: # 遍历服务器内所有成员
        await member.edit(nick=None) # 重置其昵称

bot.run(TOKEN)

补充尝试的代码:

import discord
import asyncio
import discord.ext
from discord.ext import commands

intents = discord.Intents.all()
bot = commands.Bot(command_prefix='!', intents = intents)

@bot.event
async def on_ready():
    print(f'{bot.user} has connected to Discord!')

@bot.command()
@commands.has_permissions(change_nickname=True)
async def reset(ctx):
    role = discord.utils.get(ctx.guild.roles, name='Maks')

    for user in ctx.guild.members:
        if role not in user.roles:
            continue
        await user.edit(nick=None)

其中"Maks"角色已赋予所有管理员权限用户,但错误依旧。

问题排查与解决方案

  • 核心原因:无法修改特定成员的昵称
    Discord规则下,机器人无法修改两类成员的昵称:

    1. 服务器所有者
    2. 角色层级高于或等于机器人自身角色的成员
      你的遍历逻辑会包含这些成员,直接触发权限错误。
  • 修正代码:过滤无权限操作的成员
    在执行昵称修改前添加判断逻辑,跳过无法操作的成员:

    @bot.command()
    @commands.has_permissions(manage_nicknames=True) # 修正权限检查参数
    async def reset(ctx):
        for member in ctx.guild.members:
            # 跳过服务器所有者
            if member == ctx.guild.owner:
                continue
            # 跳过角色层级不低于机器人的成员
            if member.top_role >= ctx.guild.me.top_role:
                continue
            await member.edit(nick=None)
    
  • 权限参数修正
    机器人修改他人昵称需要的是manage_nicknames(管理昵称)权限,而非change_nickname(修改自身昵称)。管理员权限已包含manage_nicknames,但需要修正代码中的权限检查装饰器参数。

  • 额外确认点
    确保机器人的角色确实在所有需要修改的成员的角色之上,包括拥有"Maks"角色的成员,避免其角色层级高于机器人导致操作失败。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 11:25:19