From 46e2c00ab1b6be6432979c76f9a7fb39f38650de Mon Sep 17 00:00:00 2001 From: Wroclaw Date: Wed, 10 May 2023 03:04:45 +0200 Subject: [PATCH] add check-limit command --- src/commands/check-limit.ts | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 src/commands/check-limit.ts diff --git a/src/commands/check-limit.ts b/src/commands/check-limit.ts new file mode 100644 index 0000000..a21c272 --- /dev/null +++ b/src/commands/check-limit.ts @@ -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 + ``, + }, + ] + }], + ephemeral: ephemeral, + }); + } +}