#How would I save an attachment provided by a command?

26 messages · Page 1 of 1 (latest)

ebon knoll
#

How would I save an attachment provided by a command (in a folder in my bots source) as I have tried saving the buffer to a file but that corrupts it.

twin oysterBOT
#

• What's your exact discord.js npm list discord.js and node node -v version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.

obsidian forge
ebon knoll
obsidian forge
#

That's what I do atleast.
wget exists in linux

ebon knoll
#

I was ideally hoping to download it with code and save it automatically

obsidian forge
#

You can achieve that by using the child_process package of node.
the exec package to be exact.
That can be used to execute a download command for the attachment link after navigating to your folder within the working directory

#

But double check what you're downloading always.

ebon knoll
#

do you have documentation for it?

obsidian forge
#

What OS are you using ?

ebon knoll
#

for testing, windows, for hosting, my VPS with ubuntu using pm2

obsidian forge
ebon knoll
#

to save time and just make my code more efficient

#

would I be able to use a write stream for this or?

#

I think I have found a way from this code sample

let request = require(`request`);
let fs = require(`fs`);
function download(url){
    request.get(url)
        .on('error', console.error)
        .pipe(fs.createWriteStream('meme.png'));
}
obsidian forge
#

Then I have to apologize because idk much about the fs ecosystem. teri_ded
For me personally I created a single download function that I can use anywhere within the code when I want.

#

Oh yeah that also works. atleast the logic is right

ebon knoll
#

yeah Ill test this and hope that it doesnt corrupt

#

even if it does ill just trial and error it

obsidian forge
#

But where does it save the file to ?
You might have to figure that out as well

#

So your core folders won't get spammed

ebon knoll
#

with that it would save it to the root

#

but if I were to put

#

let request = require(`request`);
let fs = require(`fs`);
function download(url){
    request.get(url)
        .on('error', console.error)
        .pipe(fs.createWriteStream('./files/meme.png'));
}
#

it would save it to the files directory