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

96 lines
3.5 KiB
TypeScript
Raw Normal View History

2024-09-08 10:17:40 -05:00
import { getAccountInfo, getClanInfo } from '@blitzkit/core';
2025-01-15 20:14:05 -06:00
import { literals } from '@blitzkit/i18n';
2024-03-06 17:19:00 -06:00
import { Locale } from 'discord.js';
2024-09-08 10:11:24 -05:00
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';
2024-09-08 10:15:42 -05:00
import { autocompleteClan } from '../core/discord/autocompleteClan';
2024-03-06 17:19:00 -06:00
import { createLocalizedCommand } from '../core/discord/createLocalizedCommand';
import { localizationObject } from '../core/discord/localizationObject';
2024-09-08 10:15:42 -05:00
import { resolveClanFromCommand } from '../core/discord/resolveClanFromCommand';
2024-03-06 17:19:00 -06:00
import { translator } from '../core/localization/translator';
2023-07-11 19:54:57 +00:00
import { 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({
2024-03-06 17:19:00 -06: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
2024-03-06 15:34:10 -06: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,
true,
),
2024-03-06 15:34:10 -06:00
)
2024-03-06 17:19:00 -06:00
.setDescription(
2025-01-15 20:14:05 -06: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) =>
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 =
interaction.options.getNumber('threshold')! ?? DEFAULT_THRESHOLD;
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,
] 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-01-15 20:14:05 -06:00
literals(strings.bot.commands.inactive.body.listing, [
2024-03-07 18:27:05 -06:00
days.toFixed(0),
]),
2024-03-06 17:19:00 -06:00
] 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,
[`${threshold}`],
)} • ${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,
});
});