import { AutocompleteInteraction, ApplicationCommandOptionChoiceData } from 'discord.js'; import * as log from './log'; export type Autocomplete = (interaction: AutocompleteInteraction) => Promise[]> export function set(fns: Record): Autocomplete { return async (interaction: AutocompleteInteraction) => { const focused = interaction.options.getFocused(true); const fn = fns[focused.name]; if (!fn) return []; return fn(interaction); }; } export function autocomplete(fn: Autocomplete): (interaction: AutocompleteInteraction) => Promise { return async (interaction: AutocompleteInteraction) => { try { const arr = await fn(interaction); if (arr.length > 25) log.warn(`Autocomplete for ${interaction.options.getFocused(true).name} exceeded limit of 25 autocomplete results`); return interaction.respond(arr.slice(0, 25)); } catch (err) { log.error(err); return interaction.respond([]); } }; }