【问题标题】:Discord.py | How to assign someone a role?不和谐.py |如何为某人分配角色?
【发布时间】:2021-05-31 00:19:37
【问题描述】:

我正在尝试使用角色 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

【问题讨论】:

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


    【解决方案1】:

    答案在机器人内Forbidden: 403 Forbidden (error code: 50013): Missing Permissions 应该会立即告诉您这是权限问题。

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

    确保机器人:

    • 在角色层次结构中高于您
    • 拥有Manage RolesAdministrator 权限

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

    【讨论】:

    • 我确实检查过,但我会再检查一次
    【解决方案2】:

    您可以使用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)
    

    【讨论】:

      猜你喜欢
      • 2018-08-11
      • 2021-01-06
      • 2022-10-24
      • 2022-09-27
      • 2021-05-15
      • 1970-01-01
      • 2021-05-31
      • 2021-10-19
      • 1970-01-01
      相关资源
      最近更新 更多