Files
blitzkit/src/commands/searchPlayers.ts
T

57 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_DOMAIN_NAMES, RegionDomain } from '../constants/regions';
2023-07-11 19:54:57 +00:00
import getWargamingResponse from '../core/blitz/getWargamingResponse';
import { usernamePattern } from '../core/blitz/listPlayers';
import addServerChoices from '../core/discord/addServerChoices';
import addUsernameChoices from '../core/discord/addUsernameChoices';
import embedInfo from '../core/discord/embedInfo';
import { WARGAMING_APPLICATION_ID } from '../core/node/arguments';
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-07-08 19:39:35 +00:00
inDevelopment: false,
2023-05-02 23:01:54 +00:00
inPublic: true,
command: new SlashCommandBuilder()
.setName('searchplayers')
2023-05-02 23:01:54 +00:00
.setDescription('Search players in a Blitz server')
2023-05-10 20:00:30 +00:00
.addStringOption(addServerChoices)
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) {
const server = interaction.options.getString('server') as RegionDomain;
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-06-12 01:17:26 +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_DOMAIN_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
};