add check-limit command

This commit is contained in:
Wroclaw 2023-05-10 03:04:45 +02:00
parent 48b9ec02a0
commit 46e2c00ab1

View file

@ -0,0 +1,68 @@
import { ApplicationCommandType, ChatInputCommandInteraction, ApplicationCommandOption, ApplicationCommandOptionType } from "discord.js";
import Command from "../command";
import { getUserLimit, getNthUseInLimitTimestamp } from "../execution";
export default class MyLimit extends Command implements Command {
name = "check-limit";
description = "Checks your limits and remaining usage";
type = ApplicationCommandType.ChatInput;
options: ApplicationCommandOption[] = [
{
name: "recovery-for",
description: "Calculate the limit recovery time for given message count (default 1)",
type: ApplicationCommandOptionType.Integer,
required: false,
},
{
name: "ephemeral",
description: "if true, only you can see the response (default true)",
type: ApplicationCommandOptionType.Boolean,
}
];
async execute(interaction: ChatInputCommandInteraction) {
let recoveryFor = interaction.options.getInteger("recovery-for", false) ?? 1;
const ephemeral = interaction.options.getBoolean("ephemeral", false) ?? true;
if (recoveryFor <= 0) recoveryFor = 1;
const userLimitPromise = getUserLimit(interaction.user, interaction.createdAt);
const nthUseInLimitTimestampPromise = getNthUseInLimitTimestamp(
interaction.user,
interaction.createdAt,
recoveryFor,
);
const userLimit = await userLimitPromise;
const nthUseInLimitTimestamp = await nthUseInLimitTimestampPromise;
if (userLimit === false || nthUseInLimitTimestamp === false) {
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;
}
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>`,
},
]
}],
ephemeral: ephemeral,
});
}
}