用户权限检查会引发错误,但我不知道为什么

时间:2020-08-20 18:58:42

标签: discord.py

我已经尝试了好几天没有运气了。我无法使它正常工作,所以我必须在这里询问。

如何使用@commands.has_permissions?我已经尝试了几件事。

这是我的代码。

@client.command(name="role")
@commands.has_permissions(manage_roles=True)
async def addrole(ctx, user: discord.Member, role: discord.Role):
    await user.add_roles(role)
    await ctx.send(f"I gave {user.name} the role {role.name}")

但是我一直收到此错误。

Ignoring exception in command role:
Traceback (most recent call last):
  File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:/Users/unkno/Desktop/Code_Tests/Python/discord_bot/bot2.py", line 17, in addrole
    await user.add_roles(role)
  File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\member.py", line 641, in add_roles        
    await req(guild_id, user_id, role.id, reason=reason)
  File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\http.py", line 241, 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\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\lib\site-packages\discord\ext\commands\core.py", line 855, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:\Users\unkno\AppData\Local\Programs\Python\Python38\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 

请帮助。 预先感谢!

1 个答案:

答案 0 :(得分:0)

获得异常的原因是因为漫游器缺少Manage Roles权限。

您已在此处设置

@commands.has_permissions(manage_roles=True)

要解决此问题,只需向您的漫游器授予权限即可。您还可以通过将异常添加到代码中来处理该异常:

@addrole.error
async def addrole_error(ctx, error):
    if isinstance(error, commands.errors.ErrorNameHere):
        await ctx.send("You don't have permission to execute this command!")

您只需用错误名称替换ErrorNameHere