mirror of
https://github.com/sern-handler/handler
synced 2026-06-28 02:32:15 +00:00
fix: autocomplete in nested form (#97)
Co-authored-by: Evo <85353424+EvolutionX-10@users.noreply.github.com>
This commit is contained in:
@@ -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),
|
||||
|
||||
31
src/handler/utilities/treeSearch.ts
Normal file
31
src/handler/utilities/treeSearch.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user