pythondiscord.pyembedthumbnails

Issue with displaying thumbnail in Discord message using discord.py


I'm currently developing a Discord bot using the discord.py library, and I've encountered an issue with displaying thumbnails in my Discord messages. The code I'm using appears to be correct, but I'm facing difficulties in getting the thumbnail to display as expected. Here's the relevant code:

embedMessage = discord.Embed(
    title=entry.title,
    description=entry.description,
    url=entry.link,
    color=self.color,
)

url_thumbnail = entry.links[1]["href"]
embedMessage.set_thumbnail(url=url_thumbnail)

# Converting the original date into a datetime object
date_obj = datetime.strptime(
    entry.published, "%a, %d %b %Y %H:%M:%S %z"
)

# Formatting the resulting date
date_format = date_obj.strftime("%a, %d %b %Y %H:%M")

embedMessage.set_footer(text=date_format)

await channel.send(
    content="🚀 New Post on PSTHC 📰", embed=embedMessage
)

This is the result I'm currently getting: enter image description here

I've confirmed that the thumbnail URL is valid, the bot has the necessary permissions to embed links and attach files, and the thumbnail URL is accessible. However, the thumbnail doesn't appear in the Discord message.

Here's an example of a thumbnail URL I'm using: https://www.staticbo.psthc.fr/wp-content/uploads/2023/11/Logo-PGW23.png

Here's the Embed object just before it's used in channel.send(): enter image description here

As you can see, the url is correctly defined and I can click on the link to access the image, but the image is not displayed in the message on discord.

The bot has the following permissions: enter image description here

Do you have any insights into what might be causing this issue, or do you have any suggestions on how to resolve it? Any assistance would be greatly appreciated.

Thank you in advance!


Solution

  • I also tried creating an embed with this image link as a thumbnail and it didn't work. I believe the error is that Discord is unable to correctly render images from this domain. An alternative to get around this problem would be to download the image and send it as a file:

    import discord
    from aiohttp import ClientSession
    from io import BytesIO
    
    
    embed_message = discord.Embed(
        title=entry.title,
        description=entry.description,
        url=entry.link,
        color=self.color,
    )
    
    url_thumbnail = entry.links[1]["href"]
    
    # download the image and save in a bytes object
    async with ClientSession() as session:
        async with session.get(url_thumbnail) as resp:
            thumb_image = BytesIO(await resp.read())
    
    # creating a discord file
    thumb_file = discord.File(fp=thumb_image, filename="thumb.png")
    
    # setting the file as thumbnail
    embed_message.set_thumbnail(url=f"attachment://{thumb_file.filename}")
    
    date_obj = datetime.strptime(entry.published, "%a, %d %b %Y %H:%M:%S %z")
    date_format = date_obj.strftime("%a, %d %b %Y %H:%M")
    embed_message.set_footer(text=date_format)
    
    # sending the embed along with the file
    await channel.send(
        content="🚀 New Post on PSTHC 📰",
        embed=embed_message,
        file=thumb_file
    )