Haha-Yes/commands/utility/download.js

92 lines
2.8 KiB
JavaScript
Raw 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');
const { fbuser, fbpasswd } = require('../../config.json');
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',
args: [
{
id: 'link',
type: 'string',
default: 'https://www.youtube.com/watch?v=6n3pFFPSlW4'
}
],
clientPermissions: ['ATTACH_FILES'],
description: {
content: 'Download videos from different website from the link you provided',
usage: '[link]',
examples: ['https://www.youtube.com/watch?v=6n3pFFPSlW4']
}
});
}
2019-01-02 08:09:45 +01:00
async exec(message, args) {
let link = args.link;
2019-01-19 17:23:45 +01:00
let big = false;
2018-12-30 01:20:24 +01:00
2019-01-02 08:09:45 +01:00
if (link.includes('http') || link.includes('www')) {
2019-01-19 17:41:55 +01:00
let video = youtubedl(link, [`--username=${fbuser}`, `--password=${fbpasswd}`]);
2019-01-02 08:09:45 +01:00
video.pipe(fs.createWriteStream('./video.mp4'));
video.on('error', function error(err) {
console.log('error 2:', err);
message.channel.send('An error has occured, I can\'t download from the link you provided.');
});
2019-01-19 17:23:45 +01:00
video.on('info', function(info) {
console.log('Download started');
console.log('filename: ' + info.filename);
console.log('size: ' + info.size);
if (info.size >= 8000000) {
big = true;
}
});
2019-01-02 08:09:45 +01:00
video.on('end', function () {
2019-01-19 17:23:45 +01:00
if (!big) {
message.delete();
return message.channel.send(`Downloaded by ${message.author.username}`, { files: ['./video.mp4'] })
.catch(() => message.channel.send('File too big'));
}
const options = {
input: 'video.mp4',
output: 'videoReady.mp4',
preset: 'General/Gmail Small 10 Minutes 288p30'
};
2019-01-19 17:41:55 +01:00
//Compress vid if bigger than 8MB
2019-01-19 17:23:45 +01:00
hbjs.spawn(options)
2019-01-19 17:29:45 +01:00
.on('start', function() {
2019-01-19 22:34:26 +01:00
message.channel.send('Vid bigger than 8MB compressing now ( This can take a long time!)\nWant it to go faster? Donate to the dev with the donnate command, so i can get a better server and do it faster!');
2019-01-19 17:29:45 +01:00
})
2019-01-19 17:23:45 +01:00
.on('error', err => {
message.channel.send('An error has occured while compressing the video');
console.error(err);
})
.on('progress', progress => {
console.log(
'Percent complete: %s, ETA: %s',
progress.percentComplete,
progress.eta
);
})
.on('end', function () {
message.delete();
message.channel.send(`Downloaded by ${message.author.username}`, { files: ['./videoReady.mp4'] })
.catch(() => message.channel.send('File too big'));
});
message.channel.send('Downloading <a:loadingmin:527579785212329984>').then(msg => {
video.on('end', function () {
msg.delete();
});
});
2019-01-02 08:09:45 +01:00
});
} else {
2019-01-02 10:48:26 +01:00
2019-01-02 08:09:45 +01:00
message.channel.send('You need to input a valid link');
}
}
2018-12-30 01:20:24 +01:00
}
2018-12-30 01:20:24 +01:00
module.exports = DownloadCommand;