@lilybird/handlers
Version:
Command handlers and more for lilybird
84 lines • 4.54 kB
TypeScript
import { ApplicationCommandOptionType } from "lilybird";
import type { Client, ApplicationCommand, Awaitable, Interaction } from "lilybird";
import type { HandlerListener } from "./shared.js";
type Expand<T> = T extends T ? {
[K in keyof T]: T[K];
} : never;
type U2I<U> = (U extends U ? (u: U) => 0 : never) extends (i: infer I) => 0 ? Extract<I, U> : never;
type ParseOption<O extends BaseCommandOption> = O["required"] extends true ? {
-readonly [K in keyof O as K extends "name" ? O[K] : never]: MapOptionType<O["type"]>;
} : {
-readonly [K in keyof O as K extends "name" ? O[K] : never]: MapOptionType<O["type"]> | undefined;
};
type ParseOptions<O extends Array<CommandOption>> = U2I<{
[K in keyof O]: O[K] extends BaseCommandOption ? ParseOption<O[K]> : never;
}[number]>;
type MapOptionType<T extends ApplicationCommandOptionType> = T extends ApplicationCommandOptionType.INTEGER | ApplicationCommandOptionType.NUMBER ? number : T extends ApplicationCommandOptionType.BOOLEAN ? boolean : string;
export type ApplicationCommandHandler<O extends Array<CommandOption>, HO extends (...args: any) => any> = HO extends never ? (client: Client, interaction: Interaction.ApplicationCommandInteractionStructure, options: Expand<ParseOptions<O>>) => Awaitable<unknown> : HO;
export type ApplicationCommandAutocompleteHandler<O extends Array<CommandOption>, AO extends (...args: any) => any> = AO extends never ? (client: Client, interaction: Interaction.ApplicationCommandInteractionStructure, options: Expand<ParseOptions<O>>) => Awaitable<unknown> : AO;
interface CompiledCommand {
body: {
command: string;
autocomplete: string | null;
};
function: {
names: Array<string>;
handlers: Array<ApplicationCommandHandler<Array<CommandOption>, any> | ApplicationCommandAutocompleteHandler<Array<CommandOption>, any>>;
};
json: ApplicationCommand.Create.ApplicationCommandJSONParams & {
__meta?: {
ids?: string;
};
};
}
export interface CommandStructure<O extends Array<CommandOption>, HO extends (...args: any) => any, AO extends (...args: any) => any> extends Omit<ApplicationCommand.Create.ApplicationCommandJSONParams, "options"> {
options?: O;
meta?: CommandMeta;
handle?: ApplicationCommandHandler<O, HO>;
autocomplete?: ApplicationCommandAutocompleteHandler<O, AO>;
}
export type CommandOption = BaseCommandOption | SubCommandStructure<Array<BaseCommandOption>, never, never> | SubCommandGroupOption;
export type BaseCommandOption = Exclude<ApplicationCommand.Option.Structure, ApplicationCommand.Option.SubCommandStructure>;
export interface SubCommandStructure<O extends Array<BaseCommandOption>, HO extends (...args: any) => any, AO extends (...args: any) => any> extends ApplicationCommand.Option.Base {
type: ApplicationCommandOptionType.SUB_COMMAND;
options?: O;
handle: ApplicationCommandHandler<O, HO>;
autocomplete?: ApplicationCommandAutocompleteHandler<O, AO>;
}
interface SubCommandGroupOption extends ApplicationCommand.Option.Base {
type: ApplicationCommandOptionType.SUB_COMMAND_GROUP;
options: Array<SubCommandStructure<Array<BaseCommandOption>, never, never>>;
}
interface CommandMeta {
guild_command?: boolean;
ids?: Array<string>;
}
export type ApplicationCommandStoreCustomKeys = {
sub_command: string;
sub_command_group: string;
name: string;
};
export type ApplicationCommandStoreOptions = {
transformed: false;
customKeys?: undefined;
} | {
transformed: true;
customKeys: ApplicationCommandStoreCustomKeys;
};
export declare class ApplicationCommandStore<HO extends (...args: any) => any, AO extends (...args: any) => any> {
#private;
constructor(handlerListener?: HandlerListener, options?: ApplicationCommandStoreOptions);
storeCommand<const O extends Array<CommandOption>>(command: CommandStructure<O, HO, AO>): void;
getCompilationStack(): {
functionNames: IterableIterator<string>;
handlers: IterableIterator<(...args: any) => any>;
stack: string;
} | null;
compile(): ((client: Client, interaction: Interaction.Structure) => Awaitable<unknown>) | null;
getStoredGlobalCommands(): Array<CompiledCommand>;
getStoredGuildCommands(): Array<CompiledCommand>;
clear(): void;
}
export declare function innerOptionParser(options: ApplicationCommand.DataStructure["options"], _obj: Record<string, unknown>): void;
export {};
//# sourceMappingURL=application-command-store.d.ts.map