Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

discord.py embed with locally saved images

So for setting an image on an embed in discord.py, the syntax is

embed.set_image(url='http://url_to_image')

But my problem is that I don't know how I would set it as an image that is saved locally, so I don't have to keep making connections to these URL's.

Thanks.

(link to docs: http://discordpy.readthedocs.io/en/latest/api.html#discord.Embed.set_image)

like image 703
ChickenRun Avatar asked Jan 30 '18 21:01

ChickenRun


2 Answers

You need to set the local image from the bot's root dir as an attachment and you need to mention the file in ctx.send aswell! I tried doing attachment only, but it shows a blank embed. So take mention the file and take the same file into the set_image as an attachment and try running the code. It works 😃

    file = discord.File("output.png")
    e = discord.Embed()
    e.set_image(url="attachment://output.png")
    await ctx.send(file = file, embed=e)

Let me know if you still face an issue!

like image 102
Zephyr Avatar answered Oct 23 '22 23:10

Zephyr


I have a folder with images on my computer which my discord bots use to randomly pick and send images.

This is my code simplified and repurposed to just send one random image:

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

Also in @client.event you need to remember to replace client with whatever you named your client further up in your code.

And of course also change the folder name to one that is actually in the same folder as your file.

I hope this was the answer you were looking for, good luck!

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

like image 1
Markusaw Avatar answered Oct 23 '22 21:10

Markusaw