【问题标题】:Avatar command give error when the user is a role discord.py当用户是角色 discord.py 时,头像命令给出错误
【发布时间】:2021-12-19 04:19:15
【问题描述】:

我正在创建一个头像命令,该命令有效,但我不知道如何在成员是角色或成员不是成员时给出错误。谢谢。

我的代码:

@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 行 等待 self.prepare(ctx) 准备中的文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py”,第 789 行 等待 self._parse_arguments(ctx) _parse_arguments 中的文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py”,第 706 行 kwargs[name] = await self.transform(ctx, param) 文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py”,第 552 行,在转换中 返回等待 self.do_conversion(ctx, 转换器, 参数, 参数) 文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py”,第 505 行,在 do_conversion 返回等待 self._actual_conversion(ctx, 转换器, 参数, 参数) _actual_conversion 中的文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py”,第 451 行 ret = await instance.convert(ctx, 参数) 文件“/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/converter.py”,第 195 行,转换 raise MemberNotFound(参数) discord.ext.commands.errors.MemberNotFound:找不到成员“”。

【问题讨论】:

  • 确保这是正确的角色 ID

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


【解决方案1】:

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

@client.command(aliases=["av","useravatar","usericon","userav","icon"])
async def avatar(ctx, *,  avamember : discord.Member=None):
  if avamember in ctx.guild:
    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)
  else:
      return await ctx.send("That member is not valid!")

【讨论】:

  • 我在命令上加上if语句,只是让命令只能用于命令的作者,不能用于查看他人头像。
  • @MenIndo 我编辑了我的答案以使其工作。
猜你喜欢
  • 2021-10-03
  • 2019-05-31
  • 1970-01-01
  • 2021-07-06
  • 2021-02-07
  • 1970-01-01
  • 2020-12-21
  • 2020-10-16
  • 2020-12-30
相关资源
最近更新 更多