2019-10-27 22:32:30 +01:00
const { Command } = require ( 'discord-akairo' ) ;
const YTPGenerator = require ( 'ytpplus-node' ) ;
const os = require ( 'os' ) ;
const fs = require ( 'fs' ) ;
const youtubedl = require ( 'youtube-dl' ) ;
class ytpCommand extends Command {
constructor ( ) {
super ( 'ytp' , {
aliases : [ 'ytp' , 'ytpplus' , 'ytp+' ] ,
category : 'fun' ,
2019-11-09 14:17:29 +01:00
clientPermissions : [ 'ATTACH_FILES' , 'SEND_MESSAGES' ] ,
2019-10-27 22:32:30 +01:00
args : [
{
id : 'add' ,
match : 'flag' ,
flag : [ '--add' ]
} ,
2019-10-27 23:18:09 +01:00
{
id : 'pool' ,
match : 'flag' ,
flag : [ '--pool' ]
} ,
2019-10-27 23:50:33 +01:00
{
id : 'force' ,
match : 'flag' ,
flag : [ '--force' ]
} ,
2019-12-10 22:33:52 +01:00
{
id : 'randomSound' ,
match : 'flag' ,
flag : [ '--randomSound' ]
} ,
{
id : 'randomSoundMute' ,
match : 'flag' ,
flag : [ '--randomSoundMute' ]
} ,
{
id : 'reverse' ,
match : 'flag' ,
flag : [ '--reverse' ]
} ,
{
id : 'chorus' ,
match : 'flag' ,
flag : [ '--chorus' ]
} ,
{
id : 'vibrato' ,
match : 'flag' ,
flag : [ '--vibrato' ]
} ,
{
id : 'highPitch' ,
match : 'flag' ,
flag : [ '--highPitch' ]
} ,
{
id : 'lowPitch' ,
match : 'flag' ,
flag : [ '--lowPitch' ]
} ,
{
id : 'speedUp' ,
match : 'flag' ,
flag : [ '--speedUp' ]
} ,
{
id : 'slowDown' ,
match : 'flag' ,
flag : [ '--slowDown' ]
} ,
{
id : 'dance' ,
match : 'flag' ,
flag : [ '--dance' ]
} ,
{
id : 'squidward' ,
match : 'flag' ,
flag : [ '--squidward' ]
} ,
2019-12-10 22:38:51 +01:00
{
id : 'debug' ,
match : 'flag' ,
flag : [ '--debug' ]
} ,
2019-10-27 22:32:30 +01:00
{
id : 'link' ,
type : 'string'
}
] ,
description : {
2019-10-27 23:50:33 +01:00
content : 'Generate random ytp | --add with a link or attachment to add a video to the pool, only .mp4 work | --pool to see how many vid there is currently in the pool | --force to make the command work outside of nsfw channel BE AWARE THAT IT WON\'T CHANGE THE FINAL RESULT SO NSFW CAN STILL HAPPEN' ,
2019-10-27 22:32:30 +01:00
usage : '' ,
examples : [ '' ]
}
} ) ;
}
async exec ( message , args ) {
2020-04-28 22:57:38 +02:00
let MAX _CLIPS = 5 ;
2019-11-09 14:17:29 +01:00
2019-10-27 23:18:09 +01:00
if ( args . pool ) {
2019-11-09 14:17:29 +01:00
let mp4 = [ ] ;
fs . readdirSync ( './asset/ytp/userVid/' ) . forEach ( file => {
if ( file . endsWith ( 'mp4' ) ) {
mp4 . push ( file ) ;
}
} ) ;
2019-12-10 20:06:58 +01:00
return message . channel . send ( ` There is currently ${ mp4 . length } videos, you can add yours by doing \` \` ${ this . client . commandHandler . prefix [ 0 ] } ytp --add (link or attachment) \` \` ` ) ;
2019-10-27 23:18:09 +01:00
}
2019-10-27 22:32:30 +01:00
if ( args . add ) {
let loadingmsg = await message . channel . send ( 'Downloading <a:loadingmin:527579785212329984>' ) ;
let Attachment = ( message . attachments ) . array ( ) ;
let url = args . link ;
// Get attachment link
if ( Attachment [ 0 ] && ! args . link ) {
url = Attachment [ 0 ] . url ;
}
2019-10-27 22:49:03 +01:00
2019-10-27 22:32:30 +01:00
if ( url ) {
2020-03-31 04:00:28 +02:00
return youtubedl . exec ( url , [ '--no-playlist' , '--max-filesize' , '50m' , '--format=mp4' , '-o' , ` ./asset/ytp/userVid/ ${ message . id } .mp4 ` ] , { } , function ( err , output ) {
2020-03-29 03:53:06 +02:00
console . log ( output ) ;
2019-10-27 22:32:30 +01:00
if ( err ) {
console . error ( err ) ;
loadingmsg . delete ( ) ;
2019-11-09 14:17:29 +01:00
return message . channel . send ( 'An error has occured, I can\'t download from the link you provided. Is it an mp4?' ) ;
2019-10-27 22:32:30 +01:00
} else {
2020-03-29 03:53:06 +02:00
if ( output [ 2 ] ) {
if ( output [ 2 ] . includes ( 'File is larger than max-filesize' ) ) {
loadingmsg . delete ( ) ;
return message . channel . send ( output [ 2 ] ) ;
}
2020-03-29 03:04:21 +02:00
}
2019-11-09 14:17:29 +01:00
let mp4 = [ ] ;
fs . readdirSync ( './asset/ytp/userVid/' ) . forEach ( file => {
if ( file . endsWith ( 'mp4' ) ) {
mp4 . push ( file ) ;
}
} ) ;
2019-10-27 22:51:38 +01:00
loadingmsg . delete ( ) ;
2019-11-09 14:17:29 +01:00
return message . reply ( ` Video sucessfully added to the pool! There is now ${ mp4 . length } videos ` ) ;
2019-10-27 22:32:30 +01:00
}
} ) ;
} else {
2019-10-27 22:51:38 +01:00
loadingmsg . delete ( ) ;
2019-10-27 22:32:30 +01:00
return message . channel . send ( 'You need a valid video link!' ) ;
}
2019-10-27 22:50:50 +01:00
}
2019-10-27 22:32:30 +01:00
2019-11-09 14:17:29 +01:00
2019-12-22 15:52:11 +01:00
if ( ! message . channel . nsfw && ! args . force ) return message . channel . send ( ` Please execute this command in an NSFW channel ( Content might not be NSFW but since the video are user submitted better safe than sorry ) OR do \` \` ${ this . client . commandHandler . prefix [ 0 ] } ytp --force \` \` to make the command work outside of nsfw channel BE AWARE THAT IT WON'T CHANGE THE FINAL RESULT SO NSFW CAN STILL HAPPEN ` ) ;
2019-10-27 22:32:30 +01:00
2019-11-09 14:17:29 +01:00
// Read userVid folder and select random vid and only take .mp4
let mp4 = [ ] ;
2019-10-27 22:32:30 +01:00
let asset = [ ] ;
2019-11-09 14:17:29 +01:00
let files = fs . readdirSync ( './asset/ytp/userVid/' ) ;
// Count number of total vid
files . forEach ( file => {
if ( file . endsWith ( 'mp4' ) ) {
mp4 . push ( file ) ;
}
} ) ;
// Select random vid depending on the amount of MAX_CLIPS
for ( let i = 0 ; i < MAX _CLIPS ; i ++ ) {
let random = Math . floor ( Math . random ( ) * files . length ) ;
let vid = ` ./asset/ytp/userVid/ ${ files [ random ] } ` ;
if ( files [ random ] . endsWith ( 'mp4' ) ) {
if ( ! asset . includes ( vid ) ) {
asset . push ( vid ) ;
2019-10-27 22:32:30 +01:00
}
2019-11-09 14:17:29 +01:00
}
}
2019-10-28 02:47:08 +01:00
2019-12-10 20:06:58 +01:00
let loadingmsg = await message . channel . send ( ` Processing, this can take a **long** time, i'll ping you when i finished <a:loadingmin:527579785212329984> \n Some info: There is currently ${ mp4 . length } videos, you can add yours by doing \` \` ${ this . client . commandHandler . prefix [ 0 ] } ytp --add (link or attachment) \` \` ` ) ;
2019-11-09 14:17:29 +01:00
let options = {
2019-12-10 22:38:51 +01:00
debug : args . debug ,
2020-04-02 13:21:39 +02:00
MAX _STREAM _DURATION : args . link ? args . link : Math . floor ( ( Math . random ( ) * 3 ) + 1 ) , // Random duration of video clip
2019-11-09 14:17:29 +01:00
sources : './asset/ytp/sources/' ,
sounds : './asset/ytp/sounds/' ,
music : './asset/ytp/music/' ,
resources : './asset/ytp/resources/' ,
temp : os . tmpdir ( ) ,
sourceList : asset ,
2020-04-28 22:57:38 +02:00
intro : args . force ? './asset/ytp/intro.mp4' : null ,
2019-11-09 14:17:29 +01:00
outro : './asset/ytp/outro.mp4' , // Need an outro or it won't work
OUTPUT _FILE : ` ${ os . tmpdir ( ) } / ${ message . id } _YTP.mp4 ` ,
MAX _CLIPS : MAX _CLIPS ,
transitions : true ,
2020-04-18 18:23:53 +02:00
showFileNames : true ,
2019-11-09 14:17:29 +01:00
effects : {
2019-12-10 22:33:52 +01:00
effect _RandomSound : ! args . randomSound ,
effect _RandomSoundMute : ! args . randomSoundMute ,
effect _Reverse : ! args . reverse ,
effect _Chorus : ! args . chorus ,
effect _Vibrato : ! args . vibrato ,
effect _HighPitch : ! args . highPitch ,
effect _LowPitch : ! args . lowPitch ,
effect _SpeedUp : ! args . speedUp ,
effect _SlowDown : ! args . slowDown ,
effect _Dance : ! args . dance ,
effect _Squidward : ! args . squidward
2019-11-09 14:17:29 +01:00
}
} ;
2019-10-27 22:32:30 +01:00
2019-11-09 14:17:29 +01:00
new YTPGenerator ( ) . configurateAndGo ( options )
. then ( ( ) => {
loadingmsg . delete ( ) ;
2020-04-28 22:57:38 +02:00
return message . reply ( 'Here is your YTP! Remember, it might contains nsfw!' , { files : [ ` ${ os . tmpdir ( ) } / ${ message . id } _YTP.mp4 ` ] } )
2020-04-18 18:23:53 +02:00
. catch ( err => {
console . error ( err ) ;
2019-11-09 14:17:29 +01:00
return message . channel . send ( 'Whoops, look like the vid might be too big for discord, my bad, please try again' ) ;
} ) ;
} )
. catch ( err => {
console . error ( err ) ;
loadingmsg . delete ( ) ;
return message . reply ( 'Oh no! An error has occured!' ) ;
} ) ;
2019-10-27 22:32:30 +01:00
}
}
2020-04-28 22:57:38 +02:00
module . exports = ytpCommand ;