forked from Supositware/Haha-Yes
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
178 lines
6.0 KiB
Plaintext
178 lines
6.0 KiB
Plaintext
6 years ago
|
const { Command } = require('discord-akairo');
|
||
![]()
6 years ago
|
const fs = require('fs');
|
||
|
const youtubedl = require('youtube-dl');
|
||
6 years ago
|
const hbjs = require('handbrake-js');
|
||
5 years ago
|
const os = require('os');
|
||
5 years ago
|
const filetype = require('file-type');
|
||
6 years ago
|
|
||
6 years ago
|
class DownloadCommand extends Command {
|
||
6 years ago
|
constructor() {
|
||
|
super('download', {
|
||
|
aliases: ['download', 'dl'],
|
||
|
category: 'utility',
|
||
5 years ago
|
clientPermissions: ['SEND_MESSAGES', 'EMBED_LINKS', 'ATTACH_FILES'],
|
||
6 years ago
|
args: [
|
||
|
{
|
||
|
id: 'link',
|
||
|
type: 'string',
|
||
5 years ago
|
prompt: {
|
||
5 years ago
|
start: 'Send the link of which video you want to download',
|
||
5 years ago
|
}
|
||
![]()
6 years ago
|
},
|
||
5 years ago
|
{
|
||
|
id: 'caption',
|
||
|
type: 'string',
|
||
|
match: 'rest'
|
||
|
},
|
||
5 years ago
|
{
|
||
|
id: 'spoiler',
|
||
|
match: 'flag',
|
||
|
flag: ['--spoil', '--spoiler', '-s']
|
||
6 years ago
|
}
|
||
|
],
|
||
|
description: {
|
||
5 years ago
|
content: 'Download videos from different website from the link you provided, use "-s" to make the vid a spoiler',
|
||
5 years ago
|
usage: '[link] [caption]',
|
||
|
examples: ['https://www.youtube.com/watch?v=6n3pFFPSlW4 Look at this funny gnome']
|
||
6 years ago
|
}
|
||
|
});
|
||
|
}
|
||
![]()
6 years ago
|
|
||
6 years ago
|
async exec(message, args) {
|
||
5 years ago
|
if (args.caption == null) args.caption = '';
|
||
|
|
||
6 years ago
|
let link = args.link;
|
||
5 years ago
|
let fileName;
|
||
|
|
||
|
if (args.spoiler) {
|
||
5 years ago
|
fileName = `SPOILER_${message.id}_video`;
|
||
5 years ago
|
} else {
|
||
5 years ago
|
fileName = `${message.id}_video`;
|
||
5 years ago
|
}
|
||
6 years ago
|
|
||
5 years ago
|
|
||
5 years ago
|
const Embed = this.client.util.embed()
|
||
5 years ago
|
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
|
||
5 years ago
|
.setAuthor(`Downloaded by ${message.author.username}`, message.author.displayAvatarURL(), link)
|
||
|
.setDescription(args.caption)
|
||
5 years ago
|
.setFooter(`You can get the original video by clicking on the "downloaded by ${message.author.username}" message!`);
|
||
5 years ago
|
|
||
5 years ago
|
|
||
5 years ago
|
let compressEmbed = this.client.util.embed()
|
||
5 years ago
|
.setColor(message.member ? message.member.displayHexColor : 'NAVY')
|
||
5 years ago
|
.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!');
|
||
|
|
||
6 years ago
|
if (link.includes('http') || link.includes('www')) {
|
||
5 years ago
|
let loadingmsg = await message.channel.send('Downloading <a:loadingmin:527579785212329984>');
|
||
|
|
||
5 years ago
|
if (fs.existsSync(`${os.tmpdir()}/${fileName}`)) {
|
||
|
fs.unlink(`${os.tmpdir()}/${fileName}`, (err) => {
|
||
4 years ago
|
if (err) console.error(err);
|
||
![]()
6 years ago
|
});
|
||
|
}
|
||
4 years ago
|
return youtubedl.exec(link, ['--rm-cache-dir', '-o', `${os.tmpdir()}/${fileName}`], {}, async function(err) {
|
||
5 years ago
|
if (err) {
|
||
4 years ago
|
console.error(err.toString());
|
||
5 years ago
|
loadingmsg.delete();
|
||
4 years ago
|
if (err.toString().includes('HTTP Error 429') || err.toString().includes('HTTP Error 403')) {
|
||
4 years ago
|
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.');
|
||
4 years ago
|
}
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
let ext = 'mp4';
|
||
5 years ago
|
|
||
5 years ago
|
if (fs.existsSync(`${os.tmpdir()}/${fileName}`)) {
|
||
|
ext = await filetype.fromFile(`${os.tmpdir()}/${fileName}`);
|
||
|
ext = ext.ext; // This look stupid but hey, it work
|
||
4 years ago
|
if (ext === '3gp') ext = 'mp4'; // Change 3gp file extension to mp4 so discord show the video ( and to stop people from complaining )
|
||
5 years ago
|
fs.renameSync(`${os.tmpdir()}/${fileName}`, `${os.tmpdir()}/${fileName}.${ext}`);
|
||
5 years ago
|
} 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
|
||
5 years ago
|
} else if (fs.existsSync(`${os.tmpdir()}/${fileName}.webm`)) { // Same as mkv
|
||
|
ext = 'webm';
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
let file = fs.statSync(`${os.tmpdir()}/${fileName}.${ext}`);
|
||
5 years ago
|
let fileSize = file.size / 1000000.0;
|
||
![]()
6 years ago
|
|
||
5 years ago
|
//Compress vid if bigger than 8MB
|
||
|
if (fileSize > 8) {
|
||
5 years ago
|
let compressmsg = await message.channel.send(compressEmbed);
|
||
5 years ago
|
loadingmsg.delete();
|
||
6 years ago
|
|
||
5 years ago
|
const options = {
|
||
5 years ago
|
input: `${os.tmpdir()}/${fileName}.${ext}`,
|
||
|
output: `${os.tmpdir()}/${fileName}compressed.${ext}`,
|
||
4 years ago
|
preset: 'Web/Discord Tiny 5 Minutes 240p30'
|
||
5 years ago
|
};
|
||
|
|
||
|
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');
|
||
|
});
|
||
5 years ago
|
|
||
|
|
||
|
let percentComplete;
|
||
|
let eta;
|
||
|
|
||
5 years ago
|
handbrake.on('progress', progress => {
|
||
5 years ago
|
percentComplete = progress.percentComplete;
|
||
|
eta = progress.eta;
|
||
|
console.log(`Percent complete: ${progress.percentComplete}, ETA: ${progress.eta}`);
|
||
5 years ago
|
});
|
||
5 years ago
|
|
||
|
// Every 5 seconds update the compress message with the %
|
||
5 years ago
|
let editmsg = setInterval(() => {
|
||
4 years ago
|
compressEmbed.setDescription(`Ready in ${eta === '' ? 'soon enough' : eta}. ${percentComplete}% complete.`);
|
||
5 years ago
|
compressmsg.edit(compressEmbed);
|
||
5 years ago
|
}, 5000);
|
||
|
|
||
5 years ago
|
handbrake.on('end', async function () {
|
||
5 years ago
|
clearInterval(editmsg);
|
||
5 years ago
|
file = fs.statSync(`${os.tmpdir()}/${fileName}compressed.${ext}`);
|
||
5 years ago
|
fileSize = file.size / 1000000.0;
|
||
|
|
||
5 years ago
|
compressmsg.delete();
|
||
5 years ago
|
message.delete();
|
||
5 years ago
|
|
||
5 years ago
|
if (fileSize > 8) {
|
||
|
return message.channel.send('File too big!');
|
||
|
}
|
||
|
|
||
5 years ago
|
return message.channel.send({embed: Embed, files: [`${os.tmpdir()}/${fileName}compressed.${ext}`]})
|
||
5 years ago
|
.catch(err => {
|
||
|
console.error(err);
|
||
|
return message.channel.send('File too big');
|
||
5 years ago
|
})
|
||
|
.then(() => {
|
||
|
// Delete file after it have been sent
|
||
5 years ago
|
fs.unlinkSync(`${os.tmpdir()}/${fileName}.${ext}`);
|
||
5 years ago
|
});
|
||
|
});
|
||
|
} else {
|
||
5 years ago
|
loadingmsg.delete();
|
||
5 years ago
|
message.delete();
|
||
5 years ago
|
|
||
5 years ago
|
message.channel.send({embed: Embed, files: [`${os.tmpdir()}/${fileName}.${ext}`]})
|
||
5 years ago
|
.catch(err => {
|
||
|
console.error(err);
|
||
5 years ago
|
message.channel.send('File too big');
|
||
|
})
|
||
|
.then(() => {
|
||
|
// Delete file after it have been sent
|
||
5 years ago
|
fs.unlinkSync(`${os.tmpdir()}/${fileName}.${ext}`);
|
||
5 years ago
|
});
|
||
6 years ago
|
}
|
||
6 years ago
|
});
|
||
|
}
|
||
5 years ago
|
return message.channel.send('You need to input a valid link');
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
![]()
6 years ago
|
|
||
6 years ago
|
module.exports = DownloadCommand;
|