From 562d64714c5c3423cc01f7b64d84fca2f19f00dc Mon Sep 17 00:00:00 2001 From: Tim Smart Date: Fri, 9 Dec 2022 11:05:29 +1300 Subject: [PATCH] fix: sub command Resolvables --- src/Interactions/definitions.ts | 97 ++++++++++++++++----------------- 1 file changed, 48 insertions(+), 49 deletions(-) diff --git a/src/Interactions/definitions.ts b/src/Interactions/definitions.ts index ac5ef47..35820ed 100644 --- a/src/Interactions/definitions.ts +++ b/src/Interactions/definitions.ts @@ -137,7 +137,7 @@ type CommandHandler = export interface CommandHelper { resolve: ( - name: Resolvables["name"], + name: AllResolvables["name"], f: (id: Discord.Snowflake, data: Discord.ResolvedDatum) => T | undefined, ) => Effect @@ -188,30 +188,19 @@ type CommandHandlerFn = ( i: CommandHelper, ) => Effect -type StringLiteral = T extends string - ? string extends T - ? never - : T - : never - -type Option = A extends { name: infer N } - ? N extends StringLiteral - ? A - : never - : never - -type Options = A extends T - ? T +// == Sub commands +type SubCommands = A extends { + type: Discord.ApplicationCommandOptionType.SUB_COMMAND + options?: Discord.ApplicationCommandOption[] +} + ? A : A extends { options: Discord.ApplicationCommandOption[] } - ? Options + ? SubCommands : never -type OptionsWithLiteral = A extends { - options: Discord.ApplicationCommandOption[] -} - ? Extract & T> - : never +type SubCommandNames = Option>["name"] +// == Command options type CommandOptionType = Exclude< Discord.ApplicationCommandOptionType, | Discord.ApplicationCommandOptionType.SUB_COMMAND @@ -221,41 +210,28 @@ type CommandOptionType = Exclude< type CommandOptions = OptionsWithLiteral< A, { - name: string type: CommandOptionType } > -type RequiredCommandOptions = OptionsWithLiteral< - A, +type SubCommandOptions = Extract< + Option["options"], undefined>[number]>, { type: CommandOptionType - name: string - required: true } > -type SubCommands = A extends { - name: string - type: Discord.ApplicationCommandOptionType.SUB_COMMAND - options?: Discord.ApplicationCommandOption[] -} - ? A - : A extends { options: Discord.ApplicationCommandOption[] } - ? SubCommands - : never - -type SubCommandNames = Option>["name"] +type AllCommandOptions = CommandOptions | SubCommandOptions -type SubCommandOptions = Extract< - Option["options"], undefined>[number]>, +// == Required options +type RequiredCommandOptions = OptionsWithLiteral< + A, { type: CommandOptionType + required: true } > -type AllCommandOptions = CommandOptions | SubCommandOptions - type RequiredSubCommandOptions = Extract< SubCommandOptions, { required: true } @@ -265,14 +241,37 @@ type AllRequiredCommandOptions = | RequiredCommandOptions | RequiredSubCommandOptions -type Resolvables = Options< - A, +// == Resolveables +type ResolvableType = + | Discord.ApplicationCommandOptionType.ROLE + | Discord.ApplicationCommandOptionType.USER + | Discord.ApplicationCommandOptionType.MENTIONABLE + | Discord.ApplicationCommandOptionType.CHANNEL + +type Resolvables = OptionsWithLiteral +type SubCommandResolvables = Extract< + Option["options"], undefined>[number]>, { - name: string - type: - | Discord.ApplicationCommandOptionType.ROLE - | Discord.ApplicationCommandOptionType.USER - | Discord.ApplicationCommandOptionType.MENTIONABLE - | Discord.ApplicationCommandOptionType.CHANNEL + type: ResolvableType } > +type AllResolvables = Resolvables | SubCommandResolvables + +// == Utilities +type StringLiteral = T extends string + ? string extends T + ? never + : T + : never + +type Option = A extends { name: infer N } + ? N extends StringLiteral + ? A + : never + : never + +type OptionsWithLiteral = A extends { + options: Discord.ApplicationCommandOption[] +} + ? Extract & T> + : never