add check-limit command
This commit is contained in:
parent
48b9ec02a0
commit
46e2c00ab1
1 changed files with 68 additions and 0 deletions
68
src/commands/check-limit.ts
Normal file
68
src/commands/check-limit.ts
Normal 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,
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue