Skip to content

Commit

Permalink
feat: command cooldowns
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed May 15, 2023
1 parent ad3093f commit b9ab7ef
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 34 deletions.
69 changes: 35 additions & 34 deletions src/Events/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -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);
}

Expand All @@ -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);
}
},
};
1 change: 1 addition & 0 deletions src/Structures/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b9ab7ef

Please sign in to comment.