Files
blitzkit/src/commands/playerAchievements.ts
T

68 lines
2.3 KiB
TypeScript
Raw Normal View History

2023-05-20 00:48:52 +00:00
import { SlashCommandBuilder } from 'discord.js';
2023-06-21 01:52:26 +00:00
import { startCase } from 'lodash';
2023-05-20 00:48:52 +00:00
import markdownEscape from 'markdown-escape';
2023-10-14 11:06:03 -05:00
import { getAccountAchievements } from '../core/blitz/getAccountAchievements';
import { getAccountInfo } from '../core/blitz/getAccountInfo';
2023-10-14 11:32:02 -05:00
import addUsernameChoices from '../core/discord/addUsernameChoices';
import autocompleteUsername from '../core/discord/autocompleteUsername';
import embedInfo from '../core/discord/embedInfo';
2023-10-14 11:36:10 -05:00
import markdownTable, { TableInputEntry } from '../core/discord/markdownTable';
import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand';
2023-07-11 19:54:57 +00:00
import { CommandRegistry } from '../events/interactionCreate';
2023-05-20 00:48:52 +00:00
type SortBy = 'name' | 'count';
2023-07-05 01:15:18 +00:00
export const playerAchievementsCommand: CommandRegistry = {
2023-05-20 00:48:52 +00:00
inProduction: true,
inPublic: true,
command: new SlashCommandBuilder()
2023-08-04 02:24:14 +00:00
.setName('player-achievements')
2023-05-20 00:48:52 +00:00
.setDescription("All the player's achievements")
2023-06-27 02:22:28 +00:00
.addStringOption(addUsernameChoices)
2023-05-20 00:48:52 +00:00
.addStringOption((option) =>
option
.setName('sort')
.setDescription('What to sort by (default: name)')
.addChoices(
{ name: 'By name', value: 'name' satisfies SortBy },
{ name: 'By count', value: 'count' satisfies SortBy },
),
),
2023-06-27 01:43:30 +00:00
async handler(interaction) {
2023-05-20 00:48:52 +00:00
const sortBy = (interaction.options.getString('sort') ?? 'name') as SortBy;
2023-06-27 02:07:14 +00:00
const account = await resolvePlayerFromCommand(interaction);
2023-10-12 13:00:33 -05:00
const { id, region: region } = account;
const accountInfo = await getAccountInfo(region, id);
const accountsAchievements = await getAccountAchievements(region, id);
const accountAchievements = accountsAchievements;
2023-05-20 00:48:52 +00:00
const compound = {
...accountAchievements.achievements,
...accountAchievements.max_series,
};
2023-06-27 02:22:28 +00:00
return embedInfo(
2023-10-12 13:00:33 -05:00
`${markdownEscape(accountInfo.nickname)}'s information`,
2023-05-20 00:48:52 +00:00
2023-06-27 02:22:28 +00:00
markdownTable(
2023-06-16 02:01:06 +00:00
Object.keys(compound)
.filter((achievement) => compound[achievement] > 0)
.sort(
sortBy === 'count'
? (a, b) => compound[b] - compound[a]
: undefined,
)
.map(
(achievement) =>
[
startCase(achievement),
compound[achievement],
] as TableInputEntry,
2023-05-20 00:48:52 +00:00
),
2023-06-16 02:01:06 +00:00
),
);
2023-05-20 00:48:52 +00:00
},
2023-06-27 02:22:28 +00:00
autocomplete: autocompleteUsername,
2023-07-05 01:15:18 +00:00
};