I'm making a map loader that relies on attachments and .txt files. I could do it with normal text, but since Discord has a character limit for each message, it doesn't work.
That's why I want to download message attachments on my local machine to process them further.
How can I download attachments from Discord messages using fs?
I think you can just use a simple get request to the image's url (you can get this through attachment.url) using node-fetch then pipe the response to a .png file using fs
(If you get errors while installing node-fetch or while using the code below, try installing node-fetch v2 instead of the latest release via npm install node-fetch@2)
const fs = require('fs')
const fetch = require('node-fetch')
const imageUrl = 'https://cdn.discordapp.com/attachments/GLD_ID/MSG_ID/IMAGE.png'
fetch(imageUrl)
.then(res =>
res.body.pipe(fs.createWriteStream('./path/to/image.png'))
)
You can also read this post which goes into more detail