You need to enable JavaScript to run this app. Discord.py给用户分配Muted角色禁言时报Missing Permissions错误如何解决
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py给用户分配Muted角色禁言时报Missing Permissions错误如何解决

Discord机器人分配Muted角色实现禁言时报403权限错误解决方案

现有功能代码

你编写的禁言功能代码如下:

@bot.command()
async def mute(ctx, member:commands.MemberConverter):
    role = discord.utils.find(lambda r: r.name == 'Muted', ctx.guild.roles)
    await member.edit(roles=[role])

触发的报错信息

运行mute命令时返回的异常如下:

Ignoring exception in command mute:
Traceback (most recent call last):
  File "C:\Users\gathi\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "C:\Users\gathi\Vscode\Discord_Bot\tutorial.py", line 61, in mute
    await member.edit(roles=[role]) # 848410518154117130
  File "C:\Users\gathi\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\member.py", line 681, in edit
    await http.edit_member(guild_id, self.id, reason=reason, **payload)
  File "C:\Users\gathi\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\http.py", line 248, in request
    raise Forbidden(r, 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\gathi\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\gathi\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\gathi\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: Forbidden: 403 Forbidden (error code: 50013): Missing Permissions

排查解决步骤

  • 优先检查角色排序:Discord的角色权限优先级由服务器角色列表的上下位置决定,机器人自身的角色必须拖到Muted角色的上方,才有权限给其他用户分配Muted角色。即使给机器人开了管理员权限,角色位置低于Muted的话依然会触发权限不足报错。
  • 确认操作对象权限:如果要禁言的用户是服务器所有者,或者该用户的角色位置高于机器人的角色位置,机器人无法修改他的角色,自然无法分配禁言身份。
  • 核对机器人权限配置:确认机器人已经开启「管理角色」、「管理成员」两项基础权限,如果是在服务器内单独给机器人授权,不要给机器人设置拒绝相关操作的权限覆盖。
  • 代码逻辑优化:你当前的写法会直接清空被禁言用户的所有原有角色,仅保留Muted角色,如果需求是在原有角色基础上追加禁言身份,将编辑角色的代码修改为await member.edit(roles=member.roles + [role])即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.18 07:06:07