2024-09-08 10:17:40 -05:00
|
|
|
import {
|
|
|
|
|
AccountList,
|
|
|
|
|
fetchBlitz,
|
|
|
|
|
Region,
|
|
|
|
|
usernamePattern,
|
|
|
|
|
} from '@blitzkit/core';
|
2025-01-16 13:48:13 -06:00
|
|
|
import { literals } from '@blitzkit/i18n';
|
2024-03-07 18:27:05 -06:00
|
|
|
import { Locale } from 'discord.js';
|
2023-05-06 17:45:43 +00:00
|
|
|
import markdownEscape from 'markdown-escape';
|
2024-09-08 10:15:42 -05:00
|
|
|
import { addRegionChoices } from '../core/discord/addRegionChoices';
|
|
|
|
|
import { addUsernameChoices } from '../core/discord/addUsernameChoices';
|
2024-03-07 18:27:05 -06:00
|
|
|
import { createLocalizedCommand } from '../core/discord/createLocalizedCommand';
|
2024-09-08 10:15:42 -05:00
|
|
|
import { embedInfo } from '../core/discord/embedInfo';
|
2024-03-07 18:27:05 -06:00
|
|
|
import { localizationObject } from '../core/discord/localizationObject';
|
|
|
|
|
import { translator } from '../core/localization/translator';
|
2023-07-11 19:54:57 +00:00
|
|
|
import { CommandRegistry } from '../events/interactionCreate';
|
2023-05-02 23:01:54 +00:00
|
|
|
|
2024-03-07 18:27:05 -06:00
|
|
|
const DEFAULT_LIMIT = 25;
|
|
|
|
|
|
2024-03-06 15:34:10 -06:00
|
|
|
export const searchPlayersCommand = new Promise<CommandRegistry>((resolve) => {
|
2025-01-16 13:48:13 -06:00
|
|
|
const { strings } = translator(Locale.EnglishUS);
|
2024-03-07 18:27:05 -06:00
|
|
|
|
2024-03-06 15:34:10 -06:00
|
|
|
resolve({
|
2024-03-07 18:27:05 -06:00
|
|
|
command: createLocalizedCommand('search-players')
|
2024-03-06 15:34:10 -06:00
|
|
|
.addStringOption(addRegionChoices)
|
|
|
|
|
.addStringOption((option) =>
|
|
|
|
|
addUsernameChoices(option).setAutocomplete(false).setRequired(true),
|
|
|
|
|
)
|
|
|
|
|
.addIntegerOption((option) =>
|
|
|
|
|
option
|
2025-01-16 14:22:30 -06:00
|
|
|
.setName(strings.bot.commands.search_players.options.limit.name)
|
2024-03-07 18:27:05 -06:00
|
|
|
.setNameLocalizations(
|
2024-12-19 11:04:48 -06:00
|
|
|
localizationObject(
|
2025-01-16 14:22:30 -06:00
|
|
|
(strings) =>
|
|
|
|
|
strings.bot.commands.search_players.options.limit.name,
|
2024-12-19 11:04:48 -06:00
|
|
|
undefined,
|
|
|
|
|
true,
|
|
|
|
|
),
|
2024-03-07 18:27:05 -06:00
|
|
|
)
|
|
|
|
|
.setDescription(
|
2025-01-16 13:48:13 -06:00
|
|
|
literals(
|
|
|
|
|
strings.bot.commands.search_players.options.limit.description,
|
|
|
|
|
[`${DEFAULT_LIMIT}`],
|
|
|
|
|
),
|
2024-03-07 18:27:05 -06:00
|
|
|
)
|
|
|
|
|
.setDescriptionLocalizations(
|
|
|
|
|
localizationObject(
|
2025-01-16 13:48:13 -06:00
|
|
|
(strings) =>
|
|
|
|
|
strings.bot.commands.search_players.options.limit.description,
|
2024-03-07 18:27:05 -06:00
|
|
|
[`${DEFAULT_LIMIT}`],
|
|
|
|
|
),
|
|
|
|
|
)
|
2024-03-06 15:34:10 -06:00
|
|
|
.setMinValue(1)
|
|
|
|
|
.setMaxValue(100),
|
|
|
|
|
),
|
2023-05-02 23:01:54 +00:00
|
|
|
|
2024-03-06 15:34:10 -06:00
|
|
|
async handler(interaction) {
|
2025-01-16 13:48:13 -06:00
|
|
|
const { strings } = translator(interaction.locale);
|
2024-04-15 13:19:25 -05:00
|
|
|
const region = interaction.options.getString('region') as Region;
|
2024-03-06 15:34:10 -06:00
|
|
|
const name = interaction.options.getString('username')!;
|
|
|
|
|
const limit = interaction.options.getInteger('limit') ?? 25;
|
|
|
|
|
const trimmedSearch = name.trim();
|
|
|
|
|
const players = usernamePattern.test(trimmedSearch)
|
2024-04-15 13:19:25 -05:00
|
|
|
? await fetchBlitz<AccountList>(region, 'account/list', {
|
|
|
|
|
search: trimmedSearch,
|
|
|
|
|
limit,
|
|
|
|
|
})
|
2024-03-06 15:34:10 -06:00
|
|
|
: [];
|
2023-05-08 01:45:57 +00:00
|
|
|
|
2024-03-06 15:34:10 -06:00
|
|
|
return embedInfo(
|
2025-01-16 13:48:13 -06:00
|
|
|
literals(strings.bot.commands.search_players.body.title, [
|
2024-03-07 18:27:05 -06:00
|
|
|
markdownEscape(trimmedSearch),
|
2025-01-16 13:48:13 -06:00
|
|
|
strings.common.regions.normal[region],
|
2024-03-07 18:27:05 -06:00
|
|
|
]),
|
2024-03-06 15:34:10 -06:00
|
|
|
`\`\`\`${
|
|
|
|
|
players.length === 0
|
2025-01-16 13:48:13 -06:00
|
|
|
? strings.bot.commands.search_players.body.no_results
|
2024-03-06 15:34:10 -06:00
|
|
|
: players.map((player) => player.nickname).join('\n')
|
|
|
|
|
}\`\`\``,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
});
|