使用discord.py在Discord消息中显示缩略图的问题

问题描述 投票:0回答:1

我目前正在使用discord.py 库开发Discord 机器人,并且在Discord 消息中显示缩略图时遇到了问题。我使用的代码似乎是正确的,但我在使缩略图按预期显示方面遇到困难。这是相关代码:

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
)

这是我目前得到的结果:

我已确认缩略图 URL 有效,机器人具有嵌入链接和附加文件的必要权限,并且缩略图 URL 可访问。但是,缩略图不会出现在 Discord 消息中。

这是我正在使用的缩略图 URL 的示例:https://www.staticbo.psthc.fr/wp-content/uploads/2023/11/Logo-PGW23.png

这是在

channel.send()
中使用之前的嵌入对象:

如您所见,网址已正确定义,我可以单击链接来访问图像,但图像未显示在不和谐的消息中。

机器人具有以下权限:

您对可能导致此问题的原因有任何见解吗?或者您对如何解决该问题有任何建议吗?任何帮助将不胜感激。

提前谢谢您!

python discord.py embed thumbnails
1个回答
0
投票

我还尝试使用此图像链接作为缩略图创建嵌入,但它不起作用。我认为错误在于 Discord 无法正确渲染来自该域的图像。 解决此问题的另一种方法是下载图像并将其作为文件发送:

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_bytes = BytesIO(await resp.read())

# creating a discord file
thumb_file = discord.File(fp=thumb_image_bytes, filename="thumb.png")

# setting the file as thumbnail
emb.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
)
© www.soinside.com 2019 - 2024. All rights reserved.