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

时间:2023-05-12
本文介绍了使用本地文件作为 set_image 文件 discord.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我知道在 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

这篇关于使用本地文件作为 set_image 文件 discord.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何制作不和谐的机器人循环音频?[不和谐.py] 下一篇:我将如何为我的不和谐机器人令牌创建一个 .env 文件?

相关文章