mirror of
https://github.com/JonnyBro/JaBa.git
synced 2025-01-19 17:03:47 +05:00
feat(validations): added cooldown
This commit is contained in:
parent
ceea161dc4
commit
ac63acd993
3 changed files with 40 additions and 1 deletions
37
src/handlers/command-handler/validations/cooldown.ts
Normal file
37
src/handlers/command-handler/validations/cooldown.ts
Normal 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;
|
||||
}
|
|
@ -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
1
src/types.d.ts
vendored
|
@ -49,6 +49,7 @@ export interface CommandContext<_T extends Interaction, _Cached extends CacheTyp
|
|||
|
||||
export interface CommandOptions {
|
||||
devOnly?: boolean;
|
||||
cooldown?: number;
|
||||
}
|
||||
|
||||
export interface CommandFileObject {
|
||||
|
|
Loading…
Reference in a new issue