2020-04-20 20:52:22 +00:00
const fs = require ( "fs" ) ;
2021-08-06 17:03:44 +00:00
const fetch = require ( "node-fetch" ) ;
2019-09-13 20:02:41 +00:00
const database = require ( "../utils/database.js" ) ;
const logger = require ( "../utils/logger.js" ) ;
const collections = require ( "../utils/collections.js" ) ;
2021-07-02 04:42:12 +00:00
const parseCommand = require ( "../utils/parseCommand.js" ) ;
2021-07-31 18:36:00 +00:00
const { clean } = require ( "../utils/misc.js" ) ;
2019-09-13 20:02:41 +00:00
// run when someone sends a message
2021-07-05 04:15:27 +00:00
module . exports = async ( client , cluster , worker , ipc , message ) => {
2021-08-12 23:45:17 +00:00
// ignore other bots
2019-09-13 20:02:41 +00:00
if ( message . author . bot ) return ;
2019-12-16 23:14:29 +00:00
// don't run command if bot can't send messages
2021-03-12 20:42:07 +00:00
if ( message . channel . guild && ! message . channel . permissionsOf ( client . user . id ) . has ( "sendMessages" ) ) return ;
2019-12-16 23:14:29 +00:00
2020-12-18 02:32:19 +00:00
let prefixCandidate ;
2021-08-13 03:28:09 +00:00
let guildDB ;
2020-12-26 18:17:10 +00:00
if ( message . channel . guild ) {
2021-08-07 20:28:39 +00:00
const cachedPrefix = collections . prefixCache . get ( message . channel . guild . id ) ;
if ( cachedPrefix ) {
prefixCandidate = cachedPrefix ;
2020-12-26 18:17:10 +00:00
} else {
2021-08-13 03:28:09 +00:00
guildDB = await database . getGuild ( message . channel . guild . id ) ;
2021-08-10 00:09:32 +00:00
if ( ! guildDB ) {
2021-08-13 03:28:09 +00:00
guildDB = await database . fixGuild ( message . channel . guild ) ;
2020-12-26 18:17:10 +00:00
}
prefixCandidate = guildDB . prefix ;
collections . prefixCache . set ( message . channel . guild . id , guildDB . prefix ) ;
2020-12-18 02:32:19 +00:00
}
2020-11-05 21:40:18 +00:00
}
2020-12-18 02:32:19 +00:00
2021-03-18 14:29:03 +00:00
let prefix ;
let isMention = false ;
if ( message . channel . guild ) {
const user = message . channel . guild . members . get ( client . user . id ) ;
if ( message . content . startsWith ( user . mention ) ) {
prefix = ` ${ user . mention } ` ;
isMention = true ;
} else if ( message . content . startsWith ( ` <@ ${ client . user . id } > ` ) ) { // workaround for member.mention not accounting for both mention types
prefix = ` <@ ${ client . user . id } > ` ;
isMention = true ;
} else {
prefix = prefixCandidate ;
}
} else {
prefix = "" ;
}
2019-09-13 20:02:41 +00:00
// ignore other stuff
2021-08-12 23:45:17 +00:00
if ( ! message . content . startsWith ( prefix ) ) return ;
2019-09-13 20:02:41 +00:00
// separate commands and args
2021-03-18 14:29:03 +00:00
const replace = isMention ? ` @ ${ client . user . username } ` : prefix ;
const content = message . cleanContent . substring ( replace . length ) . trim ( ) ;
2021-03-16 20:45:10 +00:00
const rawContent = message . content . substring ( prefix . length ) . trim ( ) ;
2021-07-02 04:42:12 +00:00
const preArgs = content . split ( /\s+/g ) ;
preArgs . shift ( ) ;
2021-04-14 21:42:18 +00:00
const command = rawContent . split ( /\s+/g ) . shift ( ) . toLowerCase ( ) ;
2021-07-02 04:42:12 +00:00
const parsed = parseCommand ( preArgs ) ;
2021-08-13 03:28:09 +00:00
const aliased = collections . aliases . get ( command ) ;
2019-09-13 20:02:41 +00:00
2020-04-10 02:40:52 +00:00
// don't run if message is in a disabled channel
2020-12-26 18:17:10 +00:00
if ( message . channel . guild ) {
2021-08-13 03:28:09 +00:00
const disabled = collections . disabledCache . get ( message . channel . guild . id ) ;
if ( disabled ) {
2020-12-26 18:17:10 +00:00
if ( disabled . includes ( message . channel . id ) && command != "channel" ) return ;
2021-08-13 03:28:09 +00:00
} else {
guildDB = await database . getGuild ( message . channel . guild . id ) ;
2020-12-26 18:17:10 +00:00
collections . disabledCache . set ( message . channel . guild . id , guildDB . disabled ) ;
if ( guildDB . disabled . includes ( message . channel . id ) && command !== "channel" ) return ;
}
2021-08-13 03:28:09 +00:00
const disabledCmds = collections . disabledCmdCache . get ( message . channel . guild . id ) ;
if ( disabledCmds ) {
if ( disabledCmds . includes ( aliased ? aliased : command ) ) return ;
} else {
guildDB = await database . getGuild ( message . channel . guild . id ) ;
2021-08-13 19:24:26 +00:00
collections . disabledCmdCache . set ( message . channel . guild . id , guildDB . disabled _commands ? guildDB . disabled _commands : guildDB . disabledCommands ) ;
if ( ( guildDB . disabled _commands ? guildDB . disabled _commands : guildDB . disabledCommands ) . includes ( aliased ? aliased : command ) ) return ;
2021-08-13 03:28:09 +00:00
}
2020-12-18 02:32:19 +00:00
}
2020-04-10 02:40:52 +00:00
2021-08-13 03:28:09 +00:00
// check if command exists and if it's enabled
const cmd = aliased ? collections . commands . get ( aliased ) : collections . commands . get ( command ) ;
2019-09-13 20:02:41 +00:00
if ( ! cmd ) return ;
// actually run the command
Class commands, improved sharding, and many other changes (#88)
* Load commands recursively
* Sort commands
* Missed a couple of spots
* missed even more spots apparently
* Ported commands in "fun" category to new class-based format, added babel eslint plugin
* Ported general commands, removed old/unneeded stuff, replaced moment with day, many more fixes I lost track of
* Missed a spot
* Removed unnecessary abort-controller package, add deprecation warning for mongo database
* Added imagereload, clarified premature end message
* Fixed docker-compose path issue, added total bot uptime to stats, more fixes for various parts
* Converted image commands into classes, fixed reload, ignore another WS event, cleaned up command handler and image runner
* Converted music/soundboard commands to class format
* Cleanup unnecessary logs
* awful tag command class port
* I literally somehow just learned that you can leave out the constructor in classes
* Pass client directly to commands/events, cleaned up command handler
* Migrated bot to eris-sharder, fixed some error handling stuff
* Remove unused modules
* Fixed type returning
* Switched back to Eris stable
* Some fixes and cleanup
* might wanna correct this
* Implement image command ratelimiting
* Added Bot token prefix, added imagestats, added running endpoint to API
2021-04-12 16:16:12 +00:00
logger . log ( "log" , ` ${ message . author . username } ( ${ message . author . id } ) ran command ${ command } ` ) ;
2021-05-11 16:12:01 +00:00
const reference = {
messageReference : {
channelID : message . channel . id ,
messageID : message . id ,
guildID : message . channel . guild ? message . channel . guild . id : undefined ,
failIfNotExists : false
} ,
allowedMentions : {
repliedUser : false
}
} ;
2019-09-13 20:02:41 +00:00
try {
2021-08-13 03:28:09 +00:00
await database . addCount ( collections . aliases . has ( command ) ? collections . aliases . get ( command ) : command ) ;
2021-03-05 18:03:17 +00:00
const startTime = new Date ( ) ;
2021-07-02 04:42:12 +00:00
// eslint-disable-next-line no-unused-vars
2021-07-05 04:15:27 +00:00
const commandClass = new cmd ( client , cluster , worker , ipc , message , parsed . _ , message . content . substring ( prefix . length ) . trim ( ) . replace ( command , "" ) . trim ( ) , ( ( { _ , ... o } ) => o ) ( parsed ) ) ; // we also provide the message content as a parameter for cases where we need more accuracy
2021-06-18 05:10:11 +00:00
const result = await commandClass . run ( ) ;
2021-03-05 18:03:17 +00:00
const endTime = new Date ( ) ;
2021-05-11 16:12:01 +00:00
if ( ( endTime - startTime ) >= 180000 ) reference . allowedMentions . repliedUser = true ;
if ( typeof result === "string" ) {
reference . allowedMentions . repliedUser = true ;
await client . createMessage ( message . channel . id , Object . assign ( {
content : result
} , reference ) ) ;
} else if ( typeof result === "object" && result . embed ) {
await client . createMessage ( message . channel . id , Object . assign ( result , reference ) ) ;
2020-04-12 19:51:48 +00:00
} else if ( typeof result === "object" && result . file ) {
2020-04-24 02:14:03 +00:00
if ( result . file . length > 8388119 && process . env . TEMPDIR !== "" ) {
2020-04-20 20:52:22 +00:00
const filename = ` ${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) } . ${ result . name . split ( "." ) [ 1 ] } ` ;
2020-08-13 13:47:41 +00:00
await fs . promises . writeFile ( ` ${ process . env . TEMPDIR } / ${ filename } ` , result . file ) ;
2021-08-06 17:03:44 +00:00
const imageURL = ` ${ process . env . TMP _DOMAIN == "" ? "https://tmp.projectlounge.pw" : process . env . TMP _DOMAIN } / ${ filename } ` ;
try {
await fetch ( imageURL ) ;
} catch {
// this is here to make sure the image is properly cached by discord
}
2021-05-11 16:12:01 +00:00
await client . createMessage ( message . channel . id , Object . assign ( {
2020-04-20 20:52:22 +00:00
embed : {
color : 16711680 ,
2020-12-08 02:42:36 +00:00
title : "Here's your image!" ,
2021-08-06 17:03:44 +00:00
url : imageURL ,
2020-04-20 20:52:22 +00:00
image : {
2021-08-06 17:03:44 +00:00
url : imageURL
2020-12-08 02:42:36 +00:00
} ,
footer : {
text : "The result image was more than 8MB in size, so it was uploaded to an external site instead."
} ,
2021-05-11 16:12:01 +00:00
}
} , reference ) ) ;
2020-04-20 20:52:22 +00:00
} else {
2021-05-11 16:12:01 +00:00
await client . createMessage ( message . channel . id , Object . assign ( {
content : result . text ? result . text : undefined
} , reference ) , result ) ;
2020-04-20 20:52:22 +00:00
}
2019-09-13 20:02:41 +00:00
}
} catch ( error ) {
2020-07-17 00:54:03 +00:00
if ( error . toString ( ) . includes ( "Request entity too large" ) ) {
2021-05-11 16:12:01 +00:00
await client . createMessage ( message . channel . id , Object . assign ( {
content : "The resulting file was too large to upload. Try again with a smaller image if possible."
} , reference ) ) ;
} else if ( error . toString ( ) . includes ( "Job ended prematurely" ) ) {
await client . createMessage ( message . channel . id , Object . assign ( {
content : "Something happened to the image servers before I could receive the image. Try running your command again."
} , reference ) ) ;
2020-07-17 00:54:03 +00:00
} else if ( error . toString ( ) . includes ( "Timed out" ) ) {
2021-05-11 16:12:01 +00:00
await client . createMessage ( message . channel . id , Object . assign ( {
content : "The request timed out before I could download that image. Try uploading your image somewhere else or reducing its size."
} , reference ) ) ;
2020-07-17 00:54:03 +00:00
} else {
2021-06-02 01:27:28 +00:00
logger . error ( ` Error occurred with command message ${ message . cleanContent } : ${ error . toString ( ) } ` ) ;
2021-04-15 00:57:35 +00:00
try {
2021-05-11 16:12:01 +00:00
await client . createMessage ( message . channel . id , Object . assign ( {
content : "Uh oh! I ran into an error while running this command. Please report the content of the attached file at the following link or on the esmBot Support server: <https://github.com/esmBot/esmBot/issues>"
} , reference ) , [ {
2021-07-31 18:36:00 +00:00
file : ` Message: ${ await clean ( error ) } \n \n Stack Trace: ${ await clean ( error . stack ) } ` ,
2021-04-15 00:57:35 +00:00
name : "error.txt"
} ] ) ;
} catch { /* silently ignore */ }
2020-02-25 21:07:36 +00:00
}
2019-09-13 20:02:41 +00:00
}
} ;