【发布时间】: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