2019-09-13 20:02:41 +00:00
const collections = require ( "./collections.js" ) ;
2019-11-30 02:00:14 +00:00
const logger = require ( "./logger.js" ) ;
2019-09-13 20:02:41 +00:00
2019-11-30 02:00:14 +00:00
// load command into memory
2020-06-27 21:34:31 +00:00
exports . load = async ( command , soundStatus ) => {
2019-09-13 20:02:41 +00:00
const props = require ( ` ../commands/ ${ command } ` ) ;
2019-11-30 02:00:14 +00:00
if ( props . requires === "google" && process . env . GOOGLE === "" ) return logger . log ( "info" , ` Google info not provided in config, skipped loading command ${ command } ... ` ) ;
if ( props . requires === "cat" && process . env . CAT === "" ) return logger . log ( "info" , ` Cat API info not provided in config, skipped loading command ${ command } ... ` ) ;
if ( props . requires === "mashape" && process . env . MASHAPE === "" ) return logger . log ( "info" , ` Mashape/RapidAPI info not provided in config, skipped loading command ${ command } ... ` ) ;
if ( props . requires === "twitter" && process . env . TWITTER === "false" ) return logger . log ( "info" , ` Twitter bot disabled, skipped loading command ${ command } ... ` ) ;
2020-06-27 21:34:31 +00:00
if ( props . requires === "sound" && soundStatus ) return logger . log ( "info" , ` Failed to connect to some Lavalink nodes, skipped loading command ${ command } ... ` ) ;
2019-09-13 20:02:41 +00:00
collections . commands . set ( command . split ( "." ) [ 0 ] , props . run ) ;
2019-12-02 20:47:22 +00:00
collections . info . set ( command . split ( "." ) [ 0 ] , {
category : props . category ,
description : props . help ,
2019-12-05 16:58:46 +00:00
aliases : props . aliases ,
params : props . params
2019-12-02 20:47:22 +00:00
} ) ;
2019-09-13 20:02:41 +00:00
if ( props . aliases ) {
2020-03-10 22:24:57 +00:00
for ( const alias of props . aliases ) {
2019-09-13 20:02:41 +00:00
collections . aliases . set ( alias , command . split ( "." ) [ 0 ] ) ;
2020-03-10 22:24:57 +00:00
}
2019-09-13 20:02:41 +00:00
}
return false ;
} ;
2019-11-30 02:00:14 +00:00
// unload command from memory
2019-09-13 20:02:41 +00:00
exports . unload = async ( command ) => {
let cmd ;
if ( collections . commands . has ( command ) ) {
cmd = collections . commands . get ( command ) ;
} else if ( collections . aliases . has ( command ) ) {
cmd = collections . commands . get ( collections . aliases . get ( command ) ) ;
}
if ( ! cmd ) return ` The command \` ${ command } \` doesn't seem to exist, nor is it an alias. ` ;
const mod = require . cache [ require . resolve ( ` ../commands/ ${ command } ` ) ] ;
delete require . cache [ require . resolve ( ` ../commands/ ${ command } .js ` ) ] ;
for ( let i = 0 ; i < mod . parent . children . length ; i ++ ) {
if ( mod . parent . children [ i ] === mod ) {
mod . parent . children . splice ( i , 1 ) ;
break ;
}
}
return false ;
} ;