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",
"packageManager": "yarn@3.5.1",
"version": "2.6.2",
"version": "2.6.3",
"description": "A complete, customizable, typesafe, & reactive framework for discord bots.",
"main": "dist/cjs/index.cjs",
"module": "dist/esm/index.mjs",

View File

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