Skip to content

Commit

Permalink
fix: sub command Resolvables
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Smart committed Dec 8, 2022
1 parent 763704b commit 562d647
Showing 1 changed file with 48 additions and 49 deletions.
97 changes: 48 additions & 49 deletions src/Interactions/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ type CommandHandler<R, E, A = any> =

export interface CommandHelper<A> {
resolve: <T>(
name: Resolvables<A>["name"],
name: AllResolvables<A>["name"],
f: (id: Discord.Snowflake, data: Discord.ResolvedDatum) => T | undefined,
) => Effect<Discord.Interaction, ResolvedDataNotFound, T>

Expand Down Expand Up @@ -188,30 +188,19 @@ type CommandHandlerFn<R, E, A> = (
i: CommandHelper<A>,
) => Effect<R, E, Discord.InteractionResponse>

type StringLiteral<T> = T extends string
? string extends T
? never
: T
: never

type Option<A> = A extends { name: infer N }
? N extends StringLiteral<N>
? A
: never
: never

type Options<A, T> = A extends T
? T
// == Sub commands
type SubCommands<A> = A extends {
type: Discord.ApplicationCommandOptionType.SUB_COMMAND
options?: Discord.ApplicationCommandOption[]
}
? A
: A extends { options: Discord.ApplicationCommandOption[] }
? Options<A["options"][number], T>
? SubCommands<A["options"][number]>
: never

type OptionsWithLiteral<A, T> = A extends {
options: Discord.ApplicationCommandOption[]
}
? Extract<A["options"][number], Option<A["options"][number]> & T>
: never
type SubCommandNames<A> = Option<SubCommands<A>>["name"]

// == Command options
type CommandOptionType = Exclude<
Discord.ApplicationCommandOptionType,
| Discord.ApplicationCommandOptionType.SUB_COMMAND
Expand All @@ -221,41 +210,28 @@ type CommandOptionType = Exclude<
type CommandOptions<A> = OptionsWithLiteral<
A,
{
name: string
type: CommandOptionType
}
>

type RequiredCommandOptions<A> = OptionsWithLiteral<
A,
type SubCommandOptions<A> = Extract<
Option<Exclude<SubCommands<A>["options"], undefined>[number]>,
{
type: CommandOptionType
name: string
required: true
}
>

type SubCommands<A> = A extends {
name: string
type: Discord.ApplicationCommandOptionType.SUB_COMMAND
options?: Discord.ApplicationCommandOption[]
}
? A
: A extends { options: Discord.ApplicationCommandOption[] }
? SubCommands<A["options"][number]>
: never

type SubCommandNames<A> = Option<SubCommands<A>>["name"]
type AllCommandOptions<A> = CommandOptions<A> | SubCommandOptions<A>

type SubCommandOptions<A> = Extract<
Option<Exclude<SubCommands<A>["options"], undefined>[number]>,
// == Required options
type RequiredCommandOptions<A> = OptionsWithLiteral<
A,
{
type: CommandOptionType
required: true
}
>

type AllCommandOptions<A> = CommandOptions<A> | SubCommandOptions<A>

type RequiredSubCommandOptions<A> = Extract<
SubCommandOptions<A>,
{ required: true }
Expand All @@ -265,14 +241,37 @@ type AllRequiredCommandOptions<A> =
| RequiredCommandOptions<A>
| RequiredSubCommandOptions<A>

type Resolvables<A> = Options<
A,
// == Resolveables
type ResolvableType =
| Discord.ApplicationCommandOptionType.ROLE
| Discord.ApplicationCommandOptionType.USER
| Discord.ApplicationCommandOptionType.MENTIONABLE
| Discord.ApplicationCommandOptionType.CHANNEL

type Resolvables<A> = OptionsWithLiteral<A, { type: ResolvableType }>
type SubCommandResolvables<A> = Extract<
Option<Exclude<SubCommands<A>["options"], undefined>[number]>,
{
name: string
type:
| Discord.ApplicationCommandOptionType.ROLE
| Discord.ApplicationCommandOptionType.USER
| Discord.ApplicationCommandOptionType.MENTIONABLE
| Discord.ApplicationCommandOptionType.CHANNEL
type: ResolvableType
}
>
type AllResolvables<A> = Resolvables<A> | SubCommandResolvables<A>

// == Utilities
type StringLiteral<T> = T extends string
? string extends T
? never
: T
: never

type Option<A> = A extends { name: infer N }
? N extends StringLiteral<N>
? A
: never
: never

type OptionsWithLiteral<A, T> = A extends {
options: Discord.ApplicationCommandOption[]
}
? Extract<A["options"][number], Option<A["options"][number]> & T>
: never

0 comments on commit 562d647

Please sign in to comment.