如何解决在嵌入文件中附加文件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)
解决方法
这是可能的。让我举个例子。
# 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”),但是对于附件://网址,它仍然只是名称,没有目录。
,# Async
await bot.send_file(channel,"filepath.png",content="...",filename="...")
# Rewrite
file = discord.File("filepath.png",filename="...")
await channel.send("content",file=file)
,
请看看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中的图像直接附加到嵌入中。 因此,您只需要将文件作为附件发送,而不是作为附件发送,这是操作方法-
- 将要发送的图像放在与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”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。