2019-09-13 20:02:41 +00:00
const client = require ( "../utils/client.js" ) ;
const database = require ( "../utils/database.js" ) ;
const misc = require ( "../utils/misc.js" ) ;
const logger = require ( "../utils/logger.js" ) ;
const collections = require ( "../utils/collections.js" ) ;
// run when someone sends a message
module . exports = async ( message ) => {
// ignore dms and other bots
if ( message . author . bot ) return ;
if ( ! message . channel . guild ) return ;
2019-12-16 23:14:29 +00:00
// don't run command if bot can't send messages
2019-12-16 23:55:08 +00:00
if ( ! message . channel . guild . members . get ( client . user . id ) . permission . has ( "sendMessages" ) || ! message . channel . permissionsOf ( client . user . id ) . has ( "sendMessages" ) ) return ;
2019-12-16 23:14:29 +00:00
2019-09-13 20:02:41 +00:00
// prefix can be a mention or a set of special characters
const prefixMention = new RegExp ( ` ^<@!? ${ client . user . id } > ` ) ;
2019-11-05 15:52:46 +00:00
const guildConf = ( await database . guilds . find ( { id : message . channel . guild . id } ) . exec ( ) ) [ 0 ] ;
2019-09-13 20:02:41 +00:00
const prefix = prefixMention . test ( message . content ) ? message . content . match ( prefixMention ) [ 0 ] : guildConf . prefix ;
// ignore other stuff
2019-11-23 23:23:28 +00:00
if ( message . content . startsWith ( prefix ) === false && ! message . mentions . includes ( client . user ) && message . channel . id !== "573553254575898626" ) return ;
2019-09-13 20:02:41 +00:00
// funny stuff
if ( message . channel . id === "573553254575898626" && message . channel . guild . id === "433408970955423765" ) {
2019-11-05 15:52:46 +00:00
const generalChannel = client . guilds . get ( "631290275456745502" ) . channels . get ( "631290275888627713" ) ;
2019-09-13 20:02:41 +00:00
if ( message . attachments . length !== 0 ) {
const attachments = [ ] ;
for ( const attachment of message . attachments ) {
const res = await require ( "node-fetch" ) ( attachment . url ) ;
attachments . push ( { file : await res . buffer ( ) , name : attachment . filename } ) ;
}
await client . createMessage ( generalChannel . id , message . content , attachments ) ;
} else {
await client . createMessage ( generalChannel . id , message . content ) ;
}
}
// separate commands and args
const escapedPrefix = misc . regexEscape ( prefix ) ;
const prefixRegex = new RegExp ( ` ^( ${ escapedPrefix } ) ` ) ;
2020-01-13 16:31:01 +00:00
const content = message . content . replace ( prefixRegex , "" ) . trim ( ) ;
const args = content . split ( / +/g ) ;
2019-09-13 20:02:41 +00:00
const command = args . shift ( ) . toLowerCase ( ) ;
// check if command exists
const cmd = collections . commands . get ( command ) || collections . commands . get ( collections . aliases . get ( command ) ) ;
if ( ! cmd ) return ;
// actually run the command
logger . log ( "info" , ` ${ message . author . username } ( ${ message . author . id } ) ran command ${ command } ` ) ;
try {
2020-01-13 16:31:01 +00:00
const result = await cmd ( message , args , content . replace ( command , "" ) . trim ( ) ) ; // we also provide the message content as a parameter for cases where we need more accuracy
2019-09-13 20:02:41 +00:00
if ( typeof result === "string" ) {
await client . createMessage ( message . channel . id , result ) ;
}
} catch ( error ) {
2020-01-12 18:27:16 +00:00
logger . error ( error . toString ( ) ) ;
2019-12-16 21:32:47 +00:00
await client . createMessage ( message . channel . id , "Uh oh! I ran into an error while running this command. Please report the content of the attached file here or on the esmBot Support server: <https://github.com/TheEssem/esmBot/issues>" , [ {
2019-09-13 20:02:41 +00:00
file : Buffer . from ( ` Message: ${ error } \n \n Stack Trace: ${ error . stack } ` ) ,
name : "error.txt"
} ] ) ;
}
} ;