feat: autocomplete size limits

This commit is contained in:
TrèsAbhi committed 2023-11-01 11:45:19 -05:00
1 parent 882e61ff44
commit 3ea77f5899
5 files changed
+88 -45

No files matched your search

+23 -9
View File
@@ -5,6 +5,10 @@ import {
} from 'discord.js';
import { REGION_NAMES_SHORT } from '../../constants/regions';
import searchPlayersAcrossRegions from '../blitz/searchPlayersAcrossRegions';
import {
DISCORD_CHOICES_MAX_NAME_SIZE,
OVERFLOW_SUFFIX,
} from './autocompleteClan';
export default async function autocompleteUsername(
interaction: AutocompleteInteraction<CacheType>,
@@ -16,15 +20,25 @@ export default async function autocompleteUsername(
try {
await interaction.respond(
players
? players.map(
(player) =>
({
name: `${player.nickname} (${
REGION_NAMES_SHORT[player.region]
})`,
value: `${player.region}/${player.account_id}`,
}) satisfies ApplicationCommandOptionChoiceData<string>,
)
? players.map((player) => {
let name = `${player.nickname} (${
REGION_NAMES_SHORT[player.region]
})`;
if (name.length > DISCORD_CHOICES_MAX_NAME_SIZE) {
const overSize = name.length - DISCORD_CHOICES_MAX_NAME_SIZE;
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) {}