使用成员默认头像的缩略图设置嵌入不起作用

Setting an embed with a thumbnail of a member's default avatar not working

当我使用 pycord 创建嵌入并将缩略图设置为实际个人资料图片时,该功能工作正常,但当它设置为默认的 discord 个人资料图片时,它会抛出此错误:

Ignoring exception in command checktime:
Traceback (most recent call last):
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\commands\core.py", line 113, in wrapped
    ret = await coro(arg)
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\commands\core.py", line 766, in _invoke
    await self.callback(ctx, **kwargs)
  File "C:\Users\Owner\Desktop\Coding\DiscordBots\Time_Bot_Discord.py", line 63, in checktime
    await ctx.respond(embed=embed)
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\interactions.py", line 643, in send_message
    await adapter.create_interaction_response(
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\webhook\async_.py", line 213, in request
    raise HTTPException(response, data)
discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In data.embeds.0.thumbnail.url: Scheme "none" is not supported. Scheme must be one of ('http', 'https').

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\bot.py", line 755, in process_application_commands
    await ctx.command.invoke(ctx)
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\commands\core.py", line 312, in invoke
    await injected(ctx)
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\commands\core.py", line 119, in wrapped
    raise ApplicationCommandInvokeError(exc) from exc
discord.commands.errors.ApplicationCommandInvokeError: Application Command raised an exception: HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In data.embeds.0.thumbnail.url: Scheme "none" is not supported. Scheme must be one of ('http', 'https').

函数代码:

@bot.slash_command(name='checktime', description='Check the time that someone is online')
async def checktime(ctx, member: discord.Option(discord.Member, "Enter someone", required=True)):
    global act_list
    print(member) # debug
    if not await cooldown(ctx, ctx.author, 4, 1):
        return
    embed = discord.Embed(title=f"Checking time of... {member.display_name}",
                          description=f"Percent of time: {((sum(act_list[member][3], timedelta())).total_seconds()) / ((datetime.now() - start_time).total_seconds()) * 100: .2f}%")
    try:
        embed.set_thumbnail(url=member.avatar)
    except:
        embed.remove_thumbnail()
        embed.set_thumbnail(url=member.default_avatar)
    embed.add_field(name="Start Time", value=start_time.strftime("%H:%M:%S" + " UTC" + " at %Y-%m-%d"))
    embed.set_footer(text="Note: Bot is in experimental phase, and times may reset during the day.\nAll data is not fully accurate.")


    await ctx.respond(embed=embed)
    act_list[ctx.author][2][1] = datetime.now()

我该如何解决这个问题?

这个错误意味着他们需要一个 URL 像 https//mydomain.com/mypicture.png 作为嵌入缩略图 - 这可以通过使用 member.avatar.url

除此之外,会员头像可以是Nonemember没有自定义头像。所以你应该检查 display_avatar.

您应该将 try-catch 替换为:

embed.set_thumbnail(url=str(member.display_avatar.url))

简单、容易和 one-liner。


来源