@@ -19,6 +19,7 @@ type CommandOption = Record<string, unknown> & {
1919 autocomplete ?: boolean | ( ( interaction : AutocompleteInteraction ) => Promise < void > ) ;
2020} ;
2121export type CommandOptions = CommandOption [ ] ;
22+ export type DiscordCommand = Command | CommandWithSubcommands ;
2223
2324type RawSubcommandOption = {
2425 name ?: unknown ;
@@ -72,28 +73,21 @@ function findCommandOption(
7273 return options ?. find ( ( option ) => option . name === name ) ;
7374}
7475
75- function hasCommandOptions (
76- command : BaseCommand ,
77- ) : command is BaseCommand & { options ?: CommandOptions } {
78- return "options" in command ;
79- }
80-
8176export function resolveFocusedCommandOptionAutocompleteHandler (
82- command : BaseCommand ,
77+ command : DiscordCommand ,
8378 interaction : AutocompleteInteraction ,
8479) : ( ( interaction : AutocompleteInteraction ) => Promise < void > ) | undefined {
8580 const focusedName = interaction . options . getFocused ( ) ?. name ;
8681 const options =
87- "subcommands" in command && Array . isArray ( command . subcommands )
82+ command . commandKind === "group"
8883 ? findSelectedSubcommand ( command . subcommands , interaction ) ?. options
89- : hasCommandOptions ( command )
90- ? command . options
91- : undefined ;
84+ : command . options ;
9285 const autocomplete = findCommandOption ( options , focusedName ) ?. autocomplete ;
9386 return typeof autocomplete === "function" ? autocomplete : undefined ;
9487}
9588
9689export abstract class BaseCommand {
90+ abstract readonly commandKind : "leaf" | "group" ;
9791 id ?: string ;
9892 abstract name : string ;
9993 description ?: string ;
@@ -133,6 +127,7 @@ export abstract class BaseCommand {
133127}
134128
135129export abstract class Command extends BaseCommand {
130+ readonly commandKind = "leaf" ;
136131 options ?: CommandOptions ;
137132 type = ApplicationCommandType . ChatInput ;
138133 abstract run ( interaction : unknown ) : unknown ;
@@ -153,6 +148,7 @@ export abstract class Command extends BaseCommand {
153148}
154149
155150export abstract class CommandWithSubcommands extends BaseCommand {
151+ readonly commandKind = "group" ;
156152 type = ApplicationCommandType . ChatInput ;
157153 abstract subcommands : Command [ ] ;
158154 async run ( interaction : CommandInteraction ) : Promise < unknown > {
0 commit comments