Haha-Yes/commands/utility/download.old

178 lines
6 KiB
Text
Raw Permalink Normal View History

2018-12-30 01:20:24 +01:00
const { Command } = require('discord-akairo');
const fs = require('fs');
const youtubedl = require('youtube-dl');
2019-01-19 17:23:45 +01:00
const hbjs = require('handbrake-js');
2019-09-13 19:06:29 +02:00
const os = require('os');
const filetype = require('file-type');
2018-12-25 19:17:07 +01:00
2018-12-30 01:20:24 +01:00
class DownloadCommand extends Command {
2019-01-02 08:09:45 +01:00
constructor() {
super('download', {
aliases: ['download', 'dl'],
category: 'utility',
2019-11-09 12:04:01 +01:00
clientPermissions: ['SEND_MESSAGES', 'EMBED_LINKS', 'ATTACH_FILES'],
2019-01-02 08:09:45 +01:00
args: [
{
id: 'link',
type: 'string',
2019-06-23 03:41:59 +02:00
prompt: {
2019-11-15 21:39:53 +01:00
start: 'Send the link of which video you want to download',
2019-06-23 03:41:59 +02:00
}
},
2019-11-06 20:03:12 +01:00
{
id: 'caption',
type: 'string',
match: 'rest'
},
{
id: 'spoiler',
match: 'flag',
flag: ['--spoil', '--spoiler', '-s']
2019-01-02 08:09:45 +01:00
}
],
description: {
2019-10-25 12:45:05 +02:00
content: 'Download videos from different website from the link you provided, use "-s" to make the vid a spoiler',
2019-11-06 20:03:12 +01:00
usage: '[link] [caption]',
examples: ['https://www.youtube.com/watch?v=6n3pFFPSlW4 Look at this funny gnome']
2019-01-02 08:09:45 +01:00
}
});
}
2019-01-02 08:09:45 +01:00
async exec(message, args) {
2019-11-06 20:03:12 +01:00
if (args.caption == null) args.caption = '';
2019-01-02 08:09:45 +01:00
let link = args.link;
let fileName;
if (args.spoiler) {
2020-02-25 14:05:43 +01:00
fileName = `SPOILER_${message.id}_video`;
} else {
2020-02-25 14:05:43 +01:00
fileName = `${message.id}_video`;
}
2018-12-30 01:20:24 +01:00
2019-11-22 12:30:20 +01:00
const Embed = this.client.util.embed()
2020-03-22 21:54:19 +01:00
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
2019-11-09 15:00:02 +01:00
.setAuthor(`Downloaded by ${message.author.username}`, message.author.displayAvatarURL(), link)
.setDescription(args.caption)
2020-01-30 11:55:48 +01:00
.setFooter(`You can get the original video by clicking on the "downloaded by ${message.author.username}" message!`);
2019-11-09 15:00:02 +01:00
let compressEmbed = this.client.util.embed()
2020-03-22 21:54:19 +01:00
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
.setTitle('This one will need compression!')
.setDescription('Starting compression now!')
.setFooter('Want it to go faster? Donate to the dev with the donate command, so i can get a better server and do it faster!');
2019-01-02 08:09:45 +01:00
if (link.includes('http') || link.includes('www')) {
2019-10-16 17:47:16 +02:00
let loadingmsg = await message.channel.send('Downloading <a:loadingmin:527579785212329984>');
if (fs.existsSync(`${os.tmpdir()}/${fileName}`)) {
fs.unlink(`${os.tmpdir()}/${fileName}`, (err) => {
if (err) console.error(err);
});
}
return youtubedl.exec(link, ['--rm-cache-dir', '-o', `${os.tmpdir()}/${fileName}`], {}, async function(err) {
2019-10-24 18:15:21 +02:00
if (err) {
2020-05-14 17:08:04 +02:00
console.error(err.toString());
2019-10-24 18:15:21 +02:00
loadingmsg.delete();
2020-05-14 17:44:48 +02:00
if (err.toString().includes('HTTP Error 429') || err.toString().includes('HTTP Error 403')) {
return message.channel.send('An error has occured, I can\'t download from the link you provided because the website has blocked the bot. Please try again later.');
} else {
return message.channel.send('An error has occured, I can\'t download from the link you provided.');
2020-06-17 18:41:38 +02:00
}
}
2020-02-25 14:09:42 +01:00
2020-02-25 14:05:43 +01:00
let ext = 'mp4';
2020-02-25 14:05:43 +01:00
if (fs.existsSync(`${os.tmpdir()}/${fileName}`)) {
ext = await filetype.fromFile(`${os.tmpdir()}/${fileName}`);
ext = ext.ext; // This look stupid but hey, it work
if (ext === '3gp') ext = 'mp4'; // Change 3gp file extension to mp4 so discord show the video ( and to stop people from complaining )
2020-02-25 14:09:42 +01:00
fs.renameSync(`${os.tmpdir()}/${fileName}`, `${os.tmpdir()}/${fileName}.${ext}`);
} else if (fs.existsSync(`${os.tmpdir()}/${fileName}.mkv`)) { // If it can't find the video assume it got merged and end with mkv
fs.renameSync(`${os.tmpdir()}/${fileName}.mkv`, `${os.tmpdir()}/${fileName}.mp4`); // Discord play mkv just fine but it need to end with mp4
2020-03-06 15:14:27 +01:00
} else if (fs.existsSync(`${os.tmpdir()}/${fileName}.webm`)) { // Same as mkv
ext = 'webm';
2020-02-25 14:05:43 +01:00
}
let file = fs.statSync(`${os.tmpdir()}/${fileName}.${ext}`);
2019-10-24 18:15:21 +02:00
let fileSize = file.size / 1000000.0;
2019-10-24 18:15:21 +02:00
//Compress vid if bigger than 8MB
if (fileSize > 8) {
let compressmsg = await message.channel.send(compressEmbed);
2019-10-24 18:15:21 +02:00
loadingmsg.delete();
2019-10-24 18:15:21 +02:00
const options = {
input: `${os.tmpdir()}/${fileName}.${ext}`,
output: `${os.tmpdir()}/${fileName}compressed.${ext}`,
2020-05-14 06:48:03 +02:00
preset: 'Web/Discord Tiny 5 Minutes 240p30'
2019-10-24 18:15:21 +02:00
};
let handbrake = hbjs.spawn(options);
handbrake.on('error', err => {
console.error(err);
compressmsg.delete();
return message.channel.send('An error has occured while compressing the video');
});
2019-11-10 22:37:02 +01:00
let percentComplete;
let eta;
2019-10-24 18:15:21 +02:00
handbrake.on('progress', progress => {
2019-11-10 22:37:02 +01:00
percentComplete = progress.percentComplete;
eta = progress.eta;
console.log(`Percent complete: ${progress.percentComplete}, ETA: ${progress.eta}`);
2019-10-24 18:15:21 +02:00
});
2019-11-10 22:37:02 +01:00
// Every 5 seconds update the compress message with the %
2020-02-20 20:37:42 +01:00
let editmsg = setInterval(() => {
compressEmbed.setDescription(`Ready in ${eta === '' ? 'soon enough' : eta}. ${percentComplete}% complete.`);
compressmsg.edit(compressEmbed);
2019-11-10 22:37:02 +01:00
}, 5000);
2019-10-24 18:15:21 +02:00
handbrake.on('end', async function () {
2020-02-20 20:37:42 +01:00
clearInterval(editmsg);
file = fs.statSync(`${os.tmpdir()}/${fileName}compressed.${ext}`);
2019-11-10 22:37:02 +01:00
fileSize = file.size / 1000000.0;
2019-10-24 18:15:21 +02:00
compressmsg.delete();
message.delete();
2019-11-10 22:37:02 +01:00
if (fileSize > 8) {
return message.channel.send('File too big!');
}
return message.channel.send({embed: Embed, files: [`${os.tmpdir()}/${fileName}compressed.${ext}`]})
2019-10-24 18:15:21 +02:00
.catch(err => {
console.error(err);
return message.channel.send('File too big');
})
.then(() => {
// Delete file after it have been sent
fs.unlinkSync(`${os.tmpdir()}/${fileName}.${ext}`);
2019-10-24 18:15:21 +02:00
});
});
} else {
2019-10-16 17:47:16 +02:00
loadingmsg.delete();
2019-11-11 18:04:40 +01:00
message.delete();
message.channel.send({embed: Embed, files: [`${os.tmpdir()}/${fileName}.${ext}`]})
.catch(err => {
console.error(err);
message.channel.send('File too big');
})
.then(() => {
// Delete file after it have been sent
fs.unlinkSync(`${os.tmpdir()}/${fileName}.${ext}`);
});
2019-01-19 17:23:45 +01:00
}
2019-01-02 08:09:45 +01:00
});
}
2019-10-24 18:15:21 +02:00
return message.channel.send('You need to input a valid link');
2019-01-02 08:09:45 +01:00
}
2018-12-30 01:20:24 +01:00
}
2018-12-30 01:20:24 +01:00
module.exports = DownloadCommand;