From b9ab7ef3e8ca50fa565df788b3e5e35ab78f5982 Mon Sep 17 00:00:00 2001 From: dev-737 <73829355+dev-737@users.noreply.github.com> Date: Mon, 15 May 2023 18:32:14 +0530 Subject: [PATCH] feat: command cooldowns --- src/Events/interactionCreate.ts | 69 +++++++++++++++++---------------- src/Structures/client.ts | 1 + 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/Events/interactionCreate.ts b/src/Events/interactionCreate.ts index 9244ac5a..40ddc6c9 100644 --- a/src/Events/interactionCreate.ts +++ b/src/Events/interactionCreate.ts @@ -1,37 +1,40 @@ import { Interaction } from 'discord.js'; -import { checkIfStaff } from '../Utils/functions/utils'; +import { checkIfStaff, toHuman } from '../Utils/functions/utils'; import { captureException } from '@sentry/node'; import logger from '../Utils/logger'; export default { name: 'interactionCreate', async execute(interaction: Interaction) { - if (interaction.isAutocomplete()) { + if (interaction.isChatInputCommand() || interaction.isContextMenuCommand()) { const command = interaction.client.commands.get(interaction.commandName); - if (command?.autocomplete) command.autocomplete(interaction); - } + if (!command) return; - else if (interaction.isChatInputCommand() || interaction.isMessageContextMenuCommand()) { - if ( - interaction.inCachedGuild() && - !interaction.channel?.permissionsFor(interaction.client.user)?.has(['SendMessages', 'EmbedLinks']) - ) { - return await interaction.reply({ - content: 'I do not have the right permissions in this channel to function properly!', - ephemeral: true, - }); + if (command.cooldown) { + const cooldown = interaction.client.commandCooldowns.get(`${interaction.commandName}-${interaction.user.id}`); + if (cooldown && cooldown > Date.now()) { + return interaction.reply({ + content: `You are on cooldown! Please wait \`${toHuman(cooldown - Date.now())}\` to use command again!`, + ephemeral: true, + }); + } + interaction.client.commandCooldowns.set( + `${interaction.commandName}-${interaction.user.id}`, Date.now() + command.cooldown, + ); } - const command = interaction.client.commands.get(interaction.commandName); - if (!command) return; - - try { - // Check if the user is staff/developer - if (command.staff || command.developer) { - const permCheck = checkIfStaff(interaction.user.id, command.developer); - if (!permCheck) return interaction.reply({ content: 'You do not have the right permissions to use this command!', ephemeral: true }); + // Check if the user is staff/developer + if (command.staff || command.developer) { + const permCheck = checkIfStaff(interaction.user.id, command.developer); + if (!permCheck) { + return interaction.reply({ + content: 'You do not have the right permissions to use this command!', + ephemeral: true, + }); } + } + try { await command.execute(interaction); } @@ -42,20 +45,18 @@ export default { extra: { command: interaction.commandName }, }); - try { - const errorMsg = { - content: 'There was an error while executing this command! The developers have been notified.', - ephemeral: true, - fetchReply: true, - }; - interaction.deferred - ? await interaction.followUp(errorMsg) - : interaction.replied - ? await interaction.channel?.send(errorMsg) - : await interaction.reply(errorMsg); - } - catch {return;} + const errorMsg = { + content: 'There was an error while executing this command! The developers have been notified.', + ephemeral: true, + }; + interaction.replied || interaction.deferred + ? await interaction.followUp(errorMsg).catch(() => null) + : await interaction.reply(errorMsg).catch(() => null); } } + else if (interaction.isAutocomplete()) { + const command = interaction.client.commands.get(interaction.commandName); + if (command?.autocomplete) command.autocomplete(interaction); + } }, }; diff --git a/src/Structures/client.ts b/src/Structures/client.ts index 6fe8d120..dfe3461b 100644 --- a/src/Structures/client.ts +++ b/src/Structures/client.ts @@ -23,6 +23,7 @@ export class ExtendedClient extends Client { }); this.commands = new Collection(); + this.commandCooldowns = new Collection(); this.description = project.description; this.version = project.version; this.emotes = emojis;