2017-08-03 01:48:16 +00:00
const discord = require ( 'discord.js' ) ;
const fs = require ( 'fs' ) ;
const path = require ( 'path' ) ;
const config = require ( 'config' ) ;
const schedule = require ( 'node-schedule' ) ;
2016-12-08 03:52:37 +00:00
2017-08-03 01:48:16 +00:00
const logger = require ( './logging.js' ) ;
const app = require ( './app.js' ) ;
const data = require ( './data.js' ) ;
2016-12-08 03:52:37 +00:00
var cachedModules = [ ] ;
2016-12-31 05:49:12 +00:00
var cachedTriggers = [ ] ;
2016-12-08 03:52:37 +00:00
var client = new discord . Client ( ) ;
2017-09-24 19:44:49 +00:00
process . on ( 'unhandledRejection' , function onError ( err ) {
logger . error ( err ) ;
} ) ;
// Verify configuration options are set properly.
if ( ! config . logChannel ) { throw 'Log Channel is missing from the configuration file.' ; }
if ( ! config . clientLoginToken ) { throw 'Client Login Token is missing from the configuration file.' ; }
2016-12-08 03:52:37 +00:00
function findArray ( haystack , arr ) {
return arr . some ( function ( v ) {
return haystack . indexOf ( v ) >= 0 ;
} ) ;
} ;
client . on ( 'ready' , ( ) => {
// Initalize app channels.
app . logChannel = client . channels . get ( config . logChannel ) ;
app . guild = app . logChannel . guild ;
2017-03-31 01:17:21 +00:00
logger . info ( 'Bot is now online and connected to server.' ) ;
2016-12-08 03:52:37 +00:00
} ) ;
2017-07-20 01:45:58 +00:00
client . on ( "guildMemberAdd" , ( member ) => {
app . stats . joins += 1 ;
} ) ;
client . on ( "guildMemberRemove" , ( member ) => {
app . stats . leaves += 1 ;
} ) ;
// Output the stats for app.stats every 24 hours.
2017-07-20 02:05:02 +00:00
// Server is in UTC mode, 11:30 EST would be 03:30 UTC.
schedule . scheduleJob ( { hour : 3 , minute : 30 } , function ( ) {
2017-07-20 01:45:58 +00:00
logger . info ( ` Here are today's stats for ${ ( new Date ( ) ) . toLocaleDateString ( ) } ! ${ app . stats . joins } users have joined, ${ app . stats . leaves } users have left, ${ app . stats . warnings } warnings have been issued. ` ) ;
app . logChannel . sendMessage ( ` Here are today's stats for ${ ( new Date ( ) ) . toLocaleDateString ( ) } ! ${ app . stats . joins } users have joined, ${ app . stats . leaves } users have left, ${ app . stats . warnings } warnings have been issued. ` ) ;
// Clear the stats for the day.
app . stats . joins = 0 ;
app . stats . leaves = 0 ;
app . stats . warnings = 0 ;
} ) ;
2016-12-08 03:52:37 +00:00
client . on ( 'message' , message => {
if ( message . author . bot && message . content . startsWith ( '.ban' ) == false ) { return ; }
if ( message . guild == null ) {
// We want to log PM attempts.
logger . info ( ` ${ message . author . username } ${ message . author } [PM]: ${ message . content } ` ) ;
app . logChannel . sendMessage ( ` ${ message . author } [PM]: ${ message . content } ` ) ;
message . reply ( config . pmReply ) ;
return ;
}
2017-07-09 03:53:01 +00:00
logger . verbose ( ` ${ message . author . username } ${ message . author } [Channel: ${ message . channel . name } ${ message . channel } ]: ${ message . content } ` ) ;
2016-12-08 03:52:37 +00:00
if ( message . content . startsWith ( config . commandPrefix ) ) {
let cmd = message . content . split ( ' ' ) [ 0 ] . slice ( 1 ) ;
// Check by the name of the command.
let cachedModule = cachedModules [ ` ${ cmd } .js ` ] ;
let cachedModuleType = 'Command' ;
// Check by the quotes in the configuration.
if ( cachedModule == null ) { cachedModule = config . quotes [ cmd ] ; cachedModuleType = 'Quote' ; }
if ( cachedModule ) {
// Check access permissions.
if ( cachedModule . roles != undefined && findArray ( message . member . roles . map ( function ( x ) { return x . name ; } ) , cachedModule . roles ) == false ) {
app . logChannel . sendMessage ( ` ${ message . author } attempted to use admin command: ${ message . content } ` ) ;
2017-08-02 23:11:05 +00:00
logger . info ( ` ${ message . author . username } ${ message . author } attempted to use admin command: ${ message . content } ` ) ;
2016-12-08 03:52:37 +00:00
return false ;
}
2017-03-31 01:29:52 +00:00
logger . info ( ` ${ message . author . username } ${ message . author } [Channel: ${ message . channel } ] executed command: ${ message . content } ` ) ;
2016-12-08 03:52:37 +00:00
message . delete ( ) ;
2017-01-15 17:37:43 +00:00
try {
if ( cachedModuleType == 'Command' ) {
cachedModule . command ( message ) ;
} else if ( cachedModuleType == 'Quote' ) {
cachedModules [ 'quote.js' ] . command ( message , cachedModule . reply ) ;
}
} catch ( err ) { logger . error ( err ) ; }
2016-12-08 03:52:37 +00:00
2017-04-07 23:33:03 +00:00
// Warn after running command?
2017-01-15 17:37:43 +00:00
try {
// Check if the command requires a warning.
if ( cmd != 'warn' && cachedModule . warn == true ) {
2017-04-07 23:33:03 +00:00
// Access check to see if the user has privilages to warn.
let warnCommand = cachedModules [ 'warn.js' ] ;
if ( findArray ( message . member . roles . map ( function ( x ) { return x . name ; } ) , warnCommand . roles ) ) {
// They are allowed to warn because they are in warn's roles.
warnCommand . command ( message ) ;
}
2017-01-15 17:37:43 +00:00
}
} catch ( err ) { logger . error ( err ) ; }
2017-04-07 23:33:03 +00:00
2016-12-08 03:52:37 +00:00
} else {
// Not a valid command.
}
2017-01-29 20:50:10 +00:00
} else if ( message . author . bot == false ) {
2016-12-31 05:49:12 +00:00
// This is a normal channel message.
cachedTriggers . forEach ( function ( trigger ) {
if ( trigger . roles == undefined || findArray ( message . member . roles . map ( function ( x ) { return x . name ; } ) , trigger . roles ) ) {
if ( trigger . trigger ( message ) == true ) {
2017-08-02 23:11:05 +00:00
logger . debug ( ` ${ message . author . username } ${ message . author } [Channel: ${ message . channel } ] triggered: ${ message . content } ` ) ;
2017-01-15 17:37:43 +00:00
try {
trigger . execute ( message ) ;
} catch ( err ) { logger . error ( err ) ; }
2016-12-31 05:49:12 +00:00
}
}
} ) ;
2016-12-08 03:52:37 +00:00
}
2017-03-31 01:17:21 +00:00
} ) ;
2016-12-08 03:52:37 +00:00
2017-03-31 01:17:21 +00:00
// Cache all command modules.
2017-03-31 01:18:23 +00:00
cachedModules = [ ] ;
2017-03-31 01:17:21 +00:00
require ( "fs" ) . readdirSync ( './commands/' ) . forEach ( function ( file ) {
// Load the module if it's a script.
if ( path . extname ( file ) == '.js' ) {
2017-04-07 23:36:45 +00:00
if ( file . includes ( '.disabled' ) ) {
logger . info ( ` Did not load disabled module: ${ file } ` ) ;
} else {
logger . info ( ` Loaded module: ${ file } ` ) ;
cachedModules [ file ] = require ( ` ./commands/ ${ file } ` ) ;
}
2017-03-31 01:17:21 +00:00
}
2016-12-08 03:52:37 +00:00
} ) ;
2017-03-31 01:17:21 +00:00
// Cache all triggers.
2017-03-31 01:18:23 +00:00
cachedTriggers = [ ] ;
2017-03-31 01:17:21 +00:00
require ( "fs" ) . readdirSync ( './triggers/' ) . forEach ( function ( file ) {
// Load the trigger if it's a script.
if ( path . extname ( file ) == '.js' ) {
2017-04-07 23:36:45 +00:00
if ( file . includes ( '.disabled' ) ) {
logger . info ( ` Did not load disabled trigger: ${ file } ` ) ;
} else {
logger . info ( ` Loaded trigger: ${ file } ` ) ;
cachedTriggers . push ( require ( ` ./triggers/ ${ file } ` ) ) ;
}
2017-03-31 01:17:21 +00:00
}
} ) ;
data . readWarnings ( ) ;
data . readBans ( ) ;
2017-09-24 19:44:49 +00:00
client . login ( config . clientLoginToken ) ;
logger . info ( 'Startup completed. Established connection to Discord.' ) ;