Files
blitzkit/src/commands/inactive.tsx
T

89 lines
3.3 KiB
TypeScript
Raw Normal View History

2024-03-06 17:19:00 -06:00
import { Locale } from 'discord.js';
2024-03-06 09:52:09 -06:00
import CommandWrapper from '../components/CommandWrapper';
2023-07-11 19:54:57 +00:00
import GenericStats from '../components/GenericStats';
2024-03-06 16:26:36 -06:00
import NoData from '../components/NoData';
2023-07-11 19:54:57 +00:00
import TitleBar from '../components/TitleBar';
2023-10-14 11:06:03 -05:00
import { getAccountInfo } from '../core/blitz/getAccountInfo';
import { getClanInfo } from '../core/blitz/getClanInfo';
2023-10-14 11:32:02 -05:00
import addClanChoices from '../core/discord/addClanChoices';
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';
2023-10-14 11:36:10 -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({
inProduction: true,
inPublic: true,
2023-06-06 01:31:16 +00:00
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) => {
const { translate } = translator(Locale.EnglishUS);
return option
2024-03-06 15:34:10 -06:00
.setName('threshold')
2024-03-06 17:19:00 -06:00
.setNameLocalizations(
localizationObject('bot.commands.inactive.options.threshold'),
2024-03-06 15:34:10 -06:00
)
2024-03-06 17:19:00 -06:00
.setDescription(
translate('bot.commands.inactive.options.threshold.description'),
)
.setDescriptionLocalizations(
localizationObject(
'bot.commands.inactive.options.threshold.description',
),
)
.setMinValue(0);
}),
2024-03-06 15:34:10 -06:00
async handler(interaction) {
2024-03-06 17:19:00 -06:00
const { translate } = 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,
translate('bot.commands.inactive.body.listing', [`${days}`]),
] 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`}
2024-03-06 17:19:00 -06:00
description={`${translate('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,
});
});