在他们所在的所有服务器中取消静音成员

时间:2021-04-18 00:10:36

标签: python discord.py

我想发出取消静音命令,但不仅仅是在 1 个服务器中,而是在成员所在的所有服务器中。

我的代码不起作用:

async def unmuteAterx(ctx):
  Aterx = 549328259331129364

  if ctx.message.author.id == Aterx:
    for guild in client.guilds:
      member = guild.get_member(549328259331129364)
  
      if member is not None:
        role = discord.utils.get(guild.roles, name='Muted')

        await member.remove_roles(role)

(我将它设置为只有我 rn 因为我正在测试它)

出现错误:Command raised exception: AttributeError: 'NoneType' object has no attribute 'id'

完全错误:

Ignoring exception in command unmuteAterx:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 477, in unmuteAterx
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/member.py", line 720, in remove_roles
    await req(guild_id, user_id, role.id, reason=reason)
AttributeError: 'NoneType' object has no attribute 'id'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/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: AttributeError: 'NoneType' object has no attribute 'id'

1 个答案:

答案 0 :(得分:0)

正如@Weylyn 指出的那样,发生这种情况是因为机器人所在的所有服务器上都不存在名为“静音”的角色。 在这种情况下,discord.utils.get(guild.roles, name='Muted') 将返回 None,因此您可以在尝试删除角色之前进行检查。

此外,这段代码并没有删除成员在所有他的服务器上的静音角色,而是在机器人所在的所有服务器