不和谐.py |如何为某人分配角色?

时间:2021-05-30 06:05:41

标签: python python-3.x discord.py

我正在尝试使用角色 Muted 将某人静音 代码:

@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])

但我收到了这个奇怪的错误:

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

我不明白该怎么做。

这是机器人的权限: The bots perms

2 个答案:

答案 0 :(得分:0)

答案在 bot 中 Forbidden: 403 Forbidden (error code: 50013): Missing Permissions 应该立即告诉您这是一个权限问题。

机器人在 await member.edit(roles=[role]) # 848410518154117130 处失败,告诉您代码有效,但无法分配角色。

确保机器人:

  • 在角色层次上比你高
  • 拥有 Manage RolesAdministrator 权限

如果这仍然不起作用,请考虑转到我之前关于意图的answer

答案 1 :(得分:-1)

您可以使用 discord.Member.add_roles 来分配角色。

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

这是 unmute 命令。

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