Quota: Refactor how Quotas are being handled

also renamed limits to quota

I believe this new approach would allow me and bot hosters
to add, implement or change the quota behavior more easily.

Reimplemented the currently existing "Message count" limit
to use the new IQuota, refactoring a code *a little*.
This commit is contained in:
Wroclaw 2023-09-21 07:07:43 +02:00
parent 46bb5c867d
commit 339ef06ff9
5 changed files with 214 additions and 107 deletions

View file

@ -1,7 +1,7 @@
import { ApplicationCommandType, ChatInputCommandInteraction, ApplicationCommandOption, ApplicationCommandOptionType } from "discord.js";
import { ApplicationCommandType, ChatInputCommandInteraction, ApplicationCommandOption, ApplicationCommandOptionType, APIEmbedField } from "discord.js";
import Command from "../command";
import { getUserLimit, getNthUseInLimitTimestamp } from "../execution";
import { config } from "../index";
export default class MyLimit extends Command implements Command {
name = "check-limit";
@ -27,40 +27,23 @@ export default class MyLimit extends Command implements Command {
if (recoveryFor <= 0) recoveryFor = 1;
const userLimitPromise = getUserLimit(interaction.user, interaction.createdAt);
const nthUseInLimitTimestampPromise = getNthUseInLimitTimestamp(
interaction.user,
interaction.createdAt,
recoveryFor,
);
const userQuotaRecovery = await config.quota.getUserQuotaRecovery(interaction.user, interaction, recoveryFor);
const userLimit = await userLimitPromise;
const nthUseInLimitTimestamp = await nthUseInLimitTimestampPromise;
const fields: APIEmbedField[] = [];
if (userLimit === false || nthUseInLimitTimestamp === false) {
await interaction.reply({
embeds: [{
author: { name: interaction.user.username, icon_url: interaction.user.displayAvatarURL({ size: 128 }) },
description: "User is a VIP, so there is no limit",
}],
ephemeral: ephemeral,
});
return;
}
fields.push({ name: "Quota", inline: true, value: `${userQuotaRecovery.quota} ${userQuotaRecovery.unitName}`.trim() });
fields.push({ name: "Usage", inline: true, value: `${userQuotaRecovery.used} ${userQuotaRecovery.unitName}`.trim() });
if (userQuotaRecovery.recoveryTimestamp !== undefined) fields.push({
name: `Recovery for ${recoveryFor} message${recoveryFor>1 ? "s" : ""}`,
value: userQuotaRecovery.recoveryTimestamp === Infinity ? "never" :
`<t:${Math.ceil(userQuotaRecovery.recoveryTimestamp/1000)}:R>`
});
await interaction.reply({
embeds: [{
author: { name: interaction.user.username, icon_url: interaction.user.displayAvatarURL({ size: 128 }) },
fields: [
{ name: "Limit", inline: true, value: String(userLimit.limit) },
{ name: "Usage", inline: true, value: String(userLimit.limit - userLimit.remaining) },
{
name: `Recovery for ${recoveryFor} message${recoveryFor>1 ? "s" : ""}`,
value: nthUseInLimitTimestamp === null ? "never" :
// timestamp of the nth use in limit + 24 hours
`<t:${Math.floor(new Date(nthUseInLimitTimestamp.getTime() + 1000 * 60 * 60 * 24 /*24 hours*/).getTime()/1000)}:R>`,
},
]
fields: fields,
}],
ephemeral: ephemeral,
});