Can a discord bot send a message with an image it got via a direct message?

Question:

I’m working on a Discord bot that handles taking direct messages and posting the message to a Channel in a Discord server i’m in. This is working as intended but i was wondering if there is any way to send images it gets via direct messages as well?

I’m using Python to code my bot

# Post the message into the channel
    await server_channel.send(f'**Complaint about the following**n{message.content}')

At the moment it can only output text, when an image is sent the{message.content} returns as blank

I don’t even know where to start. The bot is currently able to post the Direct Message(DM) Contents in a Specific Channel and reply to the DM confirming its been posted.

EDIT:
I have been able to sort this out with the following code

# Get the server channel you want to send the message to
        server_channel = client.get_channel(int(CHANNEL_ID))

if message.attachments:
            # Get the image file
            image_file = message.attachments[0]

            # Read the image file content as bytes
            image_bytes = await image_file.read()

            # Send the image to the server channel
            await server_channel.send(file=discord.File(fp=BytesIO(image_bytes), filename=image_file.filename))
Asked By: MadCappedBeef

||

Answers:

You can use discord.Message.attachments, it returns a list of attachments, you can loop through and get discord.Attachment attributes (like the URL). After that you can simply send URL as the content, or you can send discord.File using Requests and PIL : Here is an example.

Answered By: iRyan23
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.