add getNthUseInLimitTimestamp
will be used in a command that check the user limit
This commit is contained in:
parent
c1b165024d
commit
312f22827e
1 changed files with 33 additions and 0 deletions
|
@ -52,6 +52,39 @@ export async function getUserLimit(user: string | { id: string }, requestTimesta
|
||||||
return {limit: userLimits.limit, remaining: userLimits.limit - usedLimit};
|
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
|
* Replies to a request
|
||||||
* @param request the request to reply to
|
* @param request the request to reply to
|
||||||
|
|
Loading…
Reference in a new issue