Files
blitzkit/src/commands/searchPlayers.ts
T

55 lines
2.1 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-07-11 19:54:57 +00:00
import getWargamingResponse from '../core/blitz/getWargamingResponse';
import { usernamePattern } from '../core/blitz/listPlayers';
2023-07-18 00:23:59 +00:00
import addRegionChoices from '../core/discord/addRegionChoices';
2023-07-11 19:54:57 +00:00
import addUsernameChoices from '../core/discord/addUsernameChoices';
import embedInfo from '../core/discord/embedInfo';
2023-07-16 18:26:33 +00:00
import { secrets } from '../core/node/secrets';
2023-07-11 19:54:57 +00:00
import { CommandRegistry } from '../events/interactionCreate';
import { AccountList } from '../types/accountList';
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,
2023-08-06 19:07:44 +00:00
inDevelopment: true,
2023-05-02 23:01:54 +00:00
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-06-02 00:28:34 +00:00
? await getWargamingResponse<AccountList>(
2023-07-16 18:26:33 +00:00
`https://api.wotblitz.${server}/wotb/account/list/?application_id=${
secrets.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
};