27 lines
1.0 KiB
TypeScript
27 lines
1.0 KiB
TypeScript
import { AutocompleteInteraction, ApplicationCommandOptionChoiceData } from 'discord.js';
|
|
import * as log from './log';
|
|
|
|
export type Autocomplete = (interaction: AutocompleteInteraction) => Promise<ApplicationCommandOptionChoiceData<string | number>[]>
|
|
|
|
export function set(fns: Record<string, Autocomplete>): 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<void> {
|
|
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([]);
|
|
}
|
|
};
|
|
} |