Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Discord bot getting a file attachement and reuploading it

I’d like to make a bot, to resend an image/file sent to a channel.

The bot get’s the url of the file (message.attachments[0].url), but when I want to send it as a file, it says FileNotFoundError: [Errno 2] No such file or directory: ….png

The bot was working and resending an image one single time, but then about 50 times, it’s not.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith("anon ") or message.content.startswith("Anon "):
        if message.attachments:
            await message.channel.send(f"{message.content[5:]}", file=discord.File(message.attachments[0].url))

Thank you for helping!

>Solution :

You need to convert attachment to file. You can use to_file() function like this:

await message.channel.send(f"{message.content[5:]}", file=await message.attachments[0].to_file()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading