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

时间:2020-08-15 05:15:09

标签: python-3.x discord.py-rewrite

我目前正在用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)

3 个答案:

答案 0 :(得分:1)

这是可能的。让我举个例子。

# 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”),但是对于附件://网址,它仍然只是名称,没有目录。

答案 1 :(得分:0)

# Async
await bot.send_file(channel, "filepath.png", content="...", filename="...")

# Rewrite
file = discord.File("filepath.png", filename="...")
await channel.send("content", file=file)

答案 2 :(得分:0)

请看看documentation for discord.embed

要在嵌入文件中设置文件,必须输入网址,您无法通过本地主机将图像放入嵌入文件。

要在嵌入图像上设置图像,可以使用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”。