1

Basic thing I just need fixed; wanting to download a file from attachment but i'm not sure how to describe the attachment as an object/file.

I just have this:

let jimage = message.attachments.first()
fs.writeFileSync(`./${jimage.filename}`, jimage)

Only issue is that, for FS's second argument, discord doesn't have a selection for just the file. Reason i'm trying this way is because I took this idea from here: how to save an image from discord using discord bot node js But "attachment.file" isn't a real thing. Other threads show just really overly complicated methods with new calls, like something called request, I just wanna do this straight off fs and discord.

Cookid
  • 71
  • 1
  • 2
  • 13
  • 2
    If discord.js offered an "attachment.file" buffer that simply contained a buffer or stream of the file already set up, i would consider that a huge flaw. It would mean discord.js is downloading every attachment it sees automatically and storing them in memory. Yikes! Instead, all you have is the URL of the file. This is a web link like any other and you must use any HTTP request module to request the URL and download the image to your computer. – Klaycon Aug 10 '20 at 21:22
  • See [this StackOverflow post](https://stackoverflow.com/questions/51550993/download-file-to-local-computer-sent-attatched-to-message-discord/51565540) - it should answer your question. – Static Aug 13 '20 at 11:41

1 Answers1

1

I found a pretty simple solution using this line over here:

request(message.attachments.first().url).pipe(fs.createWriteStream('image.png'))

It basically finds where file is stored on the discord website and using "request" gets it from there. To keep file's name you can use message.attachments.first.name if you need to. Unfortunately I haven't found out how to save file type. If any questions, I will try to help! P.S. don't forget to define and download the "request" module.

Ivan_Fox
  • 11
  • 3