2020-06-17 18:42:52 +02:00
const { Command } = require ( 'discord-akairo' ) ;
const downloader = require ( '../../utils/download' ) ;
const compress = require ( '../../utils/compress' ) ;
2020-08-25 02:09:16 +02:00
const { proxy } = require ( '../../config.json' ) ;
2020-06-17 18:42:52 +02:00
const os = require ( 'os' ) ;
const fs = require ( 'fs' ) ;
class DownloadCommand extends Command {
constructor ( ) {
2020-06-18 01:36:09 +02:00
super ( 'download' , {
2020-06-18 00:36:48 +02:00
aliases : [ 'download' , 'dl' ] ,
2020-06-17 18:42:52 +02:00
category : 'utility' ,
clientPermissions : [ 'SEND_MESSAGES' , 'EMBED_LINKS' , 'ATTACH_FILES' ] ,
args : [
{
id : 'link' ,
2020-07-16 09:23:50 +02:00
type : 'url' ,
2020-06-17 18:42:52 +02:00
prompt : {
2020-07-16 09:23:50 +02:00
start : 'Please send the URL of which video you want to download. Say `cancel` to stop the command' ,
2020-08-25 03:11:01 +02:00
retry : 'Please send a valid URL of the video you want to download. Say `cancel` to stop the command' ,
optional : true
2020-07-16 09:23:50 +02:00
} ,
2020-06-17 18:42:52 +02:00
} ,
{
id : 'caption' ,
type : 'string' ,
match : 'rest'
} ,
{
id : 'spoiler' ,
match : 'flag' ,
flag : [ '--spoil' , '--spoiler' , '-s' ]
2020-08-25 02:09:16 +02:00
} ,
{
id : 'proxy' ,
2020-08-25 03:11:01 +02:00
match : 'option' ,
flag : [ '--proxy' ] ,
} ,
{
id : 'listproxy' ,
2020-08-25 02:09:16 +02:00
match : 'flag' ,
2020-08-25 03:11:01 +02:00
flag : [ '--listproxy' , '--proxylist' ]
2020-06-17 18:42:52 +02:00
}
] ,
description : {
2020-09-06 22:53:17 +02:00
content : 'Download videos from different website from the link you provided\n`-s` to make the video as a spoiler\n`--proxy #` to select a proxy\n`--listproxy` to see a list of proxy' ,
2020-06-17 18:42:52 +02:00
usage : '[link] [caption]' ,
2020-08-25 17:21:01 +02:00
examples : [ 'https://www.youtube.com/watch?v=6n3pFFPSlW4 Look at this funny gnome' , 'https://www.youtube.com/watch?v=6n3pFFPSlW4 --proxy 1' , '--listproxy' ]
2020-06-17 18:42:52 +02:00
}
} ) ;
}
async exec ( message , args ) {
2020-08-25 03:11:01 +02:00
if ( args . listproxy ) {
let proxys = [ ] ;
let i = 0 ;
proxy . forEach ( proxy => {
i ++ ;
2020-08-25 03:14:32 +02:00
proxys . push ( ` [ ${ i } ] ${ proxy . hideip ? '[IP HIDDEN]' : proxy . ip . substring ( 0 , proxy . ip . length - 5 ) } - ${ proxy . country } ` ) ;
2020-08-25 03:11:01 +02:00
} ) ;
const Embed = this . client . util . embed ( )
. setColor ( message . member ? message . member . displayHexColor : 'NAVY' )
. setTitle ( 'List of available proxy' )
. setDescription ( proxys . join ( '\n' ) )
. setFooter ( 'You can help me get more proxy by either donating to me or providing a proxy for me' ) ;
return message . channel . send ( Embed ) ;
}
2020-08-25 03:12:17 +02:00
if ( ! args . link ) return message . channel . send ( 'Please try again with a valid URL.' ) ;
2020-06-17 18:42:52 +02:00
let loadingmsg = await message . channel . send ( 'Downloading <a:loadingmin:527579785212329984>' ) ;
let filename = ` ${ message . id } _video ` ;
2020-08-25 03:11:01 +02:00
if ( args . proxy ) {
args . proxy = args . proxy - 1 ;
if ( ! proxy [ args . proxy ] ) args . proxy = 0 ;
}
2020-06-17 18:42:52 +02:00
if ( args . spoiler ) {
filename = ` SPOILER_ ${ message . id } _video ` ;
}
const Embed = this . client . util . embed ( )
. setColor ( message . member ? message . member . displayHexColor : 'NAVY' )
. setAuthor ( ` Downloaded by ${ message . author . username } ` , message . author . displayAvatarURL ( ) , args . link )
. setDescription ( args . caption ? args . caption : '' )
. setFooter ( ` You can get the original video by clicking on the "downloaded by ${ message . author . username } " message! ` ) ;
2020-08-25 03:32:58 +02:00
downloader ( args . link . href , args . proxy != null ? [ '--proxy' , proxy [ args . proxy ] . ip ] : null , ` ${ os . tmpdir ( ) } / ${ filename } .mp4 ` )
2020-07-16 09:23:50 +02:00
. on ( 'error' , async err => {
2020-09-06 22:53:17 +02:00
if ( err . includes ( 'HTTP Error 429: Too Many Requests' ) ) return message . channel . send ( '`HTTP Error 429: Too Many Requests.`\nThe website you tried to download from probably has the bot blocked, you can try again with the `--proxy #` option ( can see the list of proxy with --listproxy ) and hope it work.' ) ;
2020-06-17 18:42:52 +02:00
return message . channel . send ( err , { code : true } ) ;
} )
2020-07-11 01:23:58 +02:00
. on ( 'end' , async output => {
2020-06-17 18:42:52 +02:00
let file = fs . statSync ( output ) ;
let fileSize = file . size / 1000000.0 ;
if ( fileSize > 8 ) {
2020-07-16 09:23:50 +02:00
loadingmsg . delete ( ) ;
2020-06-17 18:42:52 +02:00
let compressEmbed = this . client . util . embed ( )
. 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!' ) ;
let compressmsg = await message . channel . send ( compressEmbed ) ;
let handbrake = compress ( output , ` ${ os . tmpdir ( ) } / ${ filename } compressed.mp4 ` ) ;
let percentComplete ;
let eta ;
handbrake . on ( 'progress' , progress => {
percentComplete = progress . percentComplete ;
eta = progress . eta ;
} ) ;
// Every 5 seconds update the compress message with the %
let editmsg = setInterval ( ( ) => {
compressEmbed . setDescription ( ` Ready in ${ eta === '' ? 'soon enough' : eta } . ${ percentComplete } % complete. ` ) ;
compressmsg . edit ( compressEmbed ) ;
} , 5000 ) ;
handbrake . on ( 'err' , ( err ) => {
clearInterval ( editmsg ) ;
compressmsg . delete ( ) ;
return message . channel . send ( err , { code : true } ) ;
} ) ;
handbrake . on ( 'end' , ( output ) => {
clearInterval ( editmsg ) ;
file = fs . statSync ( output ) ;
fileSize = file . size / 1000000.0 ;
if ( fileSize > 8 ) return message . channel . send ( 'End results is too big for discord.' ) ;
return message . channel . send ( { embed : Embed , files : [ output ] } )
. catch ( err => {
2020-07-16 09:23:50 +02:00
compressmsg . delete ( ) ;
2020-06-17 18:42:52 +02:00
console . error ( err ) ;
2020-07-16 09:23:50 +02:00
return message . channel . send ( ` ${ err . name } : ${ err . message } ${ err . message === 'Request entity too large' ? 'The file size is too big' : '' } ` ) ;
2020-06-17 18:42:52 +02:00
} )
. then ( ( ) => {
2020-07-16 09:23:50 +02:00
compressmsg . delete ( ) ;
2020-06-17 18:42:52 +02:00
// Delete file after it has been sent
fs . unlinkSync ( output ) ;
message . delete ( ) ;
} ) ;
} ) ;
} else {
return message . channel . send ( { embed : Embed , files : [ output ] } )
. catch ( err => {
2020-07-16 09:23:50 +02:00
loadingmsg . delete ( ) ;
2020-06-17 18:42:52 +02:00
console . error ( err ) ;
2020-07-16 09:23:50 +02:00
return message . channel . send ( ` ${ err . name } : ${ err . message } ${ err . message === 'Request entity too large' ? 'The file size is too big' : '' } ` ) ;
2020-06-17 18:42:52 +02:00
} )
. then ( ( ) => {
2020-07-16 09:23:50 +02:00
loadingmsg . delete ( ) ;
2020-06-17 18:42:52 +02:00
// Delete file after it has been sent
fs . unlinkSync ( output ) ;
message . delete ( ) ;
} ) ;
}
} ) ;
}
}
module . exports = DownloadCommand ;