Skip to content

Commit

Permalink
Small refactor for proper type checking
Browse files Browse the repository at this point in the history
  • Loading branch information
eritislami committed Mar 7, 2024
1 parent fd70a52 commit 92b8076
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions structs/Bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@ export class Bot {
}

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

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

if (!timestamp) return;

const expirationTime = timestamp + cooldownAmount;

if (!expirationTime) return;

if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
Expand All @@ -85,10 +91,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 @@ -98,9 +105,11 @@ export class Bot {
} else {
throw new MissingPermissionsException(permissionsCheck.missing);
}
} catch (error: any) {
} catch (error) {
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 92b8076

Please sign in to comment.