Compare commits

...
Author SHA1 Message Date
c6ef14ee70
Fix verbose flag not working
All checks were successful
CI / CI (push) Successful in 1m25s
2025-06-21 15:40:16 +02:00
e68bb41960
Fix typo 2025-06-21 15:35:26 +02:00
a6060cdbfb
Use execFileSync instead of execSync to prevent command injection
All checks were successful
CI / CI (push) Successful in 1m24s
and add verbose flag
2025-06-21 15:30:18 +02:00
6293010b6c
Fix command injection vulnerability by using a command array
All checks were successful
CI / CI (push) Successful in 1m25s
2025-06-21 14:25:43 +02:00
f4ffcbebde
Use Date.now()
All checks were successful
CI / CI (push) Successful in 1m25s
2025-06-21 13:55:18 +02:00
2073a46715
Fix file check 2025-06-21 13:54:45 +02:00
27fc1c55bf
Edit the text to nothing
All checks were successful
CI / CI (push) Successful in 1m24s
2025-06-21 13:40:03 +02:00
8314be972f
Slice first character of the argument
All checks were successful
CI / CI (push) Successful in 1m26s
2025-06-21 13:35:58 +02:00
ad73a83514
Send an inital message when downloading begins & check for cookies file
All checks were successful
CI / CI (push) Successful in 1m26s
2025-06-21 13:31:59 +02:00
40052ac85d
Improve error messages
All checks were successful
CI / CI (push) Successful in 1m25s
2025-06-18 00:37:03 +02:00
09eee1a0ca
Add support for prepending URL with '<' and appending with '>' 2025-06-18 00:36:32 +02:00
340727ae60
dl: Add cookie file
All checks were successful
CI / CI (push) Successful in 1m24s
2025-06-18 00:14:10 +02:00
0a35549666
dl: fix error check
All checks were successful
CI / CI (push) Successful in 1m25s
2025-06-17 02:24:49 +02:00
9d0eb60b81
Improve dl command
All checks were successful
CI / CI (push) Successful in 1m24s
2025-06-17 02:21:31 +02:00
d27c1bad55
Add initial dl command
All checks were successful
CI / CI (push) Successful in 1m24s
2025-06-17 01:55:20 +02:00

85
commands/misc/dl.js Normal file
View file

@ -0,0 +1,85 @@
const { execFileSync } = require('child_process');
const path = require('path');
const fs = require('fs')
module.exports = {
name: 'dl',
description: 'Download a video',
moreHelp: [
"Usage: <prefix>dl <url>"
],
async execute({message, args}) {
const downloadsDir = path.resolve(process.cwd(), 'data', 'downloads', Date.now().toString());
const cookieFilepath = path.resolve(process.cwd(), 'data', 'cookies.txt')
if(!fs.existsSync(cookieFilepath)) {
message.channel.send("Some dependencies are needed for the command to work properly. Please let the bot's owner know.")
return;
}
fs.mkdirSync(downloadsDir, {recursive: true});
let url;
if(args.length > 0){
if(args[0].charAt(0) === '<' && args[0].charAt(args[0].length - 1) === '>'){
args[0] = args[0].slice(1, args[0].length - 1)
}
try {
url = new URL(args[0]);
url = url.href;
} catch (error) {
this.cleanUp(downloadsDir);
message.channel.send("Could not parse the provided argument as a URL.");
return;
}
} else {
this.cleanUp(downloadsDir);
return message.channel.send("You have to provide a URL in an argument.")
}
const originalMessage = await message.channel.send("Downloading video...")
if(this.executeCommand("yt-dlp", [url, "-P", downloadsDir, "--cookies", cookieFilepath]).error){
originalMessage.edit("An error occurred when downloading the video.");
this.cleanUp(downloadsDir);
return;
}
let files = fs.readdirSync(downloadsDir);
if(files.length < 1) {
this.cleanUp(downloadsDir);
originalMessage.edit("Something went wrong when downloading the video.")
return;
}
const filename = files[0];
await originalMessage.edit({
content: null,
files: [{
attachment: path.resolve(downloadsDir, filename)
}]})
this.cleanUp(downloadsDir);
},
cleanUp(downloadsDir){
fs.rmSync(downloadsDir, {force: true, recursive: true});
},
executeCommand(command, commandArgs, verbose=false) {
if (typeof command !== 'string' || !Array.isArray(commandArgs)) return { error: true };
console.log("Executing:", command, commandArgs.join(" "));
try {
const output = execFileSync(command, commandArgs, {encoding: 'utf8'})
if (output.length != 0 && verbose)
console.log(output)
} catch (error) {
console.error(`Error executing ${command} command:`, error);
return { error: true };
}
return { error: false };
},
}