使用本地文件作为set_image文件discord.py


问题内容

我知道在discord.py中,您可以set_image将嵌入的图像作为URL。但是,我想使用计算机上的本地文件set_image代替图像的URL。

embed = discord.Embed(title="Title", description="Desc", color=0x00ff00)
embed.set_image(url = "https://example.com/image.png") #this is for set_image using url

我该如何实现?还有其他功能吗?


问题答案:

好吧,我明白了。它的代码如下:

embed = discord.Embed(title="Title", description="Desc", color=0x00ff00) #creates embed
file = discord.File("path/to/image/file.png", filename="image.png")
embed.set_image(url="attachment://image.png")
await ctx.send(file=file, embed=embed)

只有 你应该改变的是2号线的地方说:"path/to/image/file.png"

注意:
第2行和第3行上有一个image.png。不用担心,因为那就是Discord所谓的上载文件(例如:我有一个名为的文件duck.png,Discord将其作为上载到其服务器image.png)。因此,您无需更改image.png零件。但是,如果您使用特定扩展名很重要的文件,请记住将其更改image.png为所需的扩展名。需要特定扩展名的文件示例是GIF,因此请记住将其更改image.png为例如,image.gif如果您使用的是GIF。

您可以在discord.py的官方文档中了解更多信息:https :
//discordpy.readthedocs.io/en/latest/faq.html#how-do-i-use-a-local-image-
file-for-an-嵌入图像