Files
blitzkit/src/commands/searchPlayers.ts
T

56 lines
2.0 KiB
TypeScript
Raw Normal View History

2023-05-28 16:56:15 +00:00
import { SlashCommandBuilder } from 'discord.js';
2023-05-06 17:45:43 +00:00
import markdownEscape from 'markdown-escape';
import { REGION_NAMES, Region } from '../constants/regions';
2023-09-23 21:51:00 +00:00
import { WARGAMING_APPLICATION_ID } from '../constants/wargamingApplicationID';
2023-10-20 10:50:54 -05:00
import fetchBlitz from '../core/blitz/fetchBlitz';
2023-10-14 11:15:42 -05:00
import {
AccountList,
usernamePattern,
} from '../core/blitz/searchPlayersAcrossRegions';
2023-10-14 11:32:02 -05:00
import addRegionChoices from '../core/discord/addRegionChoices';
import addUsernameChoices from '../core/discord/addUsernameChoices';
import embedInfo from '../core/discord/embedInfo';
2023-07-11 19:54:57 +00:00
import { CommandRegistry } from '../events/interactionCreate';
2023-05-02 23:01:54 +00:00
2023-07-05 01:15:18 +00:00
export const searchPlayersCommand: CommandRegistry = {
2023-05-02 23:01:54 +00:00
inProduction: true,
inPublic: true,
command: new SlashCommandBuilder()
2023-08-04 02:24:14 +00:00
.setName('search-players')
2023-05-02 23:01:54 +00:00
.setDescription('Search players in a Blitz server')
2023-07-18 00:23:59 +00:00
.addStringOption(addRegionChoices)
2023-05-14 17:13:42 +00:00
.addStringOption((option) =>
2023-06-27 02:22:28 +00:00
addUsernameChoices(option).setAutocomplete(false).setRequired(true),
2023-05-14 17:13:42 +00:00
)
2023-05-02 23:01:54 +00:00
.addIntegerOption((option) =>
option
.setName('limit')
.setDescription('The size of the search result (default: 25)')
.setMinValue(1)
.setMaxValue(100),
),
2023-06-27 01:43:30 +00:00
async handler(interaction) {
2023-07-18 00:23:59 +00:00
const server = interaction.options.getString('region') as Region;
2023-05-14 17:13:42 +00:00
const name = interaction.options.getString('username')!;
2023-05-02 23:01:54 +00:00
const limit = interaction.options.getInteger('limit') ?? 25;
2023-06-02 00:28:34 +00:00
const trimmedSearch = name.trim();
const players = usernamePattern.test(trimmedSearch)
2023-10-14 10:58:48 -05:00
? await fetchBlitz<AccountList>(
2023-09-23 21:51:00 +00:00
`https://api.wotblitz.${server}/wotb/account/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${encodeURIComponent(
trimmedSearch,
)}&limit=${limit}`,
2023-06-02 00:28:34 +00:00
)
: [];
2023-05-08 01:45:57 +00:00
2023-06-27 02:22:28 +00:00
return embedInfo(
`Player search for "${markdownEscape(name)}" in ${REGION_NAMES[server]}`,
2023-06-16 02:01:06 +00:00
`\`\`\`${
players.length === 0
? 'No players found.'
: players.map((player) => player.nickname).join('\n')
}\`\`\``,
);
2023-05-02 23:01:54 +00:00
},
2023-07-05 01:15:18 +00:00
};