Compare commits

..

No commits in common. "46e2c00ab1b6be6432979c76f9a7fb39f38650de" and "ae3a5133b3df81a235eaca39aaa09c47b3d96687" have entirely different histories.

3 changed files with 7 additions and 107 deletions

View file

@ -1,68 +0,0 @@
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,
});
}
}

View file

@ -28,11 +28,13 @@ export function getAuthor(request: apiRequest) {
* @param requestTimestamp the timestamp of the user request * @param requestTimestamp the timestamp of the user request
* @returns object containing the limit and remaining usage or `false` if there is no limit * @returns object containing the limit and remaining usage or `false` if there is no limit
*/ */
export async function getUserLimit(user: string | { id: string }, requestTimestamp: Date) { async function getUserLimit(user: string | {id: string}, requestTimestamp: Date) {
const userId: string = typeof user === "string" ? user : user.id; const userId: string = typeof user === "string" ? user : user.id;
const userLimits = await database.limits.findUnique({ const userLimits = await database.limits.findUnique({
where: { user: BigInt(userId) } where: {
user: BigInt(userId)
}
}); });
if (userLimits?.vip) return false; if (userLimits?.vip) return false;
@ -52,39 +54,6 @@ export async function getUserLimit(user: string | { id: string }, requestTimesta
return {limit: userLimits.limit, remaining: userLimits.limit - usedLimit}; return {limit: userLimits.limit, remaining: userLimits.limit - usedLimit};
} }
/**
* gets the timestamp of nth use inside time limit
* @param user the user or id to check
* @param requestTimestamp the timestamp of the request (message/interaction createdAt)
* @param nth which timestamp in time limit to get (orderedd from oldest to newest)
* @returns `false` if user is vip
* @returns `null` if there is no request
* @returns `Date` timestamp of the nth request
*/
export async function getNthUseInLimitTimestamp(user: string | { id: string }, requestTimestamp: Date, nth = 1) {
const userId: string = typeof user === "string" ? user : user.id;
const userLimits = await database.limits.findUnique({
where: { user: BigInt(userId)}
});
if (userLimits?.vip) return false;
const nthUseInLimit = await database.usage.findFirst({
where: {
user: BigInt(userId),
timestamp: {
gte: new Date(requestTimestamp.getTime() - 1000 * 60 * 60 * 24 /* 24 hours */)
}
},
orderBy: { timestamp: "asc" },
skip: nth - 1,
});
if (!nthUseInLimit) return null;
return nthUseInLimit.timestamp;
}
/** /**
* Replies to a request * Replies to a request
* @param request the request to reply to * @param request the request to reply to

View file

@ -22,14 +22,13 @@ const rest = new REST().setToken(config.tokens.Discord);
(async () => { (async () => {
const me = await rest.get(Routes.oauth2CurrentApplication()) as RESTGetAPIOAuth2CurrentApplicationResult; const me = await rest.get(Routes.oauth2CurrentApplication()) as RESTGetAPIOAuth2CurrentApplicationResult;
if (guildId && guildId != "") { console.log(`Started refreshing ${post.length} application commands.`);
console.log(`Started refreshing ${post.length} application guild (${guildId}) commands.`); if (guildId && guildId != "")
await rest.put( await rest.put(
Routes.applicationGuildCommands(me.id, guildId), Routes.applicationGuildCommands(me.id, guildId),
{ body: post }, { body: post },
); );
} else { else {
console.log(`Started refreshing ${post.length} application global commands.`);
await rest.put( await rest.put(
Routes.applicationCommands(me.id), Routes.applicationCommands(me.id),
{ body: post }, { body: post },