Files
blitzkit/src/commands/inactive.tsx
T

88 lines
3.2 KiB
TypeScript
Raw Normal View History

2023-06-06 01:31:16 +00:00
import { SlashCommandBuilder } from 'discord.js';
import GenericStats from '../components/GenericStats/index.js';
import NoData, { NoDataType } from '../components/NoData.js';
import PoweredByWargaming from '../components/PoweredByWargaming.js';
import TitleBar from '../components/TitleBar.js';
import Wrapper from '../components/Wrapper.js';
import { BLITZ_SERVERS } from '../constants/servers.js';
import getBlitzClan from '../core/blitz/getBlitzClan.js';
import getWargamingResponse from '../core/blitz/getWargamingResponse.js';
2023-06-27 01:46:08 +00:00
import addClanChoices from '../core/discord/addClanChoices.js';
2023-06-27 02:22:28 +00:00
import autocompleteClan from '../core/discord/autocompleteClan.js';
2023-06-27 17:22:27 +00:00
import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js';
2023-06-25 02:32:56 +00:00
import { CommandRegistry } from '../events/interactionCreate/index.js';
2023-06-06 01:31:16 +00:00
import { AccountInfo } from '../types/accountInfo.js';
import { ClanInfo } from '../types/clanInfo.js';
const DEFAULT_THRESHOLD = 7;
export default {
inProduction: true,
2023-06-23 17:14:27 +00:00
inDevelopment: false,
2023-06-06 01:31:16 +00:00
inPublic: true,
command: new SlashCommandBuilder()
.setName('inactive')
2023-06-06 01:31:16 +00:00
.setDescription('Lists all inactive players')
.addStringOption(addClanChoices)
.addNumberOption((option) =>
option
.setName('threshold')
.setDescription(
`The number of days inactive (default: ${DEFAULT_THRESHOLD})`,
)
.setMinValue(0),
),
2023-06-27 01:43:30 +00:00
async handler(interaction) {
2023-06-06 01:31:16 +00:00
const clanName = interaction.options.getString('clan')!;
2023-06-27 02:13:57 +00:00
const { server, id } = await getBlitzClan(clanName);
2023-06-06 01:31:16 +00:00
const threshold =
interaction.options.getNumber('threshold')! ?? DEFAULT_THRESHOLD;
const time = new Date().getTime() / 1000;
const clanData = (
await getWargamingResponse<ClanInfo>(
2023-06-12 01:17:26 +00:00
`https://api.wotblitz.${server}/wotb/clans/info/?application_id=${WARGAMING_APPLICATION_ID}&clan_id=${id}`,
2023-06-06 01:31:16 +00:00
)
)[id];
const memberIds = clanData.members_ids;
const accounts = await getWargamingResponse<AccountInfo>(
2023-06-12 01:17:26 +00:00
`https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${memberIds.join(
',',
)}`,
2023-06-06 01:31:16 +00:00
);
const inactive = memberIds
.map((memberId) => {
const member = accounts[memberId];
const inactiveDays = (time - member.last_battle_time) / 60 / 60 / 24;
return [member.nickname, inactiveDays] as [string, number];
})
.filter(([, inactiveDays]) => inactiveDays >= threshold)
.sort((a, b) => b[1] - a[1])
.map(
([name, days]) => [name, `${days.toFixed(0)} days`] as [string, string],
);
const hasInactiveMembers = inactive.length > 0;
2023-06-16 02:01:06 +00:00
return (
2023-06-06 01:31:16 +00:00
<Wrapper>
<TitleBar
name={clanData.name}
nameDiscriminator="(Inactivity)"
image={`https://wotblitz-gc.gcdn.co/icons/clanEmblems1x/clan-icon-v2-${clanData.emblem_set_id}.png`}
description={`${threshold}+ Days • ${new Date().toDateString()} • ${
BLITZ_SERVERS[server]
}`}
/>
2023-06-06 16:53:13 +00:00
{!hasInactiveMembers && <NoData type={NoDataType.PlayersInPeriod} />}
2023-06-06 01:31:16 +00:00
{hasInactiveMembers && <GenericStats stats={inactive} />}
<PoweredByWargaming />
2023-06-16 02:01:06 +00:00
</Wrapper>
2023-06-06 01:31:16 +00:00
);
},
2023-06-27 02:22:28 +00:00
autocomplete: autocompleteClan,
2023-06-06 01:31:16 +00:00
} satisfies CommandRegistry;