Files
blitzkit/packages/bot/src/commands/searchPlayers.ts
T

83 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-09-08 10:17:40 -05:00
import {
2025-10-13 23:15:32 -05:00
type AccountList,
2024-09-08 10:17:40 -05:00
fetchBlitz,
2025-10-13 23:15:32 -05:00
type Region,
2024-09-08 10:17:40 -05:00
usernamePattern,
2025-10-02 10:15:38 -05:00
} from "@blitzkit/core";
import { literals } from "@blitzkit/i18n";
import { Locale } from "discord.js";
import markdownEscape from "markdown-escape";
import { addRegionChoices } from "../core/discord/addRegionChoices";
import { addUsernameChoices } from "../core/discord/addUsernameChoices";
import { createLocalizedCommand } from "../core/discord/createLocalizedCommand";
import { embedInfo } from "../core/discord/embedInfo";
import { localizationObject } from "../core/discord/localizationObject";
import { translator } from "../core/localization/translator";
2025-10-13 23:15:32 -05:00
import type { 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({
2025-10-02 10:15:38 -05:00
command: createLocalizedCommand("search-players")
2024-03-06 15:34:10 -06:00
.addStringOption(addRegionChoices)
.addStringOption((option) =>
2025-10-02 10:15:38 -05:00
addUsernameChoices(option).setAutocomplete(false).setRequired(true)
2024-03-06 15:34:10 -06:00
)
.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,
2025-10-02 10:15:38 -05:00
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,
2025-10-02 10:15:38 -05:00
{ limit: 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,
2025-10-02 10:15:38 -05:00
{ limit: DEFAULT_LIMIT }
)
2024-03-07 18:27:05 -06:00
)
2024-03-06 15:34:10 -06:00
.setMinValue(1)
2025-10-02 10:15:38 -05:00
.setMaxValue(100)
2024-03-06 15:34:10 -06:00
),
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);
2025-10-02 10:15:38 -05:00
const region = interaction.options.getString("region") as Region;
const name = interaction.options.getString("username")!;
const limit = interaction.options.getInteger("limit") ?? 25;
2024-03-06 15:34:10 -06:00
const trimmedSearch = name.trim();
const players = usernamePattern.test(trimmedSearch)
2025-10-02 10:15:38 -05:00
? await fetchBlitz<AccountList>(region, "account/list", {
2024-04-15 13:19:25 -05:00
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-10-02 10:15:38 -05:00
literals(strings.bot.commands.search_players.body.title, {
search: markdownEscape(trimmedSearch),
region: strings.common.regions.normal[region],
}),
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
2025-10-02 10:15:38 -05:00
: players.map((player) => player.nickname).join("\n")
}\`\`\``
2024-03-06 15:34:10 -06:00
);
},
});
});