Python 在其所在的所有服务器中取消禁用成员
我想发出一个Unmute命令,但不是只在一台服务器上,而是在一个成员所在的所有服务器上 我的代码不起作用:Python 在其所在的所有服务器中取消禁用成员,python,discord.py,Python,Discord.py,我想发出一个Unmute命令,但不是只在一台服务器上,而是在一个成员所在的所有服务器上 我的代码不起作用: async def unmuteAterx(ctx): Aterx = 549328259331129364 if ctx.message.author.id == Aterx: for guild in client.guilds: member = guild.get_member(549328259331129364) if membe
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,因为我正在测试它)
我收到一个错误:命令引发异常:AttributeError:“NoneType”对象没有属性“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'
正如@Weylyn所指出的,之所以会出现这种情况,是因为bot所在的所有服务器上都不存在名为“mute”的角色。 在这种情况下,
discord.utils.get(guild.roles,name='Muted')
将返回None
,因此您可以在尝试删除角色之前进行检查
此外,这段代码不会删除所有他的服务器上的成员的静音角色,但在机器人所在的所有服务器上,始终将完整的错误消息(从单词“Traceback”开始)作为文本(不是屏幕截图,也不是指向外部门户的链接)放在问题中(不是注释)。还有其他有用的信息。如果您在
ctx.message.author.id
中得到错误,那么您应该首先检查如果ctx.message.author不是None
我没有在ctx.message.author.id
中得到错误,因为我在其他命令中使用过它,并且它工作正常。您没有显示完整的错误消息,所以我看不到您从哪里得到这个错误错误-我在您的代码中只看到一个.id
,因此我假设您是通过ctx.message.author.id
获得它的。但是,即使它在其他命令中工作,它也不必在这个命令中工作-您应该首先检查print(ctx.message.author)
来检查它。但是,如果错误显示您在不同的位置有不同的.id
问题,那么您必须在不同的位置进行检查。您会收到错误,因为在某些行会中没有角色名称“静音”。