简体   繁体   中英

Downloading a message attachment in discord.js

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM