【问题标题】:Attach a file in an embed (Discord.py)在嵌入中附加文件 (Discord.py)
【发布时间】:2020-12-04 22:26:40
【问题描述】:

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

【问题讨论】:

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


    【解决方案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", filename="filename.png"),但是对于附件:// url 它仍然只是名称,没有目录。

    【讨论】:

    • 如果discord.File 中的文件路径是绝对路径,这似乎不起作用,而是像普通文件一样将图像发送到嵌入之外。
    • url = 'attachment://image.png' file = discord.File(image_url, filename="image.png") 为我工作(只要urlfilename 匹配,就可以了。image_url 是实际的文件路径,例如,images/filename.png)。
    【解决方案2】:

    请查看documentation for 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”。

    【讨论】:

    • 直接到imgbb.com/upload上传你的图片,上传后复制图片链接,放到embed.set_image(url="&lt;your image link&gt;")
    • 1.转到imgbb.com/upload 2. 点击`从您的计算机浏览` 3. 从您的PC 中选择图片 4. 复制您刚刚上传的图片的链接。 5. 把链接放在embed.set_image(url="&lt;your image link&gt;") 知道了吗?
    【解决方案3】:
    # Async
    await bot.send_file(channel, "filepath.png", content="...", filename="...")
    
    # Rewrite
    file = discord.File("filepath.png", filename="...")
    await channel.send("content", file=file)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 2022-12-18
      • 2014-05-05
      • 1970-01-01
      • 2021-05-06
      • 2019-06-09
      相关资源
      最近更新 更多