2022-03-22 20:43:26 +00:00
import database from "../utils/database.js" ;
import * as logger from "../utils/logger.js" ;
2022-09-01 01:00:34 +00:00
import { commands , messageCommands } from "../utils/collections.js" ;
2022-03-22 20:43:26 +00:00
import { clean } from "../utils/misc.js" ;
2022-07-18 22:05:01 +00:00
import { upload } from "../utils/tempimages.js" ;
2022-03-22 20:43:26 +00:00
// run when a slash command is executed
2022-09-21 05:05:03 +00:00
export default async ( client , interaction ) => {
2022-09-01 01:00:34 +00:00
if ( interaction ? . type !== 2 ) return ;
2022-03-22 20:43:26 +00:00
// check if command exists and if it's enabled
const command = interaction . data . name ;
2022-09-01 01:00:34 +00:00
let cmd = commands . get ( command ) ;
if ( ! cmd ) {
cmd = messageCommands . get ( command ) ;
if ( ! cmd ) return ;
}
2022-12-12 17:15:10 +00:00
if ( cmd . dbRequired && ! database ) {
await interaction [ "createMessage" ] ( { content : "This command is unavailable on stateless instances of esmBot." , flags : 64 } ) ;
return ;
2022-12-12 17:36:08 +00:00
}
2022-03-22 20:43:26 +00:00
const invoker = interaction . member ? ? interaction . user ;
// actually run the command
2022-09-01 01:00:34 +00:00
logger . log ( "log" , ` ${ invoker . username } ( ${ invoker . id } ) ran application command ${ command } ` ) ;
2022-03-22 20:43:26 +00:00
try {
2022-12-12 17:15:10 +00:00
if ( database ) {
await database . addCount ( command ) ;
}
2022-03-22 20:43:26 +00:00
// eslint-disable-next-line no-unused-vars
2022-09-21 05:05:03 +00:00
const commandClass = new cmd ( client , { type : "application" , interaction } ) ;
2022-03-22 20:43:26 +00:00
const result = await commandClass . run ( ) ;
2022-09-24 03:25:16 +00:00
const replyMethod = interaction . acknowledged ? "editOriginal" : "createMessage" ;
2022-09-01 01:00:34 +00:00
if ( typeof result === "string" ) {
await interaction [ replyMethod ] ( {
content : result ,
flags : commandClass . success ? 0 : 64
} ) ;
2022-10-04 18:23:23 +00:00
} else if ( typeof result === "object" ) {
if ( result . contents && result . name ) {
const fileSize = 8388119 ;
if ( result . contents . length > fileSize ) {
if ( process . env . TEMPDIR && process . env . TEMPDIR !== "" ) {
await upload ( client , result , interaction , true ) ;
} else {
await interaction [ replyMethod ] ( {
content : "The resulting image was more than 8MB in size, so I can't upload it." ,
flags : 64
} ) ;
}
2022-03-22 20:43:26 +00:00
} else {
2022-10-04 18:23:23 +00:00
await interaction [ replyMethod ] ( result . text ? result . text : { files : [ result ] } ) ;
2022-03-22 20:43:26 +00:00
}
} else {
2022-10-04 18:23:23 +00:00
await interaction [ replyMethod ] ( Object . assign ( {
flags : result . flags ? ? ( commandClass . success ? 0 : 64 )
} , result ) ) ;
2022-03-22 20:43:26 +00:00
}
2022-11-26 19:31:40 +00:00
} else {
logger . warn ( ` Unknown return type for command ${ command } : ${ result } ( ${ typeof result } ) ` ) ;
await interaction [ replyMethod ] ( Object . assign ( {
flags : commandClass . success ? 0 : 64
} , result ) ) ;
2022-03-22 20:43:26 +00:00
}
} catch ( error ) {
2022-09-24 03:25:16 +00:00
const replyMethod = interaction . acknowledged ? "editOriginal" : "createMessage" ;
2022-03-22 20:43:26 +00:00
if ( error . toString ( ) . includes ( "Request entity too large" ) ) {
2022-09-01 01:00:34 +00:00
await interaction [ replyMethod ] ( { content : "The resulting file was too large to upload. Try again with a smaller image if possible." , flags : 64 } ) ;
2022-03-22 20:43:26 +00:00
} else if ( error . toString ( ) . includes ( "Job ended prematurely" ) ) {
2022-09-01 01:00:34 +00:00
await interaction [ replyMethod ] ( { content : "Something happened to the image servers before I could receive the image. Try running your command again." , flags : 64 } ) ;
2022-03-22 20:43:26 +00:00
} else {
2022-10-02 18:28:43 +00:00
logger . error ( ` Error occurred with application command ${ command } with arguments ${ JSON . stringify ( interaction . data . optionsArray ) } : ${ error . stack || error } ` ) ;
2022-03-22 20:43:26 +00:00
try {
2022-09-11 04:18:44 +00:00
let err = error ;
if ( error ? . constructor ? . name == "Promise" ) err = await error ;
2022-09-24 03:25:16 +00:00
await interaction [ replyMethod ] ( {
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>" ,
files : [ {
contents : ` Message: ${ clean ( err ) } \n \n Stack Trace: ${ clean ( err . stack ) } ` ,
name : "error.txt"
} ]
2022-03-31 05:42:03 +00:00
} ) ;
2022-09-24 03:25:16 +00:00
} catch ( e ) {
logger . error ( ` While attempting to send the previous error message, another error occurred: ${ e . stack || e } ` ) ;
}
2022-03-22 20:43:26 +00:00
}
}
} ;