fix: autocomplete in nested form (#97)

Co-authored-by: Evo <85353424+EvolutionX-10@users.noreply.github.com>
This commit is contained in:
Jacob Nguyen
2022-08-02 13:51:31 -05:00
committed by GitHub
parent b6ea3da23f
commit 70d7bdb8c5
4 changed files with 112 additions and 81 deletions

View File

@@ -23,6 +23,7 @@ import type {
} from 'discord.js';
import { isAutocomplete } from '../utilities/predicates';
import { SernError } from '../structures/errors';
import treeSearch from '../utilities/treeSearch';
export function applicationCommandDispatcher(interaction: Interaction) {
if (isAutocomplete(interaction)) {
@@ -41,10 +42,9 @@ export function applicationCommandDispatcher(interaction: Interaction) {
}
export function dispatchAutocomplete(interaction: AutocompleteInteraction) {
const choice = interaction.options.getFocused(true);
return (mod: BothCommand | SlashCommand) => {
const selectedOption = mod.options?.find(o => o.autocomplete && o.name === choice.name);
if (selectedOption !== undefined && selectedOption.autocomplete) {
const selectedOption = treeSearch(interaction, mod.options);
if (selectedOption !== undefined) {
return {
mod,
execute: () => selectedOption.command.execute(interaction),

View File

@@ -0,0 +1,31 @@
import type { SernOptionsData } from '../structures/module';
import { ApplicationCommandOptionType, AutocompleteInteraction } from 'discord.js';
export default function treeSearch(iAutocomplete: AutocompleteInteraction, options: SernOptionsData[] | undefined) {
if(options === undefined) return undefined;
const _options = options.slice(); // required to prevent direct mutation of options
while ( _options.length > 0 ) {
const cur = _options.pop()!;
switch ( cur.type ) {
case ApplicationCommandOptionType.Subcommand : {
for ( const option of cur.options ?? [] ) {
_options.push(option);
}
} break;
case ApplicationCommandOptionType.SubcommandGroup : {
for (const command of cur.options ?? []) {
_options.push(command);
}
} break;
default : {
if(cur.autocomplete) {
const choice = iAutocomplete.options.getFocused(true);
if(cur.name === choice.name && cur.autocomplete) {
return cur;
}
return undefined;
}
} break;
}
}
}