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 } > ` ) ;
2020-03-02 23:10:29 +00:00
const prefix = prefixMention . test ( message . content ) ? message . content . match ( prefixMention ) [ 0 ] : ( await database . guilds . find ( { id : message . channel . guild . id } ) . exec ( ) ) [ 0 ] . prefix ;
2019-09-13 20:02:41 +00:00
// ignore other stuff
2020-03-02 23:10:29 +00:00
if ( message . content . startsWith ( prefix ) === false && ! message . mentions . includes ( client . user ) ) return ;
// && message.channel.id !== "573553254575898626"
2019-09-13 20:02:41 +00:00
// funny stuff
2020-03-02 23:10:29 +00:00
/ * i f ( m e s s a g e . c h a n n e l . i d = = = " 5 7 3 5 5 3 2 5 4 5 7 5 8 9 8 6 2 6 " & & m e s s a g e . c h a n n e l . g u i l d . i d = = = " 4 3 3 4 0 8 9 7 0 9 5 5 4 2 3 7 6 5 " ) {
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 ) ;
}
2020-03-02 23:10:29 +00:00
} * /
2019-09-13 20:02:41 +00:00
// 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-02-25 21:07:36 +00:00
if ( ! error . toString ( ) . includes ( "Request entity too large" ) ) {
logger . error ( error . toString ( ) ) ;
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>" , [ {
file : Buffer . from ( ` Message: ${ error } \n \n Stack Trace: ${ error . stack } ` ) ,
name : "error.txt"
} ] ) ;
} else {
await client . createMessage ( message . channel . id , ` ${ message . author . mention } , the resulting file was too large to upload. Try again with a smaller image if possible. ` ) ;
}
2019-09-13 20:02:41 +00:00
}
} ;