feat(validations): added cooldown

This commit is contained in:
Slincnik 2025-01-14 22:11:45 +03:00
parent ceea161dc4
commit ac63acd993
No known key found for this signature in database
3 changed files with 40 additions and 1 deletions

View file

@ -0,0 +1,37 @@
import { BuiltInValidationParams } from "@/types.js";
const cooldowns = new Map<string, Map<string, number>>();
export default function ({ targetCommand, interaction }: BuiltInValidationParams) {
const { cooldown } = targetCommand.options || {};
if (!cooldown) return;
const now = Date.now();
const userId = interaction.user.id;
const commandName = targetCommand.data.name;
if (!cooldowns.has(commandName)) {
cooldowns.set(commandName, new Map());
}
const userCooldowns = cooldowns.get(commandName)!;
if (userCooldowns) {
const expirationTime = userCooldowns.get(userId)! + cooldown * 1000;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
if (!interaction.isRepliable()) return;
interaction.reply({
content: `❌ You can use this command again in ${timeLeft.toFixed(1)} seconds.`,
ephemeral: true,
});
return true;
}
}
userCooldowns.set(userId, now);
return false;
}

View file

@ -1,3 +1,4 @@
import devOnly from "./devOnly.js";
import cooldown from "./cooldown.js";
export default [devOnly];
export default [devOnly, cooldown];

1
src/types.d.ts vendored
View file

@ -49,6 +49,7 @@ export interface CommandContext<_T extends Interaction, _Cached extends CacheTyp
export interface CommandOptions {
devOnly?: boolean;
cooldown?: number;
}
export interface CommandFileObject {