I want to let my discord bot send images/gifs

nad picture nad · Sep 9, 2018 · Viewed 32k times · Source

I've been desperately looking for answers but I can never find anything that helps! I've been trying every bit of code I find that might work but either the command doesn't load (it says the bot is typing and then stops) or just the bot itself doesn't work.

I'm not a very good coder so I've only been able to write messages but I have absolutely no clue on how to let it send images. I'm using python, by the way..

Sorry if this has already been answered ;;

Answer

Aviv Shai picture Aviv Shai · Sep 9, 2018

I know your problem is already solved, but I will post an answer so that people who have this same problem will be able to find the solution easily.

To send an image or GIF, here are two options (adapted from here):

  1. Opening the file and sending it directly to the channel:

    with open('my_image.png', 'rb') as f:
        picture = discord.File(f)
        await channel.send(channel, picture)
    
  2. Passing the file name directly:

    await channel.send(file=discord.File('my_image.png'))
    

Here are some useful links: