Answer the question
In order to leave comments, you need to log in
How to save a file sent in a message?
I need to track the presence of an attached file in the message, and if there is one, get its link. How many searched on the Internet, but found nothing.
I will make a reservation that I am a beginner in this area, so please do not throw sticks and too smart words.
Thanks in advance for your reply.
Answer the question
In order to leave comments, you need to log in
Something like this:
const fs = require("fs");
const Discord = require("discord.js");
const client = new Discord.Client();
client.login("ТУТ ВАШ ТОКЕН");
...
// обработчик события message
client.on('message', async message => {
const filename = Date.now()+".json";
const data = {
content: message.content,
attachments: message.attachments.map(attach=>{
return {
id: attach.id,
url: attach.url,
proxyURL: attach.proxyURL,
filename: attach.filename,
filesize: attach.filesize,
height: attach.height,
width: attach.width
};
})
};
// не забудьте создать папочку logs в папке с ботом
fs.writeFile('./logs/'+filename, JSON.stringify(data), (err) => {
if (err) throw err;
});
});
{
"content":"Тест",
"attachments":[
{
"id":"686193341083549696",
"url":"https://cdn.discordapp.com/attachments/452202521671237653/686193341083549696/IMG-30dd366b26acb4b1cbbe786f3ad22862-V.jpg",
"proxyURL":"https://media.discordapp.net/attachments/452202521671237653/686193341083549696/IMG-30dd366b26acb4b1cbbe786f3ad22862-V.jpg",
"filename":"IMG-30dd366b26acb4b1cbbe786f3ad22862-V.jpg",
"filesize":58121,
"height":330,
"width":374
}
]
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question