0

I'm currently trying to make a bot that automatically saves a link sent in a message. Basically, a user sends the command -save test.com/123.png and my bot then downloads the 123.png file.

I know how to download directly attached images/files, but need to have it work with links as well. I tried searching online for a solution already, but didn't really found anything that helped me.

Das_Unterstrich
  • 111
  • 1
  • 11
  • 1
    If the resource you're trying to download is public, then an HTTP GET request using any networking python library like requests should work. Read the save argument in command, and then send GET request to link. – minzey Jun 23 '21 at 18:12

2 Answers2

1

Figured out a solution by myself, ironically found a pretty old own python file where I tried exactly this.

@client.command()
async def test(ctx, url):
    ftype = url.split('/')[-1]
    myfile = requests.get(url)
    open(f'D:\\Tools\\python\\file_{ftype}', 'wb').write(myfile.content)
Das_Unterstrich
  • 111
  • 1
  • 11
0

This will download only the user send the file, this doesn't load all messages and get the image

@client.event
async def on_message(message):

    if len(message.attachments) > 0:
        attachment = message.attachments[0]

    if (
        attachment.filename.endswith(".jpg")
        or attachment.filename.endswith(".jpeg")
        or attachment.filename.endswith(".png")
        or attachment.filename.endswith(".webp")
        or attachment.filename.endswith(".gif")
    ):
        img_data = requests.get(attachment.url).content
        with open("image_name.jpg", "wb") as handler:
            handler.write(img_data)

    elif (
        "https://images-ext-1.discordapp.net" in message.content
        or "https://tenor.com/view/" in message.content
    ):
        print(message.content)
Jan
  • 302
  • 3
  • 5