From fcc400f671a58c0f144ef88b04c0abe02a64c844 Mon Sep 17 00:00:00 2001 From: shni Date: Sun, 5 Oct 2025 00:51:13 -0500 Subject: [PATCH] feat(cooldown): implement cooldown management functions for user actions --- src/game/cooldowns/service.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/game/cooldowns/service.ts diff --git a/src/game/cooldowns/service.ts b/src/game/cooldowns/service.ts new file mode 100644 index 0000000..397cf2c --- /dev/null +++ b/src/game/cooldowns/service.ts @@ -0,0 +1,20 @@ +import { prisma } from '../../core/database/prisma'; + +export async function getCooldown(userId: string, guildId: string, key: string) { + return prisma.actionCooldown.findUnique({ where: { userId_guildId_key: { userId, guildId, key } } }); +} + +export async function setCooldown(userId: string, guildId: string, key: string, seconds: number) { + const until = new Date(Date.now() + Math.max(0, seconds) * 1000); + return prisma.actionCooldown.upsert({ + where: { userId_guildId_key: { userId, guildId, key } }, + update: { until }, + create: { userId, guildId, key, until }, + }); +} + +export async function assertNotOnCooldown(userId: string, guildId: string, key: string) { + const cd = await getCooldown(userId, guildId, key); + if (cd && cd.until > new Date()) throw new Error('Cooldown activo'); +} +