2022-12-12 01:25:48 +00:00
const Event = require ( '../../base/Event.js' ) ;
2022-12-12 01:14:28 +00:00
module . exports = class InteractionHandler extends Event {
2022-12-11 02:40:15 +00:00
constructor ( wsEvent ) {
2022-12-12 01:14:28 +00:00
super ( wsEvent ) ;
2022-12-11 02:40:15 +00:00
this . wsEvent = wsEvent ;
2022-12-09 13:14:31 +00:00
}
2022-12-11 02:40:15 +00:00
async run ( client , interaction ) {
if ( ! interaction . isChatInputCommand ( ) ) return ;
2022-12-09 13:14:31 +00:00
// Request all the data we need from the database
const data = { } ;
2022-12-11 02:40:15 +00:00
data . user = await client . db . getUser ( interaction . user . id ) ;
data . guild = await client . db . getGuild ( interaction . guild . id ) ;
data . member = await client . db . getMember ( interaction . guild . id , interaction . user . id ) ;
2022-12-09 13:14:31 +00:00
2022-12-11 02:40:15 +00:00
const command = client . commands . get ( interaction . commandName ) ;
2022-12-09 13:14:31 +00:00
// Return if the command is disabled globally
2022-12-11 23:22:04 +00:00
if ( command . enabled === false ) interaction . reply ( {
content : client . config . emojis . permError + ' This command has been disabled by my developers.' ,
ephemeral : true
} ) ;
2022-12-11 02:40:15 +00:00
2022-12-09 13:14:31 +00:00
// Return if the command is restricted to developers (and the user is not a developer)
2022-12-11 02:40:15 +00:00
if ( command . devOnly === true && client . config . devIds . includes ( interaction . user . id ) !== true ) {
2022-12-11 23:22:04 +00:00
return interaction . reply ( {
content : ` ${ client . config . emojis . permError } ${ interaction . user . username } is not in the sudoers file. This incident will be reported. ` ,
ephemeral : true
} ) ;
2022-12-11 02:40:15 +00:00
}
2022-12-09 13:14:31 +00:00
// Cooldown
2022-12-11 02:40:15 +00:00
if ( client . cooldowns . get ( command . name ) . has ( interaction . user . id ) ) {
const timestamp = client . cooldowns . get ( command . name ) . get ( interaction . user . id ) ;
2022-12-09 13:14:31 +00:00
const currentTime = Date . now ( ) ;
const cooldown = command . cooldown / 1000 ;
const timePassed = Math . floor ( ( currentTime - timestamp ) / 1000 ) ;
2022-12-11 23:22:04 +00:00
return interaction . reply ( {
content : ` ${ client . config . emojis . wait } You need to wait ${ cooldown - timePassed } seconds before using this command again. ` ,
ephemeral : true
2022-12-12 01:25:48 +00:00
} ) ;
2022-12-09 13:14:31 +00:00
} else {
2022-12-11 02:40:15 +00:00
client . cooldowns . get ( command . name ) . set ( interaction . user . id , new Date ( ) ) ;
2022-12-09 13:14:31 +00:00
setTimeout ( ( ) => {
2022-12-11 02:40:15 +00:00
client . cooldowns . get ( command . name ) . delete ( interaction . user . id ) ;
} , client . commands . get ( command . name ) . cooldown ) ;
}
2022-12-09 13:14:31 +00:00
2022-12-11 06:22:57 +00:00
// Try to execute the command, if it fails return error stack and inform the user
2022-12-09 13:14:31 +00:00
try {
2022-12-11 02:40:15 +00:00
command . run ( client , interaction , data ) ;
client . logger . command ( ` Ran ${ command . name } ` ) ;
2022-12-09 13:14:31 +00:00
} catch ( error ) {
2022-12-11 02:40:15 +00:00
client . logger . error ( 'COMMAND_EXECUTION_ERROR' , ` ${ command . name } : ${ error . stack } ` ) ;
2022-12-11 23:22:04 +00:00
interaction . reply ( {
content : ` ${ client . config . emojis . botError } An error occurred when I was trying to run this command. I've sent through the details of the error to my developers. ` ,
ephemeral : true
} ) ;
2022-12-09 13:14:31 +00:00
}
}
2022-12-11 02:40:15 +00:00
} ;