Avatar命令在用户是角色不一致时出错.py



我正在制作一个avatar命令,该命令有效,但我不知道如何在成员是角色或成员不是成员时出错。谢谢

我的代码:

@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.ioke(ctx(文件"/opt/virtualenvs/python3/lib/python3.8/site packages/discord/ext/commands/core.py";,第855行,调用中等待自我准备(ctx(文件"/opt/virtualenvs/python3/lib/python3.8/site packages/discord/ext/commands/core.py";,第789行,准备中等待自我_parse_arguments(ctx(文件"/opt/virtualenvs/python3/lib/python3.8/site packages/discord/ext/commands/core.py";,第706行,在_parse_arguments中kwargs[name]=等待self.transform(ctx,param(文件"/opt/virtualenvs/python3/lib/python3.8/site packages/discord/ext/commands/core.py";,第552行,在变换中return await self.do_conversion(ctx,converter,argument,param(文件"/opt/virtualenvs/python3/lib/python3.8/site packages/discord/ext/commands/core.py";,do_conversion中的第505行归来等待自己_actual_enversion(ctx,converter,argument,param(文件"/opt/virtualenvs/python3/lib/python3.8/site packages/discord/ext/commands/core.py";,第451行,实际转换ret=等待实例.convert(ctx,参数(文件"/opt/virtualenvs/python3/lib/python3.8/site packages/discord/ext/commands/converter.py";,第195行,转换引发MemberNotFound(参数(discord.ext.commands.errors.MemberNotFound:成员"lt@&903148254051577859>quot;找不到。

您可以执行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!")

最新更新