S
S
SoWhatMan2020-02-21 01:15:54
Node.js
SoWhatMan, 2020-02-21 01:15:54

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

1 answer(s)
R
Roman, 2020-03-08
@SoWhatMan

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;
    });
});

well, here is an example of what the bot will write to the logs folder (this is a log with your message to my bot :)
{
  "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
    }
  ]
}

each attachment contains 2 links (normal url and proxyurl):
url https://cdn.discordapp.com/attachments/45220252167...
proxyurl https://media.discordapp.net/attachments/452202521...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question