I would like my bot to save on my computer every image, video or even a gif that's sent in a specific channel. Is that possible to do? I know that this kind of stuff can be made by fs directory but I'm not sure how would that code look like could you guys help me?
2 Answers
To get all the images, gifs and videos from a message you can use the .attachments
property of a message. This will give you all the files from that message (if it contains files).
With this property you can create a loop where on every message send, you itterate through the message.attachments
collection. Then, as stated by the documentation, you can call the .url
property on every attachment to get the link from which to download the file.
Once you have the link, you can follow this answer from a different question to download the file. Here's the code copied from the answer:
var http = require('http');
var fs = require('fs');
var download = function(url, dest, cb) {
var file = fs.createWriteStream(dest);
var request = http.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb); // close() is async, call cb after close completes.
});
}).on('error', function(err) { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
if (cb) cb(err.message);
});
};
This means you will have to use the Node.js modules fs
and http
. To use the code, the download method can be called like
download(<The file url to download>, <The file name you want to save it as>, <a callback function which can be called when an error occurs>);

- 3,566
- 1
- 19
- 34
This code must help you,
import shutil
from discord.ext import commands
import requests
TOKEN = ""
prefix = "?"
bot = commands.Bot(command_prefix=prefix)
bot.run(TOKEN)
@bot.event
async def on_message(message):
print("The message's content was", message.content)
url = message.attachments[0]['url']
if url[0:26] == "https://cnd.discordapp.com":
r = requests.get(url, stream=True)
with open(String.join(uuid.uuid4(),".png"), 'wb') as out_file:
shutil.copyfileobj(r.raw, out_file)
use this and create a bot and install package requests, discord.py to run the bot.
Finally, add this bot to your channel and give it a bot role.

- 350
- 3
- 18
-
1okey sorry for my unspecified question. I know how to make bot, im making this bot in discord.js and i just want a help with a code that will save every images videos and gifs in a folder in my computer. Javascript, not python – Stasio Apr 28 '19 at 03:18
-
Okay sorry but i still need some help :D – Stasio Apr 28 '19 at 05:56