2023-05-14 17:13:42 +00:00
|
|
|
import {
|
|
|
|
|
ApplicationCommandOptionChoiceData,
|
|
|
|
|
AutocompleteInteraction,
|
|
|
|
|
CacheType,
|
|
|
|
|
} from 'discord.js';
|
2023-10-20 10:28:20 -05:00
|
|
|
import { REGION_NAMES_SHORT } from '../../constants/regions';
|
2023-10-14 11:32:02 -05:00
|
|
|
import searchPlayersAcrossRegions from '../blitz/searchPlayersAcrossRegions';
|
2023-11-01 11:45:19 -05:00
|
|
|
import {
|
|
|
|
|
DISCORD_CHOICES_MAX_NAME_SIZE,
|
|
|
|
|
OVERFLOW_SUFFIX,
|
|
|
|
|
} from './autocompleteClan';
|
2023-05-14 17:13:42 +00:00
|
|
|
|
2023-06-27 02:22:28 +00:00
|
|
|
export default async function autocompleteUsername(
|
2023-05-14 17:13:42 +00:00
|
|
|
interaction: AutocompleteInteraction<CacheType>,
|
|
|
|
|
) {
|
|
|
|
|
const focusedOption = interaction.options.getFocused(true);
|
|
|
|
|
if (focusedOption.name !== 'username') return;
|
2023-10-14 11:15:42 -05:00
|
|
|
const players = await searchPlayersAcrossRegions(focusedOption.value);
|
2023-05-14 17:13:42 +00:00
|
|
|
|
2023-05-28 16:56:15 +00:00
|
|
|
try {
|
|
|
|
|
await interaction.respond(
|
|
|
|
|
players
|
2023-11-01 11:45:19 -05:00
|
|
|
? 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>;
|
|
|
|
|
})
|
2023-05-28 16:56:15 +00:00
|
|
|
: [],
|
|
|
|
|
);
|
2023-10-11 20:18:51 -05:00
|
|
|
} catch (error) {}
|
2023-05-14 17:13:42 +00:00
|
|
|
}
|