feat: search min and max lengths

This commit is contained in:
tresabhi committed 2024-12-14 13:27:45 -06:00
1 parent 3b429a6c05
commit 73cb3873d9
6 files changed
+64 -7

No files matched your search

@@ -0,0 +1,23 @@
import { searchClansAcrossRegions } from '@blitzkit/core';
import { Locale } from 'discord.js';
import { UserError } from '../blitzkit/userError';
import { translator } from '../localization/translator';
export async function searchClansAcrossRegionsBotWrapper(
search: string,
locale: Locale,
limit?: number,
) {
const { t } = translator(locale);
const trimmed = search.trim();
if (trimmed.length < 3) {
throw new UserError(t`bot.common.errors.query_too_small`);
}
if (trimmed.length > 100) {
throw new UserError(t`bot.common.errors.query_too_long`);
}
return await searchClansAcrossRegions(trimmed, limit);
}
@@ -0,0 +1,23 @@
import { searchPlayersAcrossRegions } from '@blitzkit/core';
import { Locale } from 'discord.js';
import { UserError } from '../blitzkit/userError';
import { translator } from '../localization/translator';
export async function searchPlayersAcrossRegionsBotWrapper(
search: string,
locale: Locale,
limit?: number,
) {
const { t } = translator(locale);
const trimmed = search.trim();
if (trimmed.length < 3) {
throw new UserError(t`bot.common.errors.query_too_small`);
}
if (trimmed.length > 100) {
throw new UserError(t`bot.common.errors.query_too_long`);
}
return await searchPlayersAcrossRegions(trimmed, limit);
}
@@ -1,6 +1,7 @@
import { Region, searchClansAcrossRegions } from '@blitzkit/core'; import { Region } from '@blitzkit/core';
import { ChatInputCommandInteraction } from 'discord.js'; import { ChatInputCommandInteraction } from 'discord.js';
import markdownEscape from 'markdown-escape'; import markdownEscape from 'markdown-escape';
import { searchClansAcrossRegionsBotWrapper } from '../blitz/searchClansAcrossRegionsBotWrapper';
import { UserError } from '../blitzkit/userError'; import { UserError } from '../blitzkit/userError';
import { translator } from '../localization/translator'; import { translator } from '../localization/translator';
@@ -16,7 +17,10 @@ export async function resolveClanFromCommand(
const [server, accountId] = clan.split('/'); const [server, accountId] = clan.split('/');
return { region: server as Region, id: Number(accountId) }; return { region: server as Region, id: Number(accountId) };
} else { } else {
const accounts = await searchClansAcrossRegions(clan); const accounts = await searchClansAcrossRegionsBotWrapper(
clan,
interaction.locale,
);
if (accounts[0]) { if (accounts[0]) {
return { region: accounts[0].region, id: accounts[0].clan_id }; return { region: accounts[0].region, id: accounts[0].clan_id };
@@ -1,6 +1,7 @@
import { idToRegion, Region, searchPlayersAcrossRegions } from '@blitzkit/core'; import { idToRegion, Region } from '@blitzkit/core';
import { CacheType, ChatInputCommandInteraction } from 'discord.js'; import { CacheType, ChatInputCommandInteraction } from 'discord.js';
import markdownEscape from 'markdown-escape'; import markdownEscape from 'markdown-escape';
import { searchPlayersAcrossRegionsBotWrapper } from '../../blitz/searchPlayersAcrossRegionsBotWrapper';
import { getBlitzFromDiscord } from '../../blitzkit/getBlitzFromDiscord'; import { getBlitzFromDiscord } from '../../blitzkit/getBlitzFromDiscord';
import { UserError } from '../../blitzkit/userError'; import { UserError } from '../../blitzkit/userError';
import { translator } from '../../localization/translator'; import { translator } from '../../localization/translator';
@@ -23,7 +24,10 @@ export async function resolvePlayerFromCommand(
const region = idToRegion(id); const region = idToRegion(id);
return { region, id } satisfies ResolvedPlayer; return { region, id } satisfies ResolvedPlayer;
} else { } else {
const accounts = await searchPlayersAcrossRegions(commandUsername); const accounts = await searchPlayersAcrossRegionsBotWrapper(
commandUsername,
interaction.locale,
);
if (accounts[0]) { if (accounts[0]) {
return { return {
+3 -1
View File
@@ -154,7 +154,9 @@
"no_tank_stats": "# No tank stats available\nThis player doesn't have any stats available. This may not be the player you are looking for.", "no_tank_stats": "# No tank stats available\nThis player doesn't have any stats available. This may not be the player you are looking for.",
"get_help": "Help Discord server", "get_help": "Help Discord server",
"uncaught_error": "# BlitzKit ran into an error!\nWe're so sorry about this. Feel free to [join the official Discord server](https://discord.gg/nDt7AjGJQH) to get help.", "uncaught_error": "# BlitzKit ran into an error!\nWe're so sorry about this. Feel free to [join the official Discord server](https://discord.gg/nDt7AjGJQH) to get help.",
"preview": "# This command is in preview\n\nYou can only access this command in [the official Discord server](https://discord.gg/nDt7AjGJQH). The command will be generally available soon." "preview": "# This command is in preview\n\nYou can only access this command in [the official Discord server](https://discord.gg/nDt7AjGJQH). The command will be generally available soon.",
"query_too_small": "# Your search is too short\nYour search must be at least 3 characters long.",
"query_too_long": "# Your search is too long\nYour search must be at most 100 characters long."
}, },
"wn8": { "wn8": {
@@ -11,10 +11,11 @@ export type AccountListWithServer = (AccountListItem & {
})[]; })[];
export async function searchPlayersAcrossRegions(search: string, limit = 25) { export async function searchPlayersAcrossRegions(search: string, limit = 25) {
const trimmedSearch = search.trim(); const trimmed = search.trim();
if (trimmed.length < 3 && trimmed.length > 100) return [];
const normalizedLimit = Math.floor(limit / 3); const normalizedLimit = Math.floor(limit / 3);
if (usernamePattern.test(trimmedSearch)) { if (usernamePattern.test(trimmed)) {
return ( return (
await Promise.all([ await Promise.all([
fetchBlitz<AccountList>('com', 'account/list', { fetchBlitz<AccountList>('com', 'account/list', {