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

2022-01-15 00:00:00 python python-3.x discord.py

问题描述

我知道在 discord.py 中,您可以将嵌入的 set_image 设置为图像的 url.但是,我想在我的计算机上使用本地文件作为 set_image 而不是图像的 url.

I am aware that in discord.py, you can make the set_image of an embed a url of an image. But, I want to use a local file on my computer for the set_image instead of a url of an image.

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

我怎样才能做到这一点?有没有别的功能什么的?

How can I achieve this? Is there another function or something?


解决方案

好的,我知道了.它的代码如下:

Ok I just got it. The code for it is the following:

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"

The only thing you should be changing is line 2 where it says "path/to/image/file.png"

注意:在第 2 行和第 3 行,有一个 image.png.不用担心,因为这就是 Discord 所称的上传文件(例如:我有一个名为 duck.png 的文件,Discord 将其作为 image.png 上传到他们的服务器).所以你不需要改变 image.png 部分.但是,如果您使用的是特定扩展名很重要的文件,请记住将 image.png 更改为所需的扩展名.需要特定扩展名的文件示例是 GIF,因此如果您使用的是 GIF,请记住将 image.png 更改为例如 image.gif.

Note: on lines 2 and 3, there is an image.png. Fret not about it since thats what Discord is calling the uploaded file (Example: I have a file called duck.png, Discord uploads it to their servers as image.png). So you don't need to change the image.png part. However, if you are using a file that the specific extension matters, remember to change the image.png to the desired extension. An example of a file that requires a specific extension is a GIF so remember to change image.png to for example, an image.gifif you are using a GIF.

您可以在 discord.py 的官方文档中阅读更多内容:https://discordpy.readthedocs.io/en/latest/faq.html#how-do-i-use-a-local-image-嵌入图像的文件

You can read more here at discord.py's official documentation: https://discordpy.readthedocs.io/en/latest/faq.html#how-do-i-use-a-local-image-file-for-an-embed-image

相关文章