discord.py 嵌入本地保存的图片
discord.py embed with locally saved images
所以要在 discord.py 中的嵌入中设置图像,语法是
embed.set_image(url='http://url_to_image')
但我的问题是我不知道如何将其设置为本地保存的图像,因此我不必继续与这些 URL 建立联系。
谢谢。
(link 到文档:http://discordpy.readthedocs.io/en/latest/api.html#discord.Embed.set_image)
我的计算机上有一个包含图像的文件夹,我的 discord 机器人用它来随机挑选和发送图像。
这是我的代码,经过简化和重新设计,只发送一张随机图片:
import os
@client.event
async def on_message(message):
if message.content == ".img": # Replace .img with whatever you want the command to be
imgList = os.listdir("./All_Images") # Creates a list of filenames from your folder
imgString = random.choice(imgList) # Selects a random element from the list
path = "./All_Images/" + imgString # Creates a string for the path to the file
await client.send_file(message.channel, path) # Sends the image in the channel the command was used
同样在@client.event 中,您需要记住用您在代码中进一步命名的客户端替换客户端。
当然还要将文件夹名称更改为与您的文件实际位于同一文件夹中的名称。
希望这就是您要找的答案,祝您好运!
(http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.send_file)
您需要将机器人根目录中的本地图像设置为附件,您还需要在 ctx.send
中提及该文件!我试着只做附件,但它显示了一个空白的嵌入。因此,请提及 file
并将同一文件作为附件放入 set_image
并尝试 运行 代码。有效
file = discord.File("output.png")
e = discord.Embed()
e.set_image(url="attachment://output.png")
await ctx.send(file = file, embed=e)
如果您仍然遇到问题,请告诉我!
所以要在 discord.py 中的嵌入中设置图像,语法是
embed.set_image(url='http://url_to_image')
但我的问题是我不知道如何将其设置为本地保存的图像,因此我不必继续与这些 URL 建立联系。
谢谢。
(link 到文档:http://discordpy.readthedocs.io/en/latest/api.html#discord.Embed.set_image)
我的计算机上有一个包含图像的文件夹,我的 discord 机器人用它来随机挑选和发送图像。
这是我的代码,经过简化和重新设计,只发送一张随机图片:
import os
@client.event
async def on_message(message):
if message.content == ".img": # Replace .img with whatever you want the command to be
imgList = os.listdir("./All_Images") # Creates a list of filenames from your folder
imgString = random.choice(imgList) # Selects a random element from the list
path = "./All_Images/" + imgString # Creates a string for the path to the file
await client.send_file(message.channel, path) # Sends the image in the channel the command was used
同样在@client.event 中,您需要记住用您在代码中进一步命名的客户端替换客户端。
当然还要将文件夹名称更改为与您的文件实际位于同一文件夹中的名称。
希望这就是您要找的答案,祝您好运!
(http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.send_file)
您需要将机器人根目录中的本地图像设置为附件,您还需要在 ctx.send
中提及该文件!我试着只做附件,但它显示了一个空白的嵌入。因此,请提及 file
并将同一文件作为附件放入 set_image
并尝试 运行 代码。有效
file = discord.File("output.png")
e = discord.Embed()
e.set_image(url="attachment://output.png")
await ctx.send(file = file, embed=e)
如果您仍然遇到问题,请告诉我!