Discord py MemberNotFound 异常

Discord py MemberNotFound exception

@commands.command()
async def mute(self, ctx, member : discord.Member = None):
  try:
    if isinstance(member, discord.Member):
      await member.edit(mute=True)

    elif member is None:  #setting the command to mute selected member        
    member=get(ctx.guild.members, id=Charles_id) 
      await member.edit(mute=True)        

  except discord.HTTPException:
    await ctx.channel.send(f"User {member.mention} isn't connected to voice channel")
  except discord.ext.commands.errors.MemberNotFound("@everyone"):
    vc = ctx.author.voice.channel
    for user in vc.members:  
      await user.edit(mute=True)

所以我正在尝试发出一个命令,使提到的播放器静音。默认情况下,它应该使我们的静音 朋友,查尔斯。唯一的问题是默认角色 (@Everyone)。我挣扎了很多但终于 我设法想出了一个可行的想法。可悲的是,它并没有真正起作用。计划是尝试让会员和 如果成员是@everyone,它将 运行 一个错误,该错误将触发 except 并最终完成它的工作。 我对编程还很陌生,所以我希望得到一个有用的教训,干杯!

回溯如下:

Ignoring exception in command ryj:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 903, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 851, in invoke
    await self.prepare(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 786, in prepare
    await self._parse_arguments(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 697, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 552, in transform
    return await self.do_conversion(ctx, converter, argument, param)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 505, in do_conversion
    return await self._actual_conversion(ctx, converter, argument, param)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 451, in _actual_conversion
    ret = await instance.convert(ctx, argument)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/converter.py", line 191, in convert
    raise MemberNotFound(argument)
discord.ext.commands.errors.MemberNotFound: Member "@​everyone" not found.

我认为这个错误不言自明。 @everyone不是会员,无法转换。

另一种选择是手动转换它:

async def mute(self, ctx, *, member_string: str):
    if member_string == "@everyone":
        return await ctx.send("You can't mention everyone")
 
    # Converting the string to a `discord.Member` obj
    member = commands.MemberConverter.convert(ctx, member_string)
    if member.voice is None:
        return await ctx.send("Member isn't in a voicec channel") # Exiting if the user isn't connected to any vs channel

    await member.edit(mute=True)

参考: