2023-11-27 00:02:34 +01:00
const { Events , Collection } = require ( 'discord.js' ) ;
2023-11-18 00:47:34 +01:00
2023-11-27 00:02:34 +01:00
module . exports = {
name : Events . InteractionCreate ,
async execute ( interaction ) {
if ( ! interaction . isChatInputCommand ( ) ) return ;
2023-11-18 00:47:34 +01:00
2023-11-27 00:02:34 +01:00
const command = interaction . client . commands . get ( interaction . commandName ) ;
2023-11-18 00:47:34 +01:00
2023-11-27 00:02:34 +01:00
if ( ! command ) {
console . error ( ` No command matching ${ interaction . commandName } was found. ` ) ;
return ;
}
2023-11-18 21:26:54 +01:00
2023-11-27 00:02:34 +01:00
const { cooldowns } = interaction . client ;
2023-11-18 21:26:54 +01:00
2023-11-27 00:02:34 +01:00
if ( ! cooldowns . has ( command . data . name ) ) {
cooldowns . set ( command . data . name , new Collection ( ) ) ;
}
2023-11-18 21:26:54 +01:00
2023-11-27 00:02:34 +01:00
const now = Date . now ( ) ;
const timestamps = cooldowns . get ( command . data . name ) ;
const defaultCooldownDuration = 3 ;
const cooldownAmount = ( command . cooldown ? ? defaultCooldownDuration ) * 1000 ;
2023-11-18 21:26:54 +01:00
2023-11-27 00:02:34 +01:00
if ( timestamps . has ( interaction . user . id ) ) {
const expirationTime = timestamps . get ( interaction . user . id ) + cooldownAmount ;
2023-11-18 21:26:54 +01:00
2023-11-27 00:02:34 +01:00
if ( now < expirationTime ) {
const expiredTimestamp = Math . round ( expirationTime / 1000 ) ;
return interaction . reply ( { content : ` Please wait, you are on a cooldown for \` ${ command . data . name } \` . You can use it again <t: ${ expiredTimestamp } :R>. ` , ephemeral : true } ) ;
}
2023-11-20 00:59:41 +01:00
}
2023-11-27 00:02:34 +01:00
timestamps . set ( interaction . user . id , now ) ;
setTimeout ( ( ) => timestamps . delete ( interaction . user . id ) , cooldownAmount ) ;
try {
await command . execute ( interaction ) ;
} catch ( error ) {
console . error ( error ) ;
if ( interaction . replied || interaction . deferred ) {
await interaction . followUp ( { content : 'There was an error while executing the command!' , ephemeral : true } ) ;
} else {
await interaction . reply ( { content : 'There was an error while executing this command!' , ephemeral : true } ) ;
}
2023-11-18 00:47:34 +01:00
}
2023-11-27 00:02:34 +01:00
} ,
} ;