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

标签 python python-3.x discord.py

我知道在 discord.py 中,您可以创建 set_image嵌入图像的 url。但是,我想在我的计算机上使用本地文件作为 set_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

我怎样才能做到这一点?还有其他功能吗?

最佳答案

好的,我刚收到。它的代码如下:

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-embed-image

关于python - 使用本地文件作为 set_image 文件 discord.py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61578927/

相关文章:

python - clickAndHold 在 Selenium webdriver (python) 中不起作用

python - 在循环中按索引对列表中的元素求和

python - 从 C++ 调用 Python 脚本并使用其输出

python - 在这种情况下,语法 [] 意味着什么?

python - discord.py ffmpeg 歌曲一开始播放太快

python - 反转为 'product',没有找到参数。尝试了 1 个模式 : [u'products/(? P<product>[-\\w]+)/$']

python - 如何将当前的 pygame 表面转换为更大的表面?

sql - 如何在 Python 3.7 中连接到 hadoop/hive 数据库(需要身份验证)并执行简单的 SQL 查询?

python - 为什么多个 on_message 事件不起作用?

channel - 如何使用discord.py获取所有文本 channel ?