Files
blitzkit/packages/bot/src/commands/inactive.tsx
T

96 lines
3.5 KiB
TypeScript
Raw Normal View History

2025-10-01 22:41:23 -05:00
import { getAccountInfo, getClanInfo } from "@blitzkit/core";
import { literals } from "@blitzkit/i18n";
import { Locale } from "discord.js";
import { CommandWrapper } from "../components/CommandWrapper";
import { GenericStats } from "../components/GenericStats";
import { NoData } from "../components/NoData";
import { TitleBar } from "../components/TitleBar";
import { addClanChoices } from "../core/discord/addClanChoices";
import { autocompleteClan } from "../core/discord/autocompleteClan";
import { createLocalizedCommand } from "../core/discord/createLocalizedCommand";
import { localizationObject } from "../core/discord/localizationObject";
import { resolveClanFromCommand } from "../core/discord/resolveClanFromCommand";
import { translator } from "../core/localization/translator";
2025-10-13 23:15:32 -05:00
import type { CommandRegistry } from "../events/interactionCreate";
2023-06-06 01:31:16 +00:00
const DEFAULT_THRESHOLD = 7;
2024-03-06 15:34:10 -06:00
export const inactiveCommand = new Promise<CommandRegistry>((resolve) => {
resolve({
2025-10-01 22:41:23 -05:00
command: createLocalizedCommand("inactive")
2024-03-06 15:34:10 -06:00
.addStringOption(addClanChoices)
2024-03-06 17:19:00 -06:00
.addNumberOption((option) => {
2025-01-15 20:14:05 -06:00
const { strings } = translator(Locale.EnglishUS);
2024-03-06 17:19:00 -06:00
return option
2025-10-01 22:41:23 -05:00
.setName("threshold")
2024-03-06 17:19:00 -06:00
.setNameLocalizations(
2024-12-19 11:04:48 -06:00
localizationObject(
2025-01-16 14:22:30 -06:00
(strings) => strings.bot.commands.inactive.options.threshold.name,
2024-12-19 11:04:48 -06:00
undefined,
2025-10-01 22:41:23 -05:00
true
)
2024-03-06 15:34:10 -06:00
)
2024-03-06 17:19:00 -06:00
.setDescription(
2025-10-01 22:41:23 -05:00
strings.bot.commands.inactive.options.threshold.description
2024-03-06 17:19:00 -06:00
)
.setDescriptionLocalizations(
localizationObject(
2025-01-16 14:10:28 -06:00
(strings) =>
2025-10-01 22:41:23 -05:00
strings.bot.commands.inactive.options.threshold.description
)
2024-03-06 17:19:00 -06:00
)
.setMinValue(0);
}),
2024-03-06 15:34:10 -06:00
async handler(interaction) {
2025-01-15 20:14:05 -06:00
const { strings } = translator(interaction.locale);
2024-03-06 15:34:10 -06:00
const { region, id } = await resolveClanFromCommand(interaction);
const threshold =
2025-10-01 22:41:23 -05:00
interaction.options.getNumber("threshold")! ?? DEFAULT_THRESHOLD;
2024-03-06 15:34:10 -06:00
const time = new Date().getTime() / 1000;
const clanInfo = await getClanInfo(region, id);
const accountInfo = await getAccountInfo(region, clanInfo.members_ids);
const inactive = accountInfo
.map(
(account) =>
[
account.nickname,
(time - account.last_battle_time) / 60 / 60 / 24,
2025-10-01 22:41:23 -05:00
] as [string, number]
2023-06-06 01:31:16 +00:00
)
2024-03-06 15:34:10 -06:00
.filter(([, inactiveDays]) => inactiveDays >= threshold)
.sort((a, b) => b[1] - a[1])
.map(
([name, days]) =>
2024-03-06 17:19:00 -06:00
[
name,
2025-10-01 22:41:23 -05:00
literals(strings.bot.commands.inactive.body.listing, {
days: days.toFixed(0),
}),
] as [string, string]
2024-03-06 15:34:10 -06:00
);
const hasInactiveMembers = inactive.length > 0;
2023-06-06 01:31:16 +00:00
2024-03-06 15:34:10 -06:00
return (
<CommandWrapper>
<TitleBar
title={clanInfo.name}
image={`https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clanInfo.emblem_set_id}.png`}
2025-01-15 20:14:05 -06:00
description={`${literals(
strings.bot.commands.inactive.body.subtitle,
2025-10-01 22:41:23 -05:00
{ days: threshold }
2025-01-15 20:14:05 -06:00
)} • ${new Date().toLocaleDateString(interaction.locale)}`}
2024-03-06 15:34:10 -06:00
/>
2024-03-06 16:26:36 -06:00
{!hasInactiveMembers && (
<NoData type="players_in_period" locale={interaction.locale} />
)}
2024-03-06 15:34:10 -06:00
{hasInactiveMembers && <GenericStats stats={inactive} />}
</CommandWrapper>
2023-06-06 01:31:16 +00:00
);
2024-03-06 15:34:10 -06:00
},
2023-06-06 01:31:16 +00:00
2024-03-06 15:34:10 -06:00
autocomplete: autocompleteClan,
});
});