Search code examples
pythondiscordpycord

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


When I create an embed using pycord and set the thumbnail to an actual profile picture, the function works fine, but when it is set to a default discord profile picture, it throws this error:

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').

Function code:

@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()

How do I fix this?


Solution

  • This error means that they need a URL like https//mydomain.com/mypicture.png for the embed thumbnail - which is possible by using member.avatar.url

    Aside from that, the member avatar can be None of the member has no custom avatar. So you should check the display_avatar.

    You should replace the try-catch with this:

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

    Simple, Easy and a one-liner.


    Sources