2022-03-22 20:43:26 +00:00
import database from "../utils/database.js" ;
import * as logger from "../utils/logger.js" ;
import { commands } from "../utils/collections.js" ;
import { CommandInteraction } from "eris" ;
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
export default async ( client , cluster , worker , ipc , interaction ) => {
if ( ! ( interaction instanceof CommandInteraction ) ) return ;
// check if command exists and if it's enabled
const command = interaction . data . name ;
const cmd = commands . get ( command ) ;
if ( ! cmd ) return ;
const invoker = interaction . member ? ? interaction . user ;
// actually run the command
2022-03-31 05:42:03 +00:00
logger . log ( "log" , ` ${ invoker . username } ( ${ invoker . id } ) ran slash command ${ command } ` ) ;
2022-03-22 20:43:26 +00:00
try {
await database . addCount ( command ) ;
// eslint-disable-next-line no-unused-vars
const commandClass = new cmd ( client , cluster , worker , ipc , { type : "application" , interaction } ) ;
const result = await commandClass . run ( ) ;
2022-07-20 02:06:51 +00:00
const replyMethod = interaction . acknowledged ? "editOriginalMessage" : "createMessage" ;
2022-03-22 20:43:26 +00:00
if ( typeof result === "string" || ( typeof result === "object" && result . embeds ) ) {
2022-07-20 02:06:51 +00:00
await interaction [ replyMethod ] ( result ) ;
2022-03-22 20:43:26 +00:00
} else if ( typeof result === "object" && result . file ) {
let fileSize = 8388119 ;
if ( interaction . channel . guild ) {
switch ( interaction . channel . guild . premiumTier ) {
case 2 :
fileSize = 52428308 ;
break ;
case 3 :
fileSize = 104856616 ;
break ;
}
}
if ( result . file . length > fileSize ) {
if ( process . env . TEMPDIR && process . env . TEMPDIR !== "" ) {
2022-07-20 04:27:43 +00:00
await upload ( client , ipc , result , interaction , true ) ;
2022-03-22 20:43:26 +00:00
} else {
2022-07-20 02:06:51 +00:00
await interaction [ replyMethod ] ( "The resulting image was more than 8MB in size, so I can't upload it." ) ;
2022-03-22 20:43:26 +00:00
}
} else {
2022-07-20 02:06:51 +00:00
await interaction [ replyMethod ] ( result . text ? result . text : { } , result ) ;
2022-03-22 20:43:26 +00:00
}
}
} catch ( error ) {
2022-07-20 02:06:51 +00:00
const replyMethod = interaction . acknowledged ? "editOriginalMessage" : "createMessage" ;
2022-03-22 20:43:26 +00:00
if ( error . toString ( ) . includes ( "Request entity too large" ) ) {
2022-07-20 02:06:51 +00:00
await interaction [ replyMethod ] ( "The resulting file was too large to upload. Try again with a smaller image if possible." ) ;
2022-03-22 20:43:26 +00:00
} else if ( error . toString ( ) . includes ( "Job ended prematurely" ) ) {
2022-07-20 02:06:51 +00:00
await interaction [ replyMethod ] ( "Something happened to the image servers before I could receive the image. Try running your command again." ) ;
2022-03-22 20:43:26 +00:00
} else if ( error . toString ( ) . includes ( "Timed out" ) ) {
2022-07-20 02:06:51 +00:00
await interaction [ replyMethod ] ( "The request timed out before I could download that image. Try uploading your image somewhere else or reducing its size." ) ;
2022-03-22 20:43:26 +00:00
} else {
2022-07-22 23:50:40 +00:00
logger . error ( ` Error occurred with slash command ${ command } with arguments ${ JSON . stringify ( interaction . data . options ) } : ${ error . stack || error } ` ) ;
2022-03-22 20:43:26 +00:00
try {
2022-07-20 02:06:51 +00:00
await interaction [ replyMethod ] ( "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>" , {
2022-03-22 20:43:26 +00:00
file : ` Message: ${ await clean ( error ) } \n \n Stack Trace: ${ await clean ( error . stack ) } ` ,
name : "error.txt"
2022-03-31 05:42:03 +00:00
} ) ;
2022-03-22 20:43:26 +00:00
} catch { /* silently ignore */ }
}
}
} ;