@constatic/base 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,5 @@
1
1
  import { ApplicationCommandOptionType, ApplicationCommandType, AutocompleteInteraction, ChatInputCommandInteraction, InteractionContextType, MessageContextMenuCommandInteraction, UserContextMenuCommandInteraction, type ApplicationCommandOptionAllowedChannelTypes, type ApplicationCommandOptionChoiceData, type BaseApplicationCommandData, type CacheType, type LocalizationMap } from "discord.js";
2
+ import type { NotEmptyArray, UniqueArray } from "../../utils/types.js";
2
3
  export type CommandType = Exclude<ApplicationCommandType, ApplicationCommandType.PrimaryEntryPoint>;
3
4
  type AutocompleData<Type> = Promise<readonly ApplicationCommandOptionChoiceData<Type extends (number | string) ? Type : (number | string)>[] | undefined | void>;
4
5
  export type AutocompleteRun<Type, Contexts> = (this: void, interaction: AutocompleteInteraction<CacheMode<Contexts>>) => AutocompleData<Type>;
@@ -66,7 +67,7 @@ type BaseAppCommandData = Omit<BaseApplicationCommandData, "contexts"> & Pick<Ba
66
67
  export interface CommandData<T, Contexts, R> extends BaseAppCommandData {
67
68
  name: string;
68
69
  description?: string;
69
- contexts?: Contexts;
70
+ contexts?: NotEmptyArray<UniqueArray<Contexts>>;
70
71
  type?: T;
71
72
  global?: boolean;
72
73
  run?(this: CommandRunThis, interaction: RunInteraction<T, Contexts>): Promise<R>;
@@ -9,7 +9,9 @@ export declare class CommandManager extends BaseManager {
9
9
  private get config();
10
10
  private readonly collection;
11
11
  readonly runners: Map<string, Runner[]>;
12
+ readonly autocompleteRunners: Map<string, Runner>;
12
13
  set<T, C extends readonly InteractionContextType[], R>(command: Command<T, C, R>): void;
14
+ getAutocompleteHandler(...path: (string | null)[]): Runner;
13
15
  getTitle(type?: CommandType): string[];
14
16
  build(): BuildedCommandData[];
15
17
  private buildOptions;
@@ -10,12 +10,13 @@ class CommandManager extends BaseManager {
10
10
  }
11
11
  collection = new Map;
12
12
  runners = new Map;
13
+ autocompleteRunners = new Map;
13
14
  set(command) {
14
15
  this.collection.set(command.data.name, command);
15
16
  const path = `/${command.data.type}/${command.data.name}`;
16
17
  this.runners.set(path, [command.data.run]);
17
18
  if (command.data.autocomplete) {
18
- this.runners.set(`${path}/autocomplete`, [command.data.autocomplete]);
19
+ this.autocompleteRunners.set(path, command.data.autocomplete);
19
20
  }
20
21
  const [icon, label] = this.getTitle(command.data.type);
21
22
  this.logs.push([
@@ -25,6 +26,12 @@ class CommandManager extends BaseManager {
25
26
  styleText("green", "✓")
26
27
  ].join(" "));
27
28
  }
29
+ getAutocompleteHandler(...path) {
30
+ const commandName = path[0];
31
+ const type = ApplicationCommandType.ChatInput;
32
+ const resolved = `/${type}/${path.filter((p) => p).join("/")}`;
33
+ return this.autocompleteRunners.get(resolved) ?? this.autocompleteRunners.get(`/${type}/${commandName}`);
34
+ }
28
35
  getTitle(type) {
29
36
  return {
30
37
  [ApplicationCommandType.ChatInput]: ["{/}", "Slash command"],
@@ -60,7 +67,7 @@ class CommandManager extends BaseManager {
60
67
  for (const option of options) {
61
68
  const description = option.description ?? option.name;
62
69
  if ("autocomplete" in option && option.autocomplete && typeof option.autocomplete === "function") {
63
- this.runners.set(`${path}/autocomplete/${option.name}`, [option.autocomplete]);
70
+ this.autocompleteRunners.set(`${path}/${option.name}`, option.autocomplete);
64
71
  }
65
72
  switch (option.type) {
66
73
  case ApplicationCommandOptionType.SubcommandGroup: {
@@ -137,10 +144,9 @@ class CommandManager extends BaseManager {
137
144
  }
138
145
  async onAutocomplete(interaction) {
139
146
  const options = interaction.options;
140
- const handler = this.getHandler(interaction.commandType, interaction.commandName, "autocomplete", options.getSubcommandGroup(false), options.getSubcommand(false), options.getFocused(true).name);
141
- if (!handler || !handler[0])
147
+ const run = this.getAutocompleteHandler(interaction.commandName, options.getSubcommandGroup(false), options.getSubcommand(false), options.getFocused(true).name);
148
+ if (!run)
142
149
  return;
143
- const [run] = handler;
144
150
  const choices = await run(interaction);
145
151
  if (choices && Array.isArray(choices)) {
146
152
  await interaction.respond(choices.slice(0, 25));
@@ -1,9 +1,6 @@
1
1
  import type { ButtonInteraction, CacheType, ChannelSelectMenuInteraction, MentionableSelectMenuInteraction, ModalMessageModalSubmitInteraction, ModalSubmitInteraction, RoleSelectMenuInteraction, StringSelectMenuInteraction, UserSelectMenuInteraction } from "discord.js";
2
2
  import { type InferRouteParams } from "rou3";
3
- import type { Prettify } from "../../utils/types.js";
4
- type NotEmptyArray<T> = T extends never[] ? never : T;
5
- type UniqueArray<T> = T extends readonly [infer X, ...infer Rest] ? InArray<Rest, X> extends true ? ["Encountered value with duplicates:", X] : readonly [X, ...UniqueArray<Rest>] : T;
6
- type InArray<T, X> = T extends readonly [X, ...infer _Rest] ? true : T extends readonly [X] ? true : T extends readonly [infer _, ...infer Rest] ? InArray<Rest, X> : false;
3
+ import type { NotEmptyArray, Prettify, UniqueArray } from "../../utils/types.js";
7
4
  export declare enum ResponderType {
8
5
  Button = "button",
9
6
  StringSelect = "select.string",
package/dist/env.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { StandardSchemaV1 } from "./utils/schema.js";
1
+ import type { StandardSchemaV1 } from "@standard-schema/spec";
2
2
  type Schema = StandardSchemaV1;
3
3
  type Out<T extends Schema> = StandardSchemaV1.InferOutput<T>;
4
4
  /**
@@ -1,3 +1,7 @@
1
1
  export type Prettify<T> = {
2
2
  [K in keyof T]: T[K];
3
3
  } & {};
4
+ export type NotEmptyArray<T> = T extends never[] ? never : T;
5
+ export type UniqueArray<T> = T extends readonly [infer X, ...infer Rest] ? InArray<Rest, X> extends true ? ["Encountered value with duplicates:", X] : readonly [X, ...UniqueArray<Rest>] : T;
6
+ type InArray<T, X> = T extends readonly [X, ...infer _Rest] ? true : T extends readonly [X] ? true : T extends readonly [infer _, ...infer Rest] ? InArray<Rest, X> : false;
7
+ export {};
package/dist/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/version.ts
2
- var version = "0.0.4";
2
+ var version = "0.0.5";
3
3
  export {
4
4
  version
5
5
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@constatic/base",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "Library with structures and functions for creating modern Discord applications.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -33,7 +33,7 @@
33
33
  ],
34
34
  "repository": {
35
35
  "type": "git",
36
- "url": "https://github.com/rinckodev/constatic",
36
+ "url": "git+https://github.com/rinckodev/constatic.git",
37
37
  "directory": "packages/base"
38
38
  },
39
39
  "author": {
@@ -42,6 +42,7 @@
42
42
  },
43
43
  "devDependencies": {
44
44
  "@constatic/config": "*",
45
+ "@standard-schema/spec": "^1.1.0",
45
46
  "typescript": "^5.8.2"
46
47
  },
47
48
  "exports": {
@@ -66,7 +67,6 @@
66
67
  "typegen": "tsc",
67
68
  "build": "bun run ./build.ts",
68
69
  "prepublish": "bun run build",
69
- "publish": "npm publish",
70
70
  "dev": "bun run ./playground/index.ts"
71
71
  },
72
72
  "peerDependencies": {