fix: autocomplete nested option and merge main

This commit is contained in:
Jacob Nguyen
2023-06-17 16:16:47 -05:00
parent e00d1df32e
commit 5fdc1eda7f
2 changed files with 21 additions and 14 deletions

View File

@@ -1,7 +1,7 @@
{ {
"name": "@sern/handler", "name": "@sern/handler",
"packageManager": "yarn@3.5.1", "packageManager": "yarn@3.5.1",
"version": "2.6.2", "version": "2.6.3",
"description": "A complete, customizable, typesafe, & reactive framework for discord bots.", "description": "A complete, customizable, typesafe, & reactive framework for discord bots.",
"main": "dist/cjs/index.cjs", "main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs", "module": "dist/esm/index.mjs",

View File

@@ -1,5 +1,6 @@
import { ApplicationCommandOptionType, AutocompleteInteraction } from 'discord.js'; import { ApplicationCommandOptionType, AutocompleteInteraction } from 'discord.js';
import type { SernAutocompleteData, SernOptionsData } from '../../types/module'; import type { SernAutocompleteData, SernOptionsData } from '../../types/module';
import assert from 'assert';
/** /**
* Uses an iterative DFS to check if an autocomplete node exists * Uses an iterative DFS to check if an autocomplete node exists
@@ -11,37 +12,43 @@ export default function treeSearch(
options: SernOptionsData[] | undefined, options: SernOptionsData[] | undefined,
): SernAutocompleteData | undefined { ): SernAutocompleteData | undefined {
if (options === undefined) return undefined; if (options === undefined) return undefined;
const _options = options.slice(); // required to prevent direct mutation of options //clone to prevent mutation of original command module
let autocompleteData: SernAutocompleteData | undefined; const _options = options.map(a => ({...a}));
let subcommands = new Set();
while (_options.length > 0) { while (_options.length > 0) {
const cur = _options.pop()!; const cur = _options.pop()!;
switch (cur.type) { switch (cur.type) {
case ApplicationCommandOptionType.Subcommand: case ApplicationCommandOptionType.Subcommand:
{ {
for (const option of cur.options ?? []) { subcommands.add(cur.name);
for (const option of cur.options ?? [])
_options.push(option); _options.push(option);
}
} }
break; break;
case ApplicationCommandOptionType.SubcommandGroup: case ApplicationCommandOptionType.SubcommandGroup:
{ {
for (const command of cur.options ?? []) { for (const command of cur.options ?? [])
_options.push(command); _options.push(command);
}
} }
break; break;
default: default:
{ {
if (cur.autocomplete) { if ('autocomplete' in cur && cur.autocomplete) {
const choice = iAutocomplete.options.getFocused(true); const choice = iAutocomplete.options.getFocused(true);
if (cur.name === choice.name && cur.autocomplete) { assert('command' in cur, "No command property found for autocomplete option");
autocompleteData = cur; if(subcommands.size > 0) {
const parent = iAutocomplete.options.getSubcommand();
const parentAndOptionMatches = subcommands.has(parent) && cur.name === choice.name;
if (parentAndOptionMatches) {
return cur;
}
} else {
if(cur.name === choice.name) {
return cur;
}
} }
} }
} }
break; break;
} }
} }}
return autocompleteData;
}