2018-12-30 01:20:24 +01:00
const { Command } = require ( 'discord-akairo' ) ;
2018-11-18 17:58:08 +01:00
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' ) ;
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' ,
2019-06-23 03:41:59 +02:00
prompt : {
start : 'Send the link of wich video you want to download' ,
}
2019-03-26 01:19:43 +01:00
} ,
2019-07-11 00:31:15 +02:00
{
id : 'spoiler' ,
match : 'flag' ,
flag : [ '--spoil' , '--spoiler' , '-s' ]
2019-01-02 08:09:45 +01:00
}
] ,
clientPermissions : [ 'ATTACH_FILES' ] ,
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-01-02 08:09:45 +01:00
usage : '[link]' ,
examples : [ 'https://www.youtube.com/watch?v=6n3pFFPSlW4' ]
}
} ) ;
}
2018-11-18 17:58:08 +01:00
2019-01-02 08:09:45 +01:00
async exec ( message , args ) {
let link = args . link ;
2019-07-11 00:31:15 +02:00
let fileName ;
if ( args . spoiler ) {
2019-09-13 19:06:29 +02:00
fileName = ` SPOILER_ ${ message . author . id } _video ` ;
2019-07-11 00:31:15 +02:00
} else {
2019-09-13 19:06:29 +02:00
fileName = ` ${ message . author . id } _video ` ;
2019-07-11 00:31:15 +02:00
}
2018-12-30 01:20:24 +01:00
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>' ) ;
2019-10-24 18:15:21 +02:00
if ( fs . existsSync ( ` ${ os . tmpdir ( ) } / ${ fileName } .mp4 ` ) ) {
fs . unlink ( ` ${ os . tmpdir ( ) } / ${ fileName } .mp4 ` , ( err ) => {
if ( err ) ;
2019-03-26 01:19:43 +01:00
} ) ;
}
2019-10-24 18:15:21 +02:00
return youtubedl . exec ( args . link , [ '-o' , ` ${ os . tmpdir ( ) } / ${ fileName } .mp4 ` ] , { } , async function ( err , output ) {
if ( err ) {
console . error ( err ) ;
loadingmsg . delete ( ) ;
return message . channel . send ( 'An error has occured, I can\'t download from the link you provided.' ) ;
}
console . log ( output . join ( '\n' ) ) ;
2019-03-26 01:19:43 +01:00
2019-10-24 18:15:21 +02:00
let file = fs . statSync ( ` ${ os . tmpdir ( ) } / ${ fileName } .mp4 ` ) ;
let fileSize = file . size / 1000000.0 ;
2019-03-26 01:19:43 +01:00
2019-10-24 18:15:21 +02:00
console . log ( fileSize ) ;
2019-01-23 02:18:21 +01:00
2019-10-24 18:15:21 +02:00
//Compress vid if bigger than 8MB
if ( fileSize > 8 ) {
console . log ( 'file bigger than 8MB' ) ;
let compressmsg = await message . channel . send ( 'Video bigger than 8MB compressing now <a:loadingmin:527579785212329984> (This can take a long time!)\nWant it to go faster? Donate to the dev with the donate command, so i can get a better server and do it faster!' ) ;
loadingmsg . delete ( ) ;
2019-01-23 02:18:21 +01:00
2019-10-24 18:15:21 +02:00
const options = {
input : ` ${ os . tmpdir ( ) } / ${ fileName } .mp4 ` ,
output : ` ${ os . tmpdir ( ) } / ${ fileName } compressed.mp4 ` ,
preset : 'General/Gmail Small 10 Minutes 288p30'
} ;
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' ) ;
} ) ;
handbrake . on ( 'progress' , progress => {
console . log (
'Percent complete: %s, ETA: %s' ,
progress . percentComplete ,
progress . eta
) ;
} ) ;
handbrake . on ( 'end' , async function ( ) {
message . delete ( ) ;
compressmsg . delete ( ) ;
return message . channel . send ( ` Downloaded by ${ message . author . username } ` , { files : [ ` ${ os . tmpdir ( ) } / ${ fileName } compressed.mp4 ` ] } )
. catch ( err => {
console . error ( err ) ;
compressmsg . delete ( ) ;
return message . channel . send ( 'File too big' ) ;
} ) ;
} ) ;
} else {
console . log ( 'file smaller than 8MB' ) ;
2019-01-19 17:23:45 +01:00
message . delete ( ) ;
2019-10-16 17:47:16 +02:00
loadingmsg . delete ( ) ;
2019-09-13 19:06:29 +02:00
return message . channel . send ( ` Downloaded by ${ message . author . username } ` , { files : [ ` ${ os . tmpdir ( ) } / ${ fileName } .mp4 ` ] } )
2019-07-11 00:31:15 +02:00
. catch ( err => {
console . error ( err ) ;
2019-10-16 17:47:16 +02:00
loadingmsg . delete ( ) ;
2019-10-24 18:15:21 +02:00
return message . channel . send ( 'File too big' ) ;
2019-07-11 00:31:15 +02:00
} ) ;
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-11-18 17:58:08 +01:00
2018-12-30 01:20:24 +01:00
module . exports = DownloadCommand ;