在嵌入中附加文件 (Discord.py)

新手上路,请多包涵

我目前正在用 discord.py Rewrite 编写一个不和谐的机器人,我想将图像附加到嵌入中,但我无法弄清楚。

 import discord
from discord.ext import commands
from discord import Embeds

crafting_table = Embed(title="Crafting Table", description=discord.File("./images/Crafting_Table_GUI.png"))

@client.command()
async def Info(ctx, *, question):
    if "crafting table" in question:
        await ctx.send(embed=crafting_table)

原文由 Kai 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 607
2 个回答

这个有可能。让我举个例子。

 # Rewrite
file = discord.File("filename.png") # an image in the same folder as the main bot file
embed = discord.Embed() # any kwargs you want here
embed.set_image(url="attachment://filename.png")
# filename and extension have to match (ex. "thisname.jpg" has to be "attachment://thisname.jpg")
await ctx.send(embed=embed, file=file)

如果它在目录中,您可以执行 discord.File(“images/filename.png”, filename=“filename.png”),但对于附件:// url,它仍然只是名称,没有目录。

原文由 Chicken Devs 发布,翻译遵循 CC BY-SA 4.0 许可协议

请查看 discord.embed 的文档

要在嵌入中设置文件,需要 url,您不能通过本地主机将图像放入嵌入中。

要在嵌入中设置图像,您可以使用 embed.set_image(url="<your image link>")

这是一个例子 -

 @client.command(name="Info")
async def Info(ctx, *, question):
    if question == "crafting table":
        embed = discord.Embed(color=0xffffff)
        embed.set_image(url="<your image link>")
        await ctx.send(embed=embed)

您不能直接将 PC 中的图像附加到嵌入中。所以你只需要将文件作为附件发送,而不是嵌入,这是如何做到的 -

  1. 将您要发送的图像放在与您的 Bot 相同的文件夹中。然后使用此代码 -
 @client.command(name="Info")
async def Info(ctx, *, question):
    if question == "crafting table":
        await ctx.send(file=discord.file(fp="<your_file_name>.jpg", filename="image.jpg"))

注意 - 相应地更改文件的扩展名。在示例中,我使用了“.jpg”。

原文由 Videro 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进