From d27c1bad558ce591405fb69e0ba64cd587dfee0a Mon Sep 17 00:00:00 2001 From: SileNce5k Date: Tue, 17 Jun 2025 01:55:20 +0200 Subject: [PATCH] Add initial dl command --- commands/misc/dl.js | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 commands/misc/dl.js diff --git a/commands/misc/dl.js b/commands/misc/dl.js new file mode 100644 index 0000000..d711201 --- /dev/null +++ b/commands/misc/dl.js @@ -0,0 +1,65 @@ +const { execSync } = require('child_process'); +const path = require('path'); +const fs = require('fs') + +module.exports = { + name: 'dl', + description: 'Download a video', + moreHelp: [ + "Usage: dl " + ], + async execute({message, args}) { + const downloadsDir = path.resolve(process.cwd(), 'data', 'downloads', Math.floor(new Date).toString()); + fs.mkdirSync(downloadsDir, {recursive: true}); + + let url; + + if(args[0] && args[0].startsWith("https://") ){ + url = args[0]; + } else { + return message.channel.send("No url") + } + + if(this.executeCommand(`yt-dlp "${url}" -P ${downloadsDir}`).error === false){ + message.channel.send("An error occured when executing the command"); + this.cleanUp(downloadsDir); + return; + } + + + let files = fs.readdirSync(downloadsDir); + if(files < 1) { + this.cleanUp(downloadsDir); + message.channel.send("Something went wrong when downloading the video.") + return; + } + const filename = files[0]; + + await message.channel.send({files: [{ + attachment: path.resolve(`${downloadsDir}/${filename}`) + }]}) + + this.cleanUp(downloadsDir); + + + + + }, + + cleanUp(downloadsDir){ + fs.rmSync(downloadsDir); + }, + executeCommand(command) { + console.log("Executing:", command) + try { + const output = execSync(command, { encoding: 'utf-8' }) + if (output.length != 0) + console.log(output) + } catch (error) { + console.error(`Error executing ${command.split(" ")[0]} command:`, error); + return { error: true }; + } + return { error: false }; + }, + +} \ No newline at end of file