From 65155d11d7a701587362e06a1ca37820e1a0bfd9 Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Sun, 30 Jan 2022 21:50:09 -0800 Subject: [PATCH 1/4] Type Work --- .../src/commands/slash-command.ts | 45 +++++++-------- .../interaction-kit/src/components/choices.ts | 5 ++ .../interaction-kit/src/components/inputs.ts | 55 +++++++++++-------- .../src/definitions/application-commands.ts | 2 +- .../slash-command-interaction.ts | 43 ++++++++------- .../autcomplete/autocomplete-interaction.ts | 17 +----- packages/interaction-kit/src/interfaces.ts | 2 - 7 files changed, 80 insertions(+), 89 deletions(-) diff --git a/packages/interaction-kit/src/commands/slash-command.ts b/packages/interaction-kit/src/commands/slash-command.ts index b7d4f9d9..fa1a3996 100644 --- a/packages/interaction-kit/src/commands/slash-command.ts +++ b/packages/interaction-kit/src/commands/slash-command.ts @@ -1,32 +1,34 @@ import { ApplicationCommand, ApplicationCommandType } from "../definitions"; import Application from "../application"; -import { Input } from "../components/inputs"; +import { Input, InputKey } from "../components/inputs"; import { Optional, InteractionKitCommand } from "../interfaces"; import SlashCommandInteraction from "../interactions/application-commands/slash-command-interaction"; import SlashCommandAutocompleteInteraction from "../interactions/autcomplete/application-command-autocomplete"; // TODO: options OR autocomplete -type CommandArgs = { +type CommandArgs = { name: string; description: string; defaultPermission?: boolean; - options?: Input[]; + options?: T; onAutocomplete?: ( interaction: SlashCommandAutocompleteInteraction, application: Application ) => void; - handler: (interaction: SlashCommandInteraction) => void; + handler: (interaction: SlashCommandInteraction) => void; }; -export default class SlashCommand - implements InteractionKitCommand +export default class SlashCommand< + V extends InputKey, + T extends readonly [V, ...V[]] | [] +> implements InteractionKitCommand> { public readonly type = ApplicationCommandType.CHAT_INPUT; name: string; #description: string; #defaultPermission: boolean; - #options: Map; + private readonly options: T; onAutocomplete?: ( interaction: SlashCommandAutocompleteInteraction, @@ -34,7 +36,7 @@ export default class SlashCommand ) => void; handler: ( - interaction: SlashCommandInteraction, + interaction: SlashCommandInteraction, application: Application ) => void; @@ -45,25 +47,15 @@ export default class SlashCommand onAutocomplete, handler, defaultPermission = true, - }: CommandArgs) { + }: CommandArgs) { // TODO: Validate: 1-32 lowercase character name matching ^[\w-]{1,32}$ this.name = name; this.#description = description; this.#defaultPermission = defaultPermission; this.handler = handler; this.onAutocomplete = onAutocomplete; - this.#options = new Map(); - - options?.forEach((option) => { - const key = option.name.toLowerCase(); - if (this.#options.has(key)) { - throw new Error( - `Option names must be unique (case insensitive). Duplicate name detected: ${key}` - ); - } - - this.#options.set(key, option); - }); + + this.options = options ?? ([] as T); } group() { @@ -83,13 +75,16 @@ export default class SlashCommand return false; } - if (this.#options.size !== (schema.options?.length ?? 0)) { + if (this.options.length !== (schema.options?.length ?? 0)) { return false; } return ( schema.options?.every( - (option) => this.#options.get(option.name)?.equals(option) ?? false + (option) => + this.options + .find((opt) => opt.name === option.name) + ?.equals(option) ?? false ) ?? true ); } @@ -105,10 +100,10 @@ export default class SlashCommand } // TODO: Sort these so that required options come first - if (this.#options.size > 0) { + if (this.options.length > 0) { payload.options = []; - Array.from(this.#options.entries()).forEach(([_, value]) => { + this.options.forEach((value) => { payload.options?.push(value.serialize()); }); } diff --git a/packages/interaction-kit/src/components/choices.ts b/packages/interaction-kit/src/components/choices.ts index 6b328595..8015cb54 100644 --- a/packages/interaction-kit/src/components/choices.ts +++ b/packages/interaction-kit/src/components/choices.ts @@ -84,3 +84,8 @@ export class SlashChoiceList< }); } } + +/** + * Choices.createSelectOptionList() + * Choices.createSlashChoiceList() + */ diff --git a/packages/interaction-kit/src/components/inputs.ts b/packages/interaction-kit/src/components/inputs.ts index aeaa930d..9cc1746d 100644 --- a/packages/interaction-kit/src/components/inputs.ts +++ b/packages/interaction-kit/src/components/inputs.ts @@ -8,20 +8,24 @@ import { SlashChoiceList } from "./choices"; type InputChoiceValue = ApplicationCommandOptionChoice["value"]; -type InputArgs = { +export interface InputKey { + readonly name: string; +} + +type InputArgs = { type: ApplicationCommandOptionType; - name: string; + name: T; description: string; required?: boolean; choices?: SlashChoiceList; options?: ApplicationCommandOption[]; }; -export class Input +export class Input implements Serializable, Comparable { public readonly type; - public readonly name; + public readonly name: Name; public readonly description; public readonly required; public readonly options; @@ -34,7 +38,7 @@ export class Input choices, options, required = false, - }: InputArgs) { + }: InputArgs) { this.type = type; this.name = name; this.description = description; @@ -101,62 +105,65 @@ export class Input } } -interface StringInputArgs extends Omit { +interface StringInputArgs + extends Omit, "type" | "options"> { choices?: SlashChoiceList; } -export class StringInput extends Input { - constructor(args: StringInputArgs) { +export class StringInput extends Input { + constructor(args: StringInputArgs) { super({ type: ApplicationCommandOptionType.STRING, ...args }); } } -interface IntegerInputArgs extends Omit { +interface IntegerInputArgs + extends Omit, "type" | "options"> { choices?: SlashChoiceList; } -export class IntegerInput extends Input { - constructor(args: IntegerInputArgs) { +export class IntegerInput extends Input { + constructor(args: IntegerInputArgs) { super({ type: ApplicationCommandOptionType.INTEGER, ...args }); } } -interface NumberInputArgs extends Omit { +interface NumberInputArgs + extends Omit, "type" | "options"> { choices?: SlashChoiceList; } -export class NumberInput extends Input { - constructor(args: NumberInputArgs) { +export class NumberInput extends Input { + constructor(args: NumberInputArgs) { super({ type: ApplicationCommandOptionType.NUMBER, ...args }); } } -export class BooleanInput extends Input { - constructor(args: Omit) { +export class BooleanInput extends Input { + constructor(args: Omit, "type" | "choices" | "options">) { super({ type: ApplicationCommandOptionType.BOOLEAN, ...args }); } } -export class UserInput extends Input { - constructor(args: Omit) { +export class UserInput extends Input { + constructor(args: Omit, "type" | "choices" | "options">) { super({ type: ApplicationCommandOptionType.USER, ...args }); } } -export class ChannelInput extends Input { - constructor(args: Omit) { +export class ChannelInput extends Input { + constructor(args: Omit, "type" | "choices" | "options">) { super({ type: ApplicationCommandOptionType.CHANNEL, ...args }); } } -export class RoleInput extends Input { - constructor(args: Omit) { +export class RoleInput extends Input { + constructor(args: Omit, "type" | "choices" | "options">) { super({ type: ApplicationCommandOptionType.ROLE, ...args }); } } -export class MentionableInput extends Input { - constructor(args: Omit) { +export class MentionableInput extends Input { + constructor(args: Omit, "type" | "choices" | "options">) { super({ type: ApplicationCommandOptionType.MENTIONABLE, ...args }); } } diff --git a/packages/interaction-kit/src/definitions/application-commands.ts b/packages/interaction-kit/src/definitions/application-commands.ts index 0360c884..de2440c5 100644 --- a/packages/interaction-kit/src/definitions/application-commands.ts +++ b/packages/interaction-kit/src/definitions/application-commands.ts @@ -114,7 +114,7 @@ export type ApplicationCommandInteractionData = { custom_id?: string; component_type?: ComponentType; target_id?: Snowflake; - values?: Array; + value?: Array; }; /** @link https://discord.com/developers/docs/interactions/slash-commands#interaction-object-application-command-interaction-data-resolved-structure */ diff --git a/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts b/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts index e701f8d9..825d5601 100644 --- a/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts +++ b/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts @@ -1,43 +1,44 @@ import type { FastifyReply, FastifyRequest } from "fastify"; +import { InputKey, SlashChoiceList } from "../.."; import Application from "../../application"; import { + ApplicationCommandInteractionData, ApplicationCommandInteractionDataOption, ApplicationCommandType, Interaction as InteractionDefinition, OptionType, } from "../../definitions"; -import { InteractionKitCommand } from "../../interfaces"; +import { ArrayValue, InteractionKitCommand } from "../../interfaces"; import ApplicationCommandInteraction from "./application-command-interaction"; -export default class SlashCommandInteraction extends ApplicationCommandInteraction { - readonly #options: Map; +type InteractionOptions = { + [Key in T[number]["name"]]: Extract; +}; + +type SlashCommandInteractionBody< + T extends readonly [InputKey, ...InputKey[]] | [] +> = Omit & { + data: Omit & { + options: InteractionOptions; + }; +}; + +export default class SlashCommandInteraction< + T extends readonly [InputKey, ...InputKey[]] | [] +> extends ApplicationCommandInteraction { + public readonly options: InteractionOptions; constructor( application: Application, - command: InteractionKitCommand, - request: FastifyRequest<{ Body: InteractionDefinition }>, + command: InteractionKitCommand>, + request: FastifyRequest<{ Body: SlashCommandInteractionBody }>, response: FastifyReply ) { super(application, request, response); - this.#options = new Map(); - - request.body?.data?.options?.forEach((option) => { - this.#options.set(option.name.toLowerCase(), option); - }); + this.options = request.body?.data?.options; } get commandType() { return ApplicationCommandType.CHAT_INPUT; } - - // TODO: Type? Should return an object where keys = #options keys, and value = ApplicationCommandInteractionDataOption - get options() { - return new Proxy( - {}, - { - get: (target, property): OptionType | null => - this.#options.get(property.toString())?.value ?? null, - } - ); - } } diff --git a/packages/interaction-kit/src/interactions/autcomplete/autocomplete-interaction.ts b/packages/interaction-kit/src/interactions/autcomplete/autocomplete-interaction.ts index d5621ab9..997d11d3 100644 --- a/packages/interaction-kit/src/interactions/autcomplete/autocomplete-interaction.ts +++ b/packages/interaction-kit/src/interactions/autcomplete/autocomplete-interaction.ts @@ -1,25 +1,10 @@ import type { FastifyReply, FastifyRequest } from "fastify"; import { - ApplicationCommandInteractionDataOption, Interaction as InteractionDefinition, - InteractionApplicationCommandCallbackData, - InteractionCallbackType, - InteractionResponse, - InteractionRequestType, Snowflake, } from "../../definitions"; -import { PermissionFlags } from "../../definitions/messages"; -import Embed from "../../components/embed"; -import * as API from "../../api"; import Application from "../../application"; -import { - Autocomplete, - Interaction, - InteractionMessageModifiers, - InteractionReply, - SerializableComponent, -} from "../../interfaces"; -import { isActionRow } from "../../components/action-row"; +import { Autocomplete } from "../../interfaces"; import { AutocompleteInteractionTypes, AutocompleteTypes } from "./types"; export default class AutocompleteInteraction diff --git a/packages/interaction-kit/src/interfaces.ts b/packages/interaction-kit/src/interfaces.ts index 13ca9b45..f738cf7f 100644 --- a/packages/interaction-kit/src/interfaces.ts +++ b/packages/interaction-kit/src/interfaces.ts @@ -8,11 +8,9 @@ import { Component, ComponentType, InteractionApplicationCommandCallbackData, - InteractionCallbackType, InteractionRequestType, Snowflake, } from "./definitions"; -import AutocompleteInteraction from "./interactions/autcomplete/autocomplete-interaction"; export type Optional = Omit & Partial>; From 276ff5bd42c282801ef3c6d91b3f32fe3d39599f Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Mon, 31 Jan 2022 14:37:57 -0800 Subject: [PATCH 2/4] Getting Closer --- .../src/commands/slash-command.ts | 6 ++--- .../interaction-kit/src/components/inputs.ts | 8 ++++-- .../slash-command-interaction.ts | 27 ++++++++++++------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/packages/interaction-kit/src/commands/slash-command.ts b/packages/interaction-kit/src/commands/slash-command.ts index fa1a3996..db94042a 100644 --- a/packages/interaction-kit/src/commands/slash-command.ts +++ b/packages/interaction-kit/src/commands/slash-command.ts @@ -1,6 +1,6 @@ import { ApplicationCommand, ApplicationCommandType } from "../definitions"; import Application from "../application"; -import { Input, InputKey } from "../components/inputs"; +import type { InputKey } from "../components/inputs"; import { Optional, InteractionKitCommand } from "../interfaces"; import SlashCommandInteraction from "../interactions/application-commands/slash-command-interaction"; import SlashCommandAutocompleteInteraction from "../interactions/autcomplete/application-command-autocomplete"; @@ -28,8 +28,6 @@ export default class SlashCommand< name: string; #description: string; #defaultPermission: boolean; - private readonly options: T; - onAutocomplete?: ( interaction: SlashCommandAutocompleteInteraction, application: Application @@ -40,6 +38,8 @@ export default class SlashCommand< application: Application ) => void; + private readonly options: T; + constructor({ name, description, diff --git a/packages/interaction-kit/src/components/inputs.ts b/packages/interaction-kit/src/components/inputs.ts index 9cc1746d..84169dd7 100644 --- a/packages/interaction-kit/src/components/inputs.ts +++ b/packages/interaction-kit/src/components/inputs.ts @@ -8,7 +8,9 @@ import { SlashChoiceList } from "./choices"; type InputChoiceValue = ApplicationCommandOptionChoice["value"]; -export interface InputKey { +export interface InputKey + extends Serializable, + Comparable { readonly name: string; } @@ -22,7 +24,9 @@ type InputArgs = { }; export class Input - implements Serializable, Comparable + implements + Serializable, + Comparable { public readonly type; public readonly name: Name; diff --git a/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts b/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts index 825d5601..ccc7d88e 100644 --- a/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts +++ b/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts @@ -1,27 +1,36 @@ import type { FastifyReply, FastifyRequest } from "fastify"; -import { InputKey, SlashChoiceList } from "../.."; +import { InputKey } from "../.."; import Application from "../../application"; import { - ApplicationCommandInteractionData, - ApplicationCommandInteractionDataOption, ApplicationCommandType, Interaction as InteractionDefinition, - OptionType, } from "../../definitions"; -import { ArrayValue, InteractionKitCommand } from "../../interfaces"; +import { InteractionKitCommand } from "../../interfaces"; import ApplicationCommandInteraction from "./application-command-interaction"; type InteractionOptions = { - [Key in T[number]["name"]]: Extract; + [Key in T[number]["name"]]: Extract extends { + optional: true; + } + ? Extract | undefined + : Extract; }; -type SlashCommandInteractionBody< +// type SlashCommandInteractionBody< +// T extends readonly [InputKey, ...InputKey[]] | [] +// > = Omit & { +// data: Omit & { +// options: InteractionOptions; +// }; +// }; + +interface SlashCommandInteractionBody< T extends readonly [InputKey, ...InputKey[]] | [] -> = Omit & { +> extends InteractionDefinition { data: Omit & { options: InteractionOptions; }; -}; +} export default class SlashCommandInteraction< T extends readonly [InputKey, ...InputKey[]] | [] From 0a735150801aacec1833bda79834098711d14f7a Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Mon, 31 Jan 2022 19:59:50 -0800 Subject: [PATCH 3/4] Maisy did this --- .../interaction-kit/src/components/inputs.ts | 120 ++++++++++++++---- .../slash-command-interaction.ts | 6 +- 2 files changed, 95 insertions(+), 31 deletions(-) diff --git a/packages/interaction-kit/src/components/inputs.ts b/packages/interaction-kit/src/components/inputs.ts index 84169dd7..5356b215 100644 --- a/packages/interaction-kit/src/components/inputs.ts +++ b/packages/interaction-kit/src/components/inputs.ts @@ -12,10 +12,11 @@ export interface InputKey extends Serializable, Comparable { readonly name: string; + readonly type: ApplicationCommandOptionType; } -type InputArgs = { - type: ApplicationCommandOptionType; +type InputArgs = { + type: U; name: T; description: string; required?: boolean; @@ -23,13 +24,15 @@ type InputArgs = { options?: ApplicationCommandOption[]; }; -export class Input - implements +export class Input< + Name extends string, + OptionType extends ApplicationCommandOptionType +> implements Serializable, Comparable { - public readonly type; public readonly name: Name; + public readonly type: OptionType; public readonly description; public readonly required; public readonly options; @@ -42,7 +45,7 @@ export class Input choices, options, required = false, - }: InputArgs) { + }: InputArgs) { this.type = type; this.name = name; this.description = description; @@ -109,65 +112,126 @@ export class Input } } -interface StringInputArgs - extends Omit, "type" | "options"> { +interface StringInputArgs< + Name extends string, + OptionType extends ApplicationCommandOptionType +> extends Omit, "type" | "options"> { choices?: SlashChoiceList; } -export class StringInput extends Input { - constructor(args: StringInputArgs) { +export class StringInput extends Input< + Name, + ApplicationCommandOptionType.STRING +> { + constructor( + args: StringInputArgs + ) { super({ type: ApplicationCommandOptionType.STRING, ...args }); } } -interface IntegerInputArgs - extends Omit, "type" | "options"> { +interface IntegerInputArgs< + Name extends string, + OptionType extends ApplicationCommandOptionType +> extends Omit, "type" | "options"> { choices?: SlashChoiceList; } -export class IntegerInput extends Input { - constructor(args: IntegerInputArgs) { +export class IntegerInput extends Input< + Name, + ApplicationCommandOptionType.INTEGER +> { + constructor( + args: IntegerInputArgs + ) { super({ type: ApplicationCommandOptionType.INTEGER, ...args }); } } -interface NumberInputArgs - extends Omit, "type" | "options"> { +interface NumberInputArgs< + Name extends string, + OptionType extends ApplicationCommandOptionType +> extends Omit, "type" | "options"> { choices?: SlashChoiceList; } -export class NumberInput extends Input { - constructor(args: NumberInputArgs) { +export class NumberInput extends Input< + Name, + ApplicationCommandOptionType.NUMBER +> { + constructor( + args: NumberInputArgs + ) { super({ type: ApplicationCommandOptionType.NUMBER, ...args }); } } -export class BooleanInput extends Input { - constructor(args: Omit, "type" | "choices" | "options">) { +export class BooleanInput extends Input< + Name, + ApplicationCommandOptionType.BOOLEAN +> { + constructor( + args: Omit< + InputArgs, + "type" | "choices" | "options" + > + ) { super({ type: ApplicationCommandOptionType.BOOLEAN, ...args }); } } -export class UserInput extends Input { - constructor(args: Omit, "type" | "choices" | "options">) { +export class UserInput extends Input< + Name, + ApplicationCommandOptionType.USER +> { + constructor( + args: Omit< + InputArgs, + "type" | "choices" | "options" + > + ) { super({ type: ApplicationCommandOptionType.USER, ...args }); } } -export class ChannelInput extends Input { - constructor(args: Omit, "type" | "choices" | "options">) { +export class ChannelInput extends Input< + Name, + ApplicationCommandOptionType.CHANNEL +> { + constructor( + args: Omit< + InputArgs, + "type" | "choices" | "options" + > + ) { super({ type: ApplicationCommandOptionType.CHANNEL, ...args }); } } -export class RoleInput extends Input { - constructor(args: Omit, "type" | "choices" | "options">) { +export class RoleInput extends Input< + Name, + ApplicationCommandOptionType.ROLE +> { + constructor( + args: Omit< + InputArgs, + "type" | "choices" | "options" + > + ) { super({ type: ApplicationCommandOptionType.ROLE, ...args }); } } -export class MentionableInput extends Input { - constructor(args: Omit, "type" | "choices" | "options">) { +export class MentionableInput extends Input< + Name, + ApplicationCommandOptionType.MENTIONABLE +> { + constructor( + args: Omit< + InputArgs, + "type" | "choices" | "options" + > + ) { super({ type: ApplicationCommandOptionType.MENTIONABLE, ...args }); } } diff --git a/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts b/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts index ccc7d88e..8da5f9e0 100644 --- a/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts +++ b/packages/interaction-kit/src/interactions/application-commands/slash-command-interaction.ts @@ -10,10 +10,10 @@ import ApplicationCommandInteraction from "./application-command-interaction"; type InteractionOptions = { [Key in T[number]["name"]]: Extract extends { - optional: true; + required: true; } - ? Extract | undefined - : Extract; + ? Extract["type"] + : Extract["type"] | undefined; }; // type SlashCommandInteractionBody< From 5cc219a65f8fd85a82b6e891a05dba760a9f2d6b Mon Sep 17 00:00:00 2001 From: Ian Mitchell Date: Wed, 23 Feb 2022 14:46:50 -0800 Subject: [PATCH 4/4] I don't remember what this was --- packages/interaction-kit/src/application.ts | 13 ++++++------- .../src/commands/slash-command.ts | 6 +++--- .../application-command-autocomplete.ts | 13 +++++++++---- .../autocomplete-interaction.ts | 9 ++++++--- .../{autcomplete => autocomplete}/types.ts | 11 ++++++----- .../interaction-kit/src/interactions/index.ts | 17 +++++++++-------- packages/interaction-kit/src/scripts.ts | 3 --- 7 files changed, 39 insertions(+), 33 deletions(-) rename packages/interaction-kit/src/interactions/{autcomplete => autocomplete}/application-command-autocomplete.ts (68%) rename packages/interaction-kit/src/interactions/{autcomplete => autocomplete}/autocomplete-interaction.ts (87%) rename packages/interaction-kit/src/interactions/{autcomplete => autocomplete}/types.ts (58%) diff --git a/packages/interaction-kit/src/application.ts b/packages/interaction-kit/src/application.ts index b5cf7467..c43a8343 100644 --- a/packages/interaction-kit/src/application.ts +++ b/packages/interaction-kit/src/application.ts @@ -1,20 +1,19 @@ -import type { FastifyRequest, FastifyReply } from "fastify"; - +import type { FastifyReply, FastifyRequest } from "fastify"; import fs from "node:fs"; import path from "node:path"; -import SlashCommand from "./commands/slash-command"; -import ContextMenu from "./commands/context-menu"; import Config from "./api/config"; +import ContextMenu from "./commands/context-menu"; +import SlashCommand from "./commands/slash-command"; +import { ExecutableComponent, isExecutableComponent } from "./components"; import { + ApplicationCommandType, Interaction as InteractionDefinition, Snowflake, - ApplicationCommandType, } from "./definitions"; import * as Interaction from "./interactions"; +import ApplicationCommandInteraction from "./interactions/application-commands/application-command-interaction"; import { InteractionKitCommand, SerializableComponent } from "./interfaces"; import startInteractionKitServer from "./server"; -import ApplicationCommandInteraction from "./interactions/application-commands/application-command-interaction"; -import { ExecutableComponent, isExecutableComponent } from "./components"; type ApplicationArgs = { applicationID: string; diff --git a/packages/interaction-kit/src/commands/slash-command.ts b/packages/interaction-kit/src/commands/slash-command.ts index db94042a..5c9b9b2b 100644 --- a/packages/interaction-kit/src/commands/slash-command.ts +++ b/packages/interaction-kit/src/commands/slash-command.ts @@ -1,9 +1,9 @@ -import { ApplicationCommand, ApplicationCommandType } from "../definitions"; import Application from "../application"; import type { InputKey } from "../components/inputs"; -import { Optional, InteractionKitCommand } from "../interfaces"; +import { ApplicationCommand, ApplicationCommandType } from "../definitions"; import SlashCommandInteraction from "../interactions/application-commands/slash-command-interaction"; -import SlashCommandAutocompleteInteraction from "../interactions/autcomplete/application-command-autocomplete"; +import SlashCommandAutocompleteInteraction from "../interactions/autocomplete/application-command-autocomplete"; +import { InteractionKitCommand, Optional } from "../interfaces"; // TODO: options OR autocomplete type CommandArgs = { diff --git a/packages/interaction-kit/src/interactions/autcomplete/application-command-autocomplete.ts b/packages/interaction-kit/src/interactions/autocomplete/application-command-autocomplete.ts similarity index 68% rename from packages/interaction-kit/src/interactions/autcomplete/application-command-autocomplete.ts rename to packages/interaction-kit/src/interactions/autocomplete/application-command-autocomplete.ts index 1eb60b0a..32f0f73d 100644 --- a/packages/interaction-kit/src/interactions/autcomplete/application-command-autocomplete.ts +++ b/packages/interaction-kit/src/interactions/autocomplete/application-command-autocomplete.ts @@ -1,6 +1,7 @@ import type { FastifyReply, FastifyRequest } from "fastify"; -import SlashCommand from "../../commands/slash-command"; import Application from "../../application"; +import SlashCommand from "../../commands/slash-command"; +import { InputKey } from "../../components/inputs"; import { Interaction as InteractionDefinition, InteractionCallbackType, @@ -8,11 +9,15 @@ import { import AutocompleteInteraction from "./autocomplete-interaction"; import { SlashCommandAutocompleteType } from "./types"; -export default class SlashCommandAutocompleteInteraction extends AutocompleteInteraction { - public readonly command: SlashCommand; +export default class SlashCommandAutocompleteInteraction< + V extends InputKey, + T extends readonly [V, ...V[]] | [] +> extends AutocompleteInteraction { + public readonly command: SlashCommand; + constructor( application: Application, - command: SlashCommand, + command: SlashCommand, request: FastifyRequest<{ Body: InteractionDefinition }>, response: FastifyReply ) { diff --git a/packages/interaction-kit/src/interactions/autcomplete/autocomplete-interaction.ts b/packages/interaction-kit/src/interactions/autocomplete/autocomplete-interaction.ts similarity index 87% rename from packages/interaction-kit/src/interactions/autcomplete/autocomplete-interaction.ts rename to packages/interaction-kit/src/interactions/autocomplete/autocomplete-interaction.ts index 997d11d3..876d8538 100644 --- a/packages/interaction-kit/src/interactions/autcomplete/autocomplete-interaction.ts +++ b/packages/interaction-kit/src/interactions/autocomplete/autocomplete-interaction.ts @@ -1,14 +1,17 @@ import type { FastifyReply, FastifyRequest } from "fastify"; +import Application from "../../application"; +import { InputKey } from "../../components/inputs"; import { Interaction as InteractionDefinition, Snowflake, } from "../../definitions"; -import Application from "../../application"; import { Autocomplete } from "../../interfaces"; import { AutocompleteInteractionTypes, AutocompleteTypes } from "./types"; -export default class AutocompleteInteraction - implements Autocomplete +export default class AutocompleteInteraction< + U extends readonly [InputKey, ...InputKey[]] | [], + T extends AutocompleteTypes +> implements Autocomplete { public readonly name: string; public readonly token: string; diff --git a/packages/interaction-kit/src/interactions/autcomplete/types.ts b/packages/interaction-kit/src/interactions/autocomplete/types.ts similarity index 58% rename from packages/interaction-kit/src/interactions/autcomplete/types.ts rename to packages/interaction-kit/src/interactions/autocomplete/types.ts index 3ff6aaf9..24300bde 100644 --- a/packages/interaction-kit/src/interactions/autcomplete/types.ts +++ b/packages/interaction-kit/src/interactions/autocomplete/types.ts @@ -8,9 +8,10 @@ import { InteractionCallbackType } from "../../definitions/application-commands" export type AutocompleteInteractionTypes = InteractionCallbackType.APPLICATION_COMMAND_AUTOCOMPLETE_RESULT; -export type AutocompleteTypes = SlashCommandAutocompleteType; +export type AutocompleteTypes = + SlashCommandAutocompleteType; -export type SlashCommandAutocompleteType = - | StringInput - | IntegerInput - | NumberInput; +export type SlashCommandAutocompleteType = + | StringInput + | IntegerInput + | NumberInput; diff --git a/packages/interaction-kit/src/interactions/index.ts b/packages/interaction-kit/src/interactions/index.ts index afcd5aa4..e4bb191e 100644 --- a/packages/interaction-kit/src/interactions/index.ts +++ b/packages/interaction-kit/src/interactions/index.ts @@ -1,20 +1,21 @@ +import type { FastifyReply, FastifyRequest } from "fastify"; +import { ApplicationCommandInteraction, PingInteraction } from ".."; import Application from "../application"; -import type { FastifyRequest, FastifyReply } from "fastify"; +import { ExecutableComponent } from "../components"; +import { Button } from "../components/button"; +import Select from "../components/select"; import { ApplicationCommandType, ComponentType, Interaction as InteractionDefinition, InteractionRequestType, } from "../definitions"; +import { InteractionKitCommand } from "../interfaces"; +import ContextMenuInteraction from "./application-commands/context-menu-interaction"; +import SlashCommandInteraction from "./application-commands/slash-command-interaction"; +import SlashCommandAutocompleteInteraction from "./autocomplete/application-command-autocomplete"; import ButtonInteraction from "./message-components/button-interaction"; import SelectInteraction from "./message-components/select-interaction"; -import SlashCommandInteraction from "./application-commands/slash-command-interaction"; -import ContextMenuInteraction from "./application-commands/context-menu-interaction"; -import { ExecutableComponent } from "../components"; -import { Button } from "../components/button"; -import Select from "../components/select"; -import { InteractionKitCommand } from "../interfaces"; -import { ApplicationCommandInteraction, PingInteraction } from ".."; const autocompleteTypes = new Set([ InteractionRequestType.APPLICATION_COMMAND_AUTOCOMPLETE, diff --git a/packages/interaction-kit/src/scripts.ts b/packages/interaction-kit/src/scripts.ts index 57961c6e..2abaed98 100644 --- a/packages/interaction-kit/src/scripts.ts +++ b/packages/interaction-kit/src/scripts.ts @@ -42,10 +42,8 @@ function getChangeSet( } if (command.equals(signature)) { - // @ts-expect-error ???? changeSet.unchangedCommands.add(command.serialize()); } else { - // @ts-expect-error ???? changeSet.updatedCommands.add(command.serialize()); changeSet.hasChanges = true; } @@ -54,7 +52,6 @@ function getChangeSet( } // If the command does not exist, we add it else { - // @ts-expect-error ???? changeSet.newCommands.add(command.serialize()); changeSet.hasChanges = true; }