discord.py嵌入本地保存的图像

Chi*_*Run 5 python discord.py

因此,为在discord.py中嵌入的图像设置图像时,语法为

embed.set_image(url='http://url_to_image')
Run Code Online (Sandbox Code Playgroud)

但是我的问题是我不知道如何将其设置为本地保存的图像,因此我不必一直保持与这些URL的连接。

谢谢。

(链接到文档:http : //discordpy.readthedocs.io/en/latest/api.html#discord.Embed.set_image

小智 5

您需要将机器人根目录中的本地图像设置为附件,并且还需要提及该文件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)
Run Code Online (Sandbox Code Playgroud)

如果您仍然遇到问题,请告诉我!


Mar*_*saw 2

我的计算机上有一个包含图像的文件夹,我的不和谐机器人使用它来随机挑选和发送图像。

这是我的代码简化后的目的,仅发送一张随机图像:

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
Run Code Online (Sandbox Code Playgroud)

另外,在 @client.event 中,您需要记住将 client 替换为您在代码中进一步命名的 client 。

当然,还要将文件夹名称更改为与您的文件实际上位于同一文件夹中的名称。

我希望这是您正在寻找的答案,祝您好运!

http://discordpy.readthedocs.io/en/latest/api.html#discord.Client.send_file

  • 这并不能解决使用“set_image”方法设置本地保存的“Embed”图像的问题。 (13认同)