diff --git a/src/execution.ts b/src/execution.ts index c66ad4f..9104207 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -52,6 +52,39 @@ export async function getUserLimit(user: string | { id: string }, requestTimesta return {limit: userLimits.limit, remaining: userLimits.limit - usedLimit}; } +/** + * gets the timestamp of nth use inside time limit + * @param user the user or id to check + * @param requestTimestamp the timestamp of the request (message/interaction createdAt) + * @param nth which timestamp in time limit to get (orderedd from oldest to newest) + * @returns `false` if user is vip + * @returns `null` if there is no request + * @returns `Date` timestamp of the nth request + */ +export async function getNthUseInLimitTimestamp(user: string | { id: string }, requestTimestamp: Date, nth = 1) { + const userId: string = typeof user === "string" ? user : user.id; + + const userLimits = await database.limits.findUnique({ + where: { user: BigInt(userId)} + }); + + if (userLimits?.vip) return false; + + const nthUseInLimit = await database.usage.findFirst({ + where: { + user: BigInt(userId), + timestamp: { + gte: new Date(requestTimestamp.getTime() - 1000 * 60 * 60 * 24 /* 24 hours */) + } + }, + orderBy: { timestamp: "asc" }, + skip: nth - 1, + }); + + if (!nthUseInLimit) return null; + return nthUseInLimit.timestamp; +} + /** * Replies to a request * @param request the request to reply to