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 limit and 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) { 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; } 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 ``, }, ] }], ephemeral: ephemeral, }); } }