47 lines
1.5 KiB
JavaScript
47 lines
1.5 KiB
JavaScript
|
const { Events, Collection } = require('discord.js');
|
||
|
|
||
|
module.exports = {
|
||
|
name: Events.InteractionCreate,
|
||
|
async execute(interaction) {
|
||
|
if (!interaction.isChatInputCommand()) return;
|
||
|
const { cooldowns } = interaction.client;
|
||
|
|
||
|
const command = interaction.client.commands.get(interaction.commandName);
|
||
|
|
||
|
if (!command) {
|
||
|
console.error(`ERR: Command "${interaction.commandName}" not found`);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!cooldowns.has(command.data.name)) {
|
||
|
cooldowns.set(command.data.name, new Collection());
|
||
|
}
|
||
|
|
||
|
const now = Date.now();
|
||
|
const timestamps = cooldowns.get(command.data.name);
|
||
|
const defaultCooldownDuration = 5;
|
||
|
const cooldownAmount = (command.cooldown ?? defaultCooldownDuration) * 1000;
|
||
|
if (timestamps.has(interaction.user.id)) {
|
||
|
const expirationTime = timestamps.get(interaction.user.id) + cooldownAmount;
|
||
|
if (now < expirationTime) {
|
||
|
const expiredTimestamp = Math.round(expirationTime / 1000);
|
||
|
return interaction.reply({ content: `Please wait for <t:${expiredTimestamp}:R> before using \`${command.data.name}\` again.`, ephemeral: true });
|
||
|
}
|
||
|
}
|
||
|
|
||
|
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: `ERROR!`, ephemeral: true});
|
||
|
} else {
|
||
|
await interaction.reply({ content: `ERROR!`, ephemeral: true});
|
||
|
}
|
||
|
}
|
||
|
},
|
||
|
}
|