Skip to content

Commit

Permalink
Refactor for type-narrowing in cooldown logic
Browse files Browse the repository at this point in the history
  • Loading branch information
eritislami committed Mar 7, 2024
1 parent 7ecf9f6 commit d7a2967
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions structs/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,14 @@ export class Bot {
}

const now = Date.now();
const timestamps = this.cooldowns.get(interaction.commandName);
const timestamps = this.cooldowns.get(interaction.commandName)!;
const cooldownAmount = (command.cooldown || 1) * 1000;

if (timestamps && timestamps.has(interaction.user.id)) {
const timestamp = timestamps.get(interaction.user.id);

if (!timestamp) return;
const timestamp = timestamps.get(interaction.user.id);

if (timestamp) {
const expirationTime = timestamp + cooldownAmount;

if (!expirationTime) return;

if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
return interaction.reply({
Expand All @@ -91,12 +87,11 @@ export class Bot {
ephemeral: true
});
}

timestamps.set(interaction.user.id, now);

setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);
}

timestamps.set(interaction.user.id, now);
setTimeout(() => timestamps.delete(interaction.user.id), cooldownAmount);

try {
const permissionsCheck: PermissionResult = await checkPermissions(command, interaction);

Expand All @@ -105,11 +100,9 @@ export class Bot {
} else {
throw new MissingPermissionsException(permissionsCheck.missing);
}
} catch (error) {
} catch (error: any) {
console.error(error);

if (!(error instanceof Error)) return;

if (error.message.includes("permissions")) {
interaction.reply({ content: error.toString(), ephemeral: true }).catch(console.error);
} else {
Expand Down

0 comments on commit d7a2967

Please sign in to comment.