使用Python的Discord库时如何解决403权限错误?
Discord Bot 修改昵称时出现403权限错误的解决方法
问题重现
执行以下Python代码时遇到权限错误:
import discord from discord.ext import commands intents = discord.Intents.default() intents.members = True intents.message_content = True peoples = [ {'Name': 'Artem', 'id': 'steamid:1234'}, {'Name': 'Sergey', 'id': 'steamid:1434'}, {'Name': 'Bogdan', 'id': 'steamid:1534'} ] bot = commands.Bot(command_prefix='/', intents=intents) @bot.event async def on_ready(): print(f'We have logged in as {bot.user.name}') @bot.command() async def accept(ctx, value: str): id_v = None print(value) print(f"Command 'accept' invoked by {ctx.author.name} ({ctx.author.id}) with argument {value}") for i in peoples: print(i['id']) if i['id'] == value: id_v = i print(id_v['id']) break if id_v is not None: new_nickname = str(id_v['Name']) print(new_nickname) await ctx.author.edit(nick=new_nickname) await ctx.send(f"{new_nickname} принято ✅") print(f"Nickname updated for {ctx.author.name}") else: await ctx.send(f"Значение {value} не найдено в списке.") # Replace 'YOUR_BOT_TOKEN' with your actual bot token token = '' # Run the bot bot.run(token)
报错信息:
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权限系统中,机器人无法修改角色层级高于自己的用户昵称,哪怕有管理员权限也不行。
- 目标用户是服务器所有者:Discord限制机器人修改服务器创建者的昵称,这是平台级别的限制,无法通过权限设置绕过。
- 权限被单独取消:即使角色拥有管理员权限,也可能被手动取消了「修改昵称(Manage Nicknames)」的具体权限。
解决步骤
- 调整机器人角色位置:进入服务器设置 → 角色,将机器人的角色拖动到角色列表的上层,确保其层级高于需要修改昵称的用户的角色。
- 确认目标用户身份:如果执行命令的是服务器所有者,机器人无法修改其昵称,这是正常限制。
- 验证具体权限:在服务器角色设置中,检查机器人角色的权限,确保「修改昵称」权限处于启用状态(即使是管理员角色,也可能被单独关闭该权限)。
代码优化(可选)
可以在代码中添加错误捕获,针对不同场景给出明确提示,避免模糊的报错:
@bot.command() async def accept(ctx, value: str): id_v = None print(value) print(f"Command 'accept' invoked by {ctx.author.name} ({ctx.author.id}) with argument {value}") for i in peoples: print(i['id']) if i['id'] == value: id_v = i print(id_v['id']) break if id_v is not None: new_nickname = str(id_v['Name']) print(new_nickname) try: await ctx.author.edit(nick=new_nickname) await ctx.send(f"{new_nickname} принято ✅") print(f"Nickname updated for {ctx.author.name}") except discord.Forbidden: if ctx.author == ctx.guild.owner: await ctx.send("无法修改服务器所有者的昵称") else: await ctx.send("权限不足,无法修改该用户昵称,请检查机器人角色位置和权限") else: await ctx.send(f"Значение {value} не найдено в списке.")
内容的提问来源于stack exchange,提问作者bigclaw
相关产品推荐
相关产品推荐

