2020-08-18 04:58:36 +00:00
// The MESSAGE event runs anytime a message is received
// Note that due to the binding of client to every event, every event
// goes `client, other, args` when this function is run.
module . exports = class {
constructor ( client ) {
this . client = client ;
}
async run ( message ) {
if ( message . author . bot ) return ;
2020-10-10 08:36:49 +00:00
const userPrefix = await this . client . db . getUserKey ( message . author . id , 'prefix' ) ;
const userBlacklisted = await this . client . db . getUserKey ( message . author . id , 'blacklisted' ) ;
2020-08-18 04:58:36 +00:00
2020-10-10 08:36:49 +00:00
const prefixes = [ userPrefix ] ;
2020-10-10 02:30:24 +00:00
2020-10-11 04:03:54 +00:00
if ( message . guild ) prefixes . push ( await this . client . db . getGuildKey ( message . guild . id , 'prefix' ) ) ;
2020-10-10 02:30:24 +00:00
prefixes . push ( ` <@ ${ this . client . user . id } > ` , ` <@! ${ this . client . user . id } > ` )
let prefix ;
for ( const thisPrefix of prefixes ) {
if ( message . content . startsWith ( thisPrefix ) ) prefix = thisPrefix ;
} ;
if ( message . content . indexOf ( prefix ) !== 0 ) return ;
if ( prefix === ` <@ ${ this . client . user . id } > ` || prefix === ` <@! ${ this . client . user . id } > ` ) {
message . prefix = '@Woomy ' ;
} else {
message . prefix = prefix ;
} ;
2020-10-11 04:03:54 +00:00
2020-10-10 05:26:06 +00:00
// Naughty users can't run commands!
2020-10-11 04:03:54 +00:00
if ( message . guild ) {
const check = await this . client . db . getMemberKey ( message . guild . id , message . author . id , 'blacklisted' )
if ( check === true ) { return } ;
} ;
if ( userBlacklisted ) return ;
2020-10-10 05:26:06 +00:00
2020-10-10 02:30:24 +00:00
const args = message . content . slice ( prefix . length ) . trim ( ) . split ( / +/g ) ;
2020-08-18 04:58:36 +00:00
const command = args . shift ( ) . toLowerCase ( ) ;
// Cache uncached members
if ( message . guild && ! message . member ) await message . guild . fetchMember ( message . author ) ;
const cmd = this . client . commands . get ( command ) || this . client . commands . get ( this . client . aliases . get ( command ) ) ;
if ( ! cmd ) return ;
2020-10-10 02:30:24 +00:00
if ( message . guild ) {
if ( ! message . channel . permissionsFor ( this . client . user ) . has ( 'SEND_MESSAGES' ) ) {
try {
return message . author . send ( ` I don't have permission to speak in \` # ${ message . channel . name } \` , Please ask a moderator to give me the send messages permission! ` )
} catch ( err ) { } ;
} ;
/ * N E E D A W A Y T O S T O R E A R R A Y S I N A H A S H
if ( data . guild . disabledCommands . includes ( cmd . help . name ) ) {
if ( data . guild . systemNotice . enabled === true ) {
return message . channel . send ( 'This command has been disabled in this server.' ) ;
} ;
} ;
if ( data . guild . disabledCategories . includes ( cmd . help . category ) ) {
if ( data . guild . systemNotice . enabled === true ) {
return message . channel . send ( 'The category this command is apart of has been disabled in this server.' ) ;
} ;
} ;
* /
} ;
if ( cmd && cmd . conf . enabled === false ) {
return message . channel . send ( 'This command has been disabled by my developers.' ) ;
} ;
2020-10-08 02:05:14 +00:00
if ( cmd && cmd . conf . devOnly && this . client . functions . isDeveloper ( message . author . id ) !== true ) {
2020-08-18 04:58:36 +00:00
return message . channel . send ( "devs only!" ) ;
2020-10-10 03:22:39 +00:00
} ;
2020-10-10 02:30:24 +00:00
if ( cmd && ! message . guild && cmd . conf . guildOnly === true ) {
2020-08-18 04:58:36 +00:00
return message . channel . send ( "This command is unavailable via private message. Please run this command in a guild." ) ;
2020-10-10 03:22:39 +00:00
} ;
// Permission handler, for both Woomy and the user
if ( message . guild ) {
2020-10-10 03:59:50 +00:00
// User
let missingUserPerms = new Array ( ) ;
cmd . conf . userPerms . forEach ( ( p ) => {
if ( ! message . channel . permissionsFor ( message . member ) . has ( p ) ) missingUserPerms . push ( p ) ;
2020-10-10 03:22:39 +00:00
} ) ;
if ( missingUserPerms . length > 0 ) {
missingUserPerms = '`' + ( missingUserPerms . join ( '`, `' ) ) + '`'
2020-10-10 03:59:50 +00:00
return message . channel . send ( ` You don't have sufficient permissions to run this command! Missing: ${ missingUserPerms } ` ) ;
2020-10-10 03:22:39 +00:00
} ;
2020-10-10 03:59:50 +00:00
// Bot
let missingBotPerms = new Array ( ) ;
cmd . conf . botPerms . forEach ( ( p ) => {
if ( ! message . channel . permissionsFor ( message . guild . member ( this . client . user ) ) . has ( p ) ) missingBotPerms . push ( p ) ;
2020-10-10 03:22:39 +00:00
} ) ;
if ( missingBotPerms . length > 0 ) {
2020-10-10 03:59:50 +00:00
missingBotPerms = '`' + ( missingBotPerms . join ( '`, `' ) ) + '`'
return message . channel . send ( ` I can't run this command because I'm missing these permissions: ${ missingBotPerms } ` ) ;
2020-10-10 03:22:39 +00:00
} ;
2020-10-10 03:59:50 +00:00
}
2020-08-18 04:58:36 +00:00
2020-10-10 02:30:24 +00:00
// Cooldown
2020-10-17 01:15:38 +00:00
if ( this . client . cooldowns . get ( cmd . help . name ) . has ( message . author . id ) ) {
const init = this . client . cooldowns . get ( command ) . get ( message . author . id ) ;
2020-10-10 02:30:24 +00:00
const curr = new Date ( ) ;
const diff = Math . round ( ( curr - init ) / 1000 ) ;
const time = cmd . conf . cooldown / 1000 ;
return message . reply ( ` this command is on cooldown! You'll be able to use it again in ${ time - diff } seconds. ` ) ;
} else {
2020-10-17 01:15:38 +00:00
this . client . cooldowns . get ( cmd . help . name ) . set ( message . author . id , new Date ( ) ) ;
2020-10-10 02:30:24 +00:00
setTimeout ( ( ) => {
2020-10-17 01:15:38 +00:00
this . client . cooldowns . get ( cmd . help . name ) . delete ( message . author . id ) ;
2020-10-10 02:30:24 +00:00
} , this . client . commands . get ( cmd . help . name ) . conf . cooldown ) ;
} ;
2020-08-18 04:58:36 +00:00
message . flags = [ ] ;
while ( args [ 0 ] && args [ 0 ] [ 0 ] === "-" ) {
message . flags . push ( args . shift ( ) . slice ( 1 ) ) ;
}
2020-10-10 08:36:49 +00:00
cmd . run ( message , args ) ;
2020-08-18 04:58:36 +00:00
this . client . logger . cmd ( ` Command ran: ${ message . content } ` ) ;
// TODO: Command caching if it"s worth it
}
} ;