mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: autocomplete size limits
This commit is contained in:
1 parent
882e61ff44
commit
3ea77f5899
5 files changed
+88
-45
No files matched your search
@@ -9,43 +9,45 @@ export type ClanListWithRegion = (Clan & {
|
|||||||
|
|
||||||
export default async function searchClansAcrossRegions(
|
export default async function searchClansAcrossRegions(
|
||||||
search: string,
|
search: string,
|
||||||
limit = 9,
|
limit = 25,
|
||||||
) {
|
) {
|
||||||
const trimmed = search.trim();
|
const trimmed = search.trim();
|
||||||
if (trimmed.length < 3 && trimmed.length > 100) return [];
|
if (trimmed.length < 3 && trimmed.length > 100) return [];
|
||||||
const normalizedLimit = Math.round(limit / 3);
|
const normalizedLimit = Math.floor(limit / 3);
|
||||||
|
|
||||||
|
// TODO: remove splice once wargaming fixes the functionality of the limit felid
|
||||||
|
|
||||||
return (
|
return (
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
fetchBlitz<ClanList>(
|
fetchBlitz<ClanList>(
|
||||||
`https://api.wotblitz.com/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
|
`https://api.wotblitz.com/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
|
||||||
).then(
|
).then((value) =>
|
||||||
(value) =>
|
value
|
||||||
value &&
|
.map((account) => ({
|
||||||
value.map((account) => ({
|
|
||||||
...account,
|
...account,
|
||||||
region: 'com' as Region,
|
region: 'com' as Region,
|
||||||
})),
|
}))
|
||||||
|
.splice(0, normalizedLimit),
|
||||||
),
|
),
|
||||||
fetchBlitz<ClanList>(
|
fetchBlitz<ClanList>(
|
||||||
`https://api.wotblitz.eu/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
|
`https://api.wotblitz.eu/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
|
||||||
).then(
|
).then((value) =>
|
||||||
(value) =>
|
value
|
||||||
value &&
|
.map((account) => ({
|
||||||
value.map((account) => ({
|
|
||||||
...account,
|
...account,
|
||||||
region: 'eu' as Region,
|
region: 'eu' as Region,
|
||||||
})),
|
}))
|
||||||
|
.splice(0, normalizedLimit),
|
||||||
),
|
),
|
||||||
fetchBlitz<ClanList>(
|
fetchBlitz<ClanList>(
|
||||||
`https://api.wotblitz.asia/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
|
`https://api.wotblitz.asia/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`,
|
||||||
).then(
|
).then((value) =>
|
||||||
(value) =>
|
value
|
||||||
value &&
|
.map((account) => ({
|
||||||
value.map((account) => ({
|
|
||||||
...account,
|
...account,
|
||||||
region: 'asia' as Region,
|
region: 'asia' as Region,
|
||||||
})),
|
}))
|
||||||
|
.splice(0, normalizedLimit),
|
||||||
),
|
),
|
||||||
])
|
])
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ export const usernamePatternWithoutPosition = /[a-zA-Z0-9_]{3,24}/;
|
|||||||
|
|
||||||
export default async function searchPlayersAcrossRegions(
|
export default async function searchPlayersAcrossRegions(
|
||||||
search: string,
|
search: string,
|
||||||
limit = 9,
|
limit = 25,
|
||||||
) {
|
) {
|
||||||
const trimmedSearch = search.trim();
|
const trimmedSearch = search.trim();
|
||||||
const normalizedLimit = Math.round(limit / 3);
|
const normalizedLimit = Math.floor(limit / 3);
|
||||||
const encodedSearch = encodeURIComponent(trimmedSearch);
|
const encodedSearch = encodeURIComponent(trimmedSearch);
|
||||||
|
|
||||||
if (usernamePattern.test(trimmedSearch)) {
|
if (usernamePattern.test(trimmedSearch)) {
|
||||||
|
|||||||
@@ -3,26 +3,39 @@ import {
|
|||||||
AutocompleteInteraction,
|
AutocompleteInteraction,
|
||||||
CacheType,
|
CacheType,
|
||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
import { REGION_NAMES } from '../../constants/regions';
|
import { REGION_NAMES_SHORT } from '../../constants/regions';
|
||||||
import searchClansAcrossRegions from '../blitz/searchClansAcrossRegions';
|
import searchClansAcrossRegions from '../blitz/searchClansAcrossRegions';
|
||||||
|
|
||||||
|
export const DISCORD_CHOICES_MAX_NAME_SIZE = 25;
|
||||||
|
export const OVERFLOW_SUFFIX = '…';
|
||||||
|
|
||||||
export default async function autocompleteClan(
|
export default async function autocompleteClan(
|
||||||
interaction: AutocompleteInteraction<CacheType>,
|
interaction: AutocompleteInteraction<CacheType>,
|
||||||
) {
|
) {
|
||||||
const focusedOption = interaction.options.getFocused(true);
|
const focusedOption = interaction.options.getFocused(true);
|
||||||
if (focusedOption.name !== 'clan') return;
|
if (focusedOption.name !== 'clan') return;
|
||||||
if (focusedOption.value.length < 2) return interaction.respond([]);
|
if (focusedOption.value.length < 2) return interaction.respond([]);
|
||||||
const players = await searchClansAcrossRegions(focusedOption.value);
|
const clans = await searchClansAcrossRegions(focusedOption.value);
|
||||||
|
|
||||||
await interaction.respond(
|
await interaction.respond(
|
||||||
players
|
clans
|
||||||
? players.map(
|
? clans.map((clan) => {
|
||||||
(player) =>
|
let name = `${clan.name} (${REGION_NAMES_SHORT[clan.region]})`;
|
||||||
({
|
|
||||||
name: `${player.name} (${REGION_NAMES[player.region]})`,
|
if (name.length > DISCORD_CHOICES_MAX_NAME_SIZE) {
|
||||||
value: `${player.region}/${player.clan_id}`,
|
const overSize = name.length - DISCORD_CHOICES_MAX_NAME_SIZE;
|
||||||
}) satisfies ApplicationCommandOptionChoiceData<string>,
|
|
||||||
)
|
name = `${clan.name.slice(
|
||||||
|
0,
|
||||||
|
clan.name.length - overSize - OVERFLOW_SUFFIX.length,
|
||||||
|
)}${OVERFLOW_SUFFIX} (${REGION_NAMES_SHORT[clan.region]})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
name,
|
||||||
|
value: `${clan.region}/${clan.clan_id}`,
|
||||||
|
} satisfies ApplicationCommandOptionChoiceData<string>;
|
||||||
|
})
|
||||||
: [],
|
: [],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,10 @@ import {
|
|||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
import { go } from 'fuzzysort';
|
import { go } from 'fuzzysort';
|
||||||
import { TANK_NAMES_DIACRITICS } from '../blitzstars/tankopedia';
|
import { TANK_NAMES_DIACRITICS } from '../blitzstars/tankopedia';
|
||||||
|
import {
|
||||||
|
DISCORD_CHOICES_MAX_NAME_SIZE,
|
||||||
|
OVERFLOW_SUFFIX,
|
||||||
|
} from './autocompleteClan';
|
||||||
|
|
||||||
export default async function autocompleteTanks(
|
export default async function autocompleteTanks(
|
||||||
interaction: AutocompleteInteraction<CacheType>,
|
interaction: AutocompleteInteraction<CacheType>,
|
||||||
@@ -18,13 +22,23 @@ export default async function autocompleteTanks(
|
|||||||
go(focusedOption.value, await TANK_NAMES_DIACRITICS, {
|
go(focusedOption.value, await TANK_NAMES_DIACRITICS, {
|
||||||
keys: ['combined'],
|
keys: ['combined'],
|
||||||
limit: 10,
|
limit: 10,
|
||||||
}).map(
|
}).map(async (item) => {
|
||||||
async (item) =>
|
let name = item.obj.original;
|
||||||
({
|
|
||||||
name: item.obj.original,
|
if (name.length > DISCORD_CHOICES_MAX_NAME_SIZE) {
|
||||||
value: `${item.obj.id}`,
|
const overSize = name.length - DISCORD_CHOICES_MAX_NAME_SIZE;
|
||||||
}) satisfies ApplicationCommandOptionChoiceData<string>,
|
|
||||||
),
|
name = `${item.obj.original.slice(
|
||||||
|
0,
|
||||||
|
item.obj.original.length - overSize - OVERFLOW_SUFFIX.length,
|
||||||
|
)}${OVERFLOW_SUFFIX}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
name,
|
||||||
|
value: `${item.obj.id}`,
|
||||||
|
} satisfies ApplicationCommandOptionChoiceData<string>;
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
: [],
|
: [],
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ import {
|
|||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
import { REGION_NAMES_SHORT } from '../../constants/regions';
|
import { REGION_NAMES_SHORT } from '../../constants/regions';
|
||||||
import searchPlayersAcrossRegions from '../blitz/searchPlayersAcrossRegions';
|
import searchPlayersAcrossRegions from '../blitz/searchPlayersAcrossRegions';
|
||||||
|
import {
|
||||||
|
DISCORD_CHOICES_MAX_NAME_SIZE,
|
||||||
|
OVERFLOW_SUFFIX,
|
||||||
|
} from './autocompleteClan';
|
||||||
|
|
||||||
export default async function autocompleteUsername(
|
export default async function autocompleteUsername(
|
||||||
interaction: AutocompleteInteraction<CacheType>,
|
interaction: AutocompleteInteraction<CacheType>,
|
||||||
@@ -16,15 +20,25 @@ export default async function autocompleteUsername(
|
|||||||
try {
|
try {
|
||||||
await interaction.respond(
|
await interaction.respond(
|
||||||
players
|
players
|
||||||
? players.map(
|
? players.map((player) => {
|
||||||
(player) =>
|
let name = `${player.nickname} (${
|
||||||
({
|
REGION_NAMES_SHORT[player.region]
|
||||||
name: `${player.nickname} (${
|
})`;
|
||||||
REGION_NAMES_SHORT[player.region]
|
|
||||||
})`,
|
if (name.length > DISCORD_CHOICES_MAX_NAME_SIZE) {
|
||||||
value: `${player.region}/${player.account_id}`,
|
const overSize = name.length - DISCORD_CHOICES_MAX_NAME_SIZE;
|
||||||
}) satisfies ApplicationCommandOptionChoiceData<string>,
|
|
||||||
)
|
name = `${player.nickname.slice(
|
||||||
|
0,
|
||||||
|
player.nickname.length - overSize - OVERFLOW_SUFFIX.length,
|
||||||
|
)}${OVERFLOW_SUFFIX} (${REGION_NAMES_SHORT[player.region]})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
name,
|
||||||
|
value: `${player.region}/${player.account_id}`,
|
||||||
|
} satisfies ApplicationCommandOptionChoiceData<string>;
|
||||||
|
})
|
||||||
: [],
|
: [],
|
||||||
);
|
);
|
||||||
} catch (error) {}
|
} catch (error) {}
|
||||||
|
|||||||
Reference in new issue
Block a user