繁体   English   中英

当用户是角色 discord.py 时,头像命令会出错

[英]Avatar command give error when the user is a role discord.py

我正在制作头像命令,该命令有效,但如果成员是角色或仅当成员不是成员时,我无法找出如何给出错误的方法。 谢谢。

我的代码:

@client.command(aliases=["av","useravatar","usericon","userav","icon"])
async def avatar(ctx, *,  avamember : discord.Member=None):
  try:
    if avamember == None:
      Author = ctx.author
      userAvatarUrl = Author.avatar_url
      stringUrl = str(userAvatarUrl)

      png = stringUrl.replace("webp","png")
      jpg = stringUrl.replace("webp","jpg")

      embed = discord.Embed(title = f"{Author}'s avatar.",
      description = f"**Links :** \n[Default]({userAvatarUrl}) \n[PNG]({png}) \n[JPG]({jpg})",
      color = 0xf461ff)

      embed.set_image(url=userAvatarUrl)
 
      now = datetime.now()
      current_time = now.strftime("%H:%M")

      embed.set_footer(text=f"Requested by  {ctx.author} at {current_time}")
      await ctx.reply(embed=embed, mention_author=False)
    else:
      userAvatarUrl = avamember.avatar_url
      stringUrl = str(userAvatarUrl)
      png = stringUrl.replace("webp","png")
      jpg = stringUrl.replace("webp","jpg")

      embed = discord.Embed(title = f"{avamember}'s avatar.",
      description = f"**Links :** \n[Default]({userAvatarUrl}) \n[PNG]({png}) \n[JPG]({jpg})",
      color = 0xf461ff)

      embed.set_image(url=userAvatarUrl)

      now = datetime.now()
      current_time = now.strftime("%H:%M")

      embed.set_footer(text=f"Requested by  {ctx.author} at {current_time}")
      await ctx.reply(embed=embed,mention_author=False)
  except:
    embed = discord.Embed(title = f"ERROR!",
    description = f"An error acurred, please try again.",
    color = 0xf461ff)

    embed.set_image(url=userAvatarUrl)

    now = datetime.now()
    current_time = now.strftime("%H:%M")

    embed.set_footer(text=f"Requested by  {ctx.author} at {current_time}")
    await ctx.reply(embed=embed,mention_author=False)

我得到的错误:

忽略命令头像中的异常:回溯(最近一次调用最后一次):文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py”,第 939 行,调用等待ctx.command.invoke(ctx) 文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py”,第 855 行,调用 await self.prepare(ctx)文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py”,第 789 行,准备等待 self._parse_arguments(ctx) 文件“/opt/virtualenvs/python3 /lib/python3.8/site-packages/discord/ext/commands/core.py", line 706, in _parse_arguments kwargs[name] = 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, conve rter, argument, param) 文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py”,第 451 行,在 _actual_conversion ret = await instance.convert(ctx,参数)文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/converter.py”,第195行,在转换中引发MemberNotFound(参数)discord.ext.commands.errors。 MemberNotFound:未找到成员“<@&903148254051577859>”。

您可以执行if语句来检查成员是否在服务器中。

if avamember in ctx.guild: #checks if the member is in guild
    #do whatever
else: #if the member is not in the guild
    return await ctx.send("That member is not valid!") #sends an error message

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM