Issue with displaying thumbnail in Discord message using discord.py

280 Views Asked by At

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!

2

There are 2 best solutions below

7
Hazzu On BEST ANSWER

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
)
2
Ethan On

I occasionally run into this error, and what I've found always solves it is using https://imgur.com. I upload my image, publish it to community, then make it hidden. Then I right click the image and click the "copy image link" option that pops up, and add that to my code. I've had no problems since I've started to do this!

As always, make sure to provide credit to assets that are not yours.

Here's an example:

I have a bot that I use as a testing bot before I deploy my code to other bots. The logo for the bot can be found here.

  1. I go to imgur, then click "New Post".
  2. I choose my image, then name it.
  3. I click the "To Community" button under the "Post" option.
  4. I click "Post Publicly", then go to view image in the gallery.
  5. I go to the lower right of the screen, under "img tools", and click "make post hidden".
  6. Once hidden, I right click the image, and select "Copy Image Address".

And then you should have a link that will work!

Edit: Forgot to show how it is implemented in my code. I use cogs, so it may look a little different than yours, but the basic idea is the same.

    @commands.hybrid_command(name="helper", description="Displays the help menu")
    async def helper(self, *, ctx: commands.Context) -> None:
      embed = discord.Embed(
        title = "Auxillium Discord Testing Help",
        description = "*The Help Interface for the ADB Testing Bot*",
        color = discord.Color.green()
      )
      embed.set_thumbnail(url = "https://i.imgur.com/A4sLrSk.png")
      await ctx.send(embed=embed)