bakit 1.0.0-beta.5 → 1.0.0-beta.8

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,7 +1,35 @@
1
1
  import * as discord_js from 'discord.js';
2
- import { ChatInputCommandInteraction, CacheType, Message, InteractionReplyOptions, MessageCreateOptions, User, Awaitable, Collection, RESTPostAPIApplicationCommandsJSONBody, Client, ClientOptions, IntentsBitField } from 'discord.js';
2
+ import { Awaitable, ChatInputCommandInteraction, CacheType, Message, InteractionReplyOptions, MessageCreateOptions, User, Collection, RESTPostAPIApplicationCommandsJSONBody, Client, ClientOptions, IntentsBitField } from 'discord.js';
3
3
  import { SetOptional } from 'type-fest';
4
4
 
5
+ type ConstructorLike = new (...args: unknown[]) => object;
6
+ declare enum HookExecutionState {
7
+ Main = "MAIN",
8
+ Pre = "PRE",
9
+ Post = "POST",
10
+ Error = "ERROR"
11
+ }
12
+ type BaseMainHookMethod<Args extends unknown[]> = (...args: Args) => Awaitable<void>;
13
+ type BaseErrorHookMethod<Args extends unknown[]> = (error: unknown, ...args: Args) => Awaitable<void>;
14
+ interface BaseHook {
15
+ state: HookExecutionState;
16
+ method: BaseMainHookMethod<never> | BaseErrorHookMethod<never>;
17
+ entry: unknown;
18
+ }
19
+ declare class BaseEntry<Constructor extends ConstructorLike, Hook extends BaseHook, MainHookMethod extends BaseMainHookMethod<never>, ErrorHookMethod extends BaseErrorHookMethod<never>> {
20
+ protected target?: Constructor;
21
+ hooks: Record<HookExecutionState, Hook | undefined>;
22
+ main: <T extends MainHookMethod>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
23
+ pre: <T extends MainHookMethod>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
24
+ post: <T extends MainHookMethod>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
25
+ error: <T extends MainHookMethod>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
26
+ constructor();
27
+ setTarget(target: Constructor): void;
28
+ createMainHookDecorator(state: HookExecutionState): <T extends MainHookMethod>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
29
+ createErrorHookDecorator(state: HookExecutionState): <T extends ErrorHookMethod>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
30
+ protected addHook<T extends Hook["method"]>(state: HookExecutionState, target: object, descriptor: TypedPropertyDescriptor<T>): void;
31
+ }
32
+
5
33
  type ChatInputContextSendOptions = string | InteractionReplyOptions;
6
34
  type MessageContextSendOptions = string | MessageCreateOptions;
7
35
  type ContextSendOptions = ChatInputContextSendOptions | MessageContextSendOptions;
@@ -30,26 +58,10 @@ declare class MessageContext<Cached extends boolean = boolean, InGuild extends b
30
58
  }
31
59
  type Context<Cached extends boolean = boolean, InGuild extends boolean = boolean> = ChatInputContext<Cached, InGuild> | MessageContext<Cached, InGuild>;
32
60
 
33
- type CommandConstructor = new (...args: unknown[]) => object;
34
- declare function use(command: RootCommandEntry): (target: CommandConstructor) => void;
35
- declare function getRoot(constructor: CommandConstructor): RootCommandEntry | undefined;
36
- declare function CommandFactory(options: CreateCommandOptions<BaseCommandEntryOptions> | string): RootCommandEntry;
37
- declare const Command: typeof CommandFactory & {
38
- use: typeof use;
39
- getRoot: typeof getRoot;
40
- };
41
-
42
- declare enum CommandHookExecutionState {
43
- Main = "main",
44
- Pre = "pre",
45
- Post = "post",
46
- Error = "error"
47
- }
48
- type CommandHookMethod = (ctx: Context, ...args: any[]) => Awaitable<void>;
49
- interface CommandHook {
50
- state: CommandHookExecutionState;
51
- method: CommandHookMethod;
52
- entry: CommandEntry;
61
+ type MainCommandHookMethod = BaseMainHookMethod<[context: Context, ...args: unknown[]]>;
62
+ type ErrorCommandHookMethod = BaseErrorHookMethod<[context: Context, ...args: unknown[]]>;
63
+ interface CommandHook extends BaseHook {
64
+ method: MainCommandHookMethod | ErrorCommandHookMethod;
53
65
  }
54
66
  interface BaseCommandEntryOptions {
55
67
  name: string;
@@ -57,42 +69,26 @@ interface BaseCommandEntryOptions {
57
69
  nsfw?: boolean;
58
70
  }
59
71
  type CreateCommandOptions<T extends BaseCommandEntryOptions> = SetOptional<T, "description"> | string;
60
- declare const HOOKS_KEY: unique symbol;
61
- declare abstract class BaseCommandEntry {
72
+ declare class BaseCommandEntry extends BaseEntry<ConstructorLike, CommandHook, MainCommandHookMethod, ErrorCommandHookMethod> {
62
73
  options: BaseCommandEntryOptions;
63
- private static cache;
64
- main: <T extends CommandHookMethod>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
65
- pre: <T extends CommandHookMethod>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
66
- post: <T extends CommandHookMethod>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
67
- error: <T extends CommandHookMethod>(target: object, _key: string, descriptor: TypedPropertyDescriptor<T>) => void;
68
74
  constructor(options: BaseCommandEntryOptions);
69
- static getHooks(constructor: CommandConstructor): readonly CommandHook[];
70
- static getHooks(constructor: CommandConstructor, init: true): CommandHook[];
71
- private static createMainHookDecorator;
72
- private static createErrorHookDecorator;
73
- private static addHook;
74
75
  }
75
- declare abstract class BaseCommandGroupEntry extends BaseCommandEntry {
76
+ declare class BaseCommandGroupEntry extends BaseCommandEntry {
76
77
  children: Collection<string, CommandGroupEntry | SubcommandEntry>;
77
78
  subcommand(options: CreateCommandOptions<BaseCommandEntryOptions>): SubcommandEntry;
78
79
  }
80
+ declare class RootCommandEntry extends BaseCommandGroupEntry {
81
+ group(options: CreateCommandOptions<BaseCommandEntryOptions>): CommandGroupEntry;
82
+ }
79
83
  declare class CommandGroupEntry extends BaseCommandGroupEntry {
80
- options: BaseCommandEntryOptions;
81
84
  parent: RootCommandEntry;
82
85
  children: Collection<string, SubcommandEntry>;
83
86
  constructor(options: BaseCommandEntryOptions, parent: RootCommandEntry);
84
87
  }
85
- declare class RootCommandEntry extends BaseCommandGroupEntry {
86
- options: BaseCommandEntryOptions;
87
- constructor(options: BaseCommandEntryOptions);
88
- group(options: CreateCommandOptions<BaseCommandEntryOptions>): CommandGroupEntry;
89
- }
90
88
  declare class SubcommandEntry extends BaseCommandEntry {
91
- options: BaseCommandEntryOptions;
92
- parent: RootCommandEntry | CommandGroupEntry;
93
- constructor(options: BaseCommandEntryOptions, parent: RootCommandEntry | CommandGroupEntry);
89
+ parent: BaseCommandGroupEntry;
90
+ constructor(options: BaseCommandEntryOptions, parent: BaseCommandGroupEntry);
94
91
  }
95
- type CommandEntry = RootCommandEntry | CommandGroupEntry | SubcommandEntry;
96
92
 
97
93
  declare enum ArgumentType {
98
94
  String = "string",
@@ -148,15 +144,38 @@ declare const Arg: {
148
144
  member: (options: string | Omit<MemberArgumentOptions, "type">) => (target: object, key: string | symbol, _index: number) => void;
149
145
  };
150
146
 
147
+ declare namespace CommandAPI {
148
+ function use(root: RootCommandEntry): (target: ConstructorLike) => void;
149
+ function getRoot(constructor: ConstructorLike): RootCommandEntry | undefined;
150
+ }
151
+ declare function CommandFactory(options: CreateCommandOptions<BaseCommandEntryOptions> | string): RootCommandEntry;
152
+ declare const Command: typeof CommandFactory & typeof CommandAPI;
153
+
151
154
  declare class CommandRegistry {
152
- static constructors: Collection<string, CommandConstructor>;
155
+ static constructors: Collection<string, ConstructorLike>;
153
156
  static instances: Collection<string, object>;
154
- static add(constructor: CommandConstructor): void;
155
- static buildSlashCommand(constructor: CommandConstructor): RESTPostAPIApplicationCommandsJSONBody;
156
- static loadDirectory(pattern: string, parallel?: boolean): Promise<CommandConstructor[]>;
157
+ /**
158
+ * Add a command to the registry.
159
+ * @param constructor The command class you want to add.
160
+ */
161
+ static add(constructor: ConstructorLike): void;
162
+ /**
163
+ * Load and add all commands which matched provided glob pattern to the registry.
164
+ * @param pattern glob pattern to load.
165
+ * @param parallel load all matched results in parallel, enabled by default.
166
+ * @returns All loaded command constructors.
167
+ */
168
+ static load(pattern: string, parallel?: boolean): Promise<ConstructorLike[]>;
169
+ /**
170
+ * Build a command into application command data.
171
+ * @param constructor The command class you want to build.
172
+ * @returns a REST JSON version of the application command data.
173
+ */
174
+ static buildSlashCommand(constructor: ConstructorLike): RESTPostAPIApplicationCommandsJSONBody;
175
+ private static getMainHookArguments;
176
+ private static buildSlashCommandSubcommands;
157
177
  private static buildSlashCommandOptions;
158
- private static buildSubcommand;
159
- private static buildSlashCommandOption;
178
+ private static attachSlashCommandOption;
160
179
  }
161
180
 
162
181
  declare enum CommandSyntaxErrorType {
@@ -195,10 +214,11 @@ declare class BakitClient<Ready extends boolean = boolean> extends Client<Ready>
195
214
  private handleInteraction;
196
215
  private handleChatInputHooks;
197
216
  private handleMessageHooks;
217
+ private handleChildMessageHooks;
198
218
  private runMessageHooks;
199
219
  private runChatInputHooks;
220
+ private runHooks;
200
221
  private getChatInputTargetHooks;
201
- private createHookRecord;
202
222
  }
203
223
 
204
- export { Arg as A, BakitClient as B, CommandHookExecutionState as C, CommandSyntaxError as D, type GetSyntaxErrorMessageFunction as G, HOOKS_KEY as H, type IntegerArgumentOptions as I, type MemberArgumentOptions as M, type NumberArgumentOptions as N, RootCommandEntry as R, type StringArgumentOptions as S, type UserArgumentOptions as U, type BakitClientOptions as a, ArgumentType as b, type BaseArgumentOptions as c, type ArgumentOptions as d, type CommandHookMethod as e, type CommandHook as f, type BaseCommandEntryOptions as g, type CreateCommandOptions as h, BaseCommandEntry as i, BaseCommandGroupEntry as j, CommandGroupEntry as k, SubcommandEntry as l, type CommandEntry as m, type CommandConstructor as n, CommandFactory as o, Command as p, CommandRegistry as q, type ChatInputContextSendOptions as r, type MessageContextSendOptions as s, type ContextSendOptions as t, BaseContext as u, ChatInputContext as v, MessageContext as w, type Context as x, CommandSyntaxErrorType as y, type CommandSyntaxErrorOptions as z };
224
+ export { Arg as A, BaseEntry as B, type ConstructorLike as C, type Context as D, type ErrorCommandHookMethod as E, CommandSyntaxErrorType as F, type GetSyntaxErrorMessageFunction as G, type CommandSyntaxErrorOptions as H, type IntegerArgumentOptions as I, CommandSyntaxError as J, type MemberArgumentOptions as M, type NumberArgumentOptions as N, RootCommandEntry as R, type StringArgumentOptions as S, type UserArgumentOptions as U, type BaseHook as a, type BaseMainHookMethod as b, type BaseErrorHookMethod as c, BakitClient as d, type BakitClientOptions as e, ArgumentType as f, type BaseArgumentOptions as g, type ArgumentOptions as h, type MainCommandHookMethod as i, type CommandHook as j, type BaseCommandEntryOptions as k, type CreateCommandOptions as l, BaseCommandEntry as m, BaseCommandGroupEntry as n, CommandGroupEntry as o, SubcommandEntry as p, CommandAPI as q, CommandFactory as r, Command as s, CommandRegistry as t, type ChatInputContextSendOptions as u, type MessageContextSendOptions as v, type ContextSendOptions as w, BaseContext as x, ChatInputContext as y, MessageContext as z };
@@ -1,3 +1,3 @@
1
- export { A as Arg, d as ArgumentOptions, b as ArgumentType, c as BaseArgumentOptions, i as BaseCommandEntry, g as BaseCommandEntryOptions, j as BaseCommandGroupEntry, u as BaseContext, v as ChatInputContext, r as ChatInputContextSendOptions, p as Command, n as CommandConstructor, m as CommandEntry, o as CommandFactory, k as CommandGroupEntry, f as CommandHook, C as CommandHookExecutionState, e as CommandHookMethod, q as CommandRegistry, x as Context, t as ContextSendOptions, h as CreateCommandOptions, H as HOOKS_KEY, I as IntegerArgumentOptions, M as MemberArgumentOptions, w as MessageContext, s as MessageContextSendOptions, N as NumberArgumentOptions, R as RootCommandEntry, S as StringArgumentOptions, l as SubcommandEntry, U as UserArgumentOptions } from '../BakitClient-CgtDS6Hr.js';
1
+ export { A as Arg, h as ArgumentOptions, f as ArgumentType, g as BaseArgumentOptions, m as BaseCommandEntry, k as BaseCommandEntryOptions, n as BaseCommandGroupEntry, x as BaseContext, y as ChatInputContext, u as ChatInputContextSendOptions, s as Command, q as CommandAPI, r as CommandFactory, o as CommandGroupEntry, j as CommandHook, t as CommandRegistry, D as Context, w as ContextSendOptions, l as CreateCommandOptions, E as ErrorCommandHookMethod, I as IntegerArgumentOptions, i as MainCommandHookMethod, M as MemberArgumentOptions, z as MessageContext, v as MessageContextSendOptions, N as NumberArgumentOptions, R as RootCommandEntry, S as StringArgumentOptions, p as SubcommandEntry, U as UserArgumentOptions } from '../BakitClient-D9kRvFS3.js';
2
2
  import 'discord.js';
3
3
  import 'type-fest';
@@ -1,6 +1,3 @@
1
- // src/command/index.ts
2
- import "reflect-metadata";
3
-
4
1
  // src/command/argument/Argument.ts
5
2
  var ArgumentType = /* @__PURE__ */ ((ArgumentType2) => {
6
3
  ArgumentType2["String"] = "string";
@@ -106,138 +103,115 @@ var Arg = {
106
103
 
107
104
  // src/command/CommandEntry.ts
108
105
  import { Collection } from "discord.js";
109
- var CommandHookExecutionState = /* @__PURE__ */ ((CommandHookExecutionState2) => {
110
- CommandHookExecutionState2["Main"] = "main";
111
- CommandHookExecutionState2["Pre"] = "pre";
112
- CommandHookExecutionState2["Post"] = "post";
113
- CommandHookExecutionState2["Error"] = "error";
114
- return CommandHookExecutionState2;
115
- })(CommandHookExecutionState || {});
116
- var HOOKS_KEY = Symbol("hooks");
117
- var BaseCommandEntry = class _BaseCommandEntry {
118
- constructor(options) {
119
- this.options = options;
120
- }
121
- static cache = /* @__PURE__ */ new WeakMap();
122
- main = _BaseCommandEntry.createMainHookDecorator("main" /* Main */, this);
123
- pre = _BaseCommandEntry.createMainHookDecorator("pre" /* Pre */, this);
124
- post = _BaseCommandEntry.createMainHookDecorator("post" /* Post */, this);
125
- error = _BaseCommandEntry.createErrorHookDecorator("error" /* Error */, this);
126
- static getHooks(constructor, init = false) {
127
- let hooks = this.cache.get(constructor) ?? Reflect.getMetadata(HOOKS_KEY, constructor);
128
- if (!hooks) {
129
- hooks = [];
130
- if (init) {
131
- Reflect.defineMetadata(HOOKS_KEY, hooks, constructor);
132
- this.cache.set(constructor, hooks);
133
- }
134
- }
135
- return init ? hooks : Object.freeze([...hooks]);
136
- }
137
- static createMainHookDecorator(state, entry) {
106
+
107
+ // src/base/BaseEntry.ts
108
+ var BaseEntry = class {
109
+ target;
110
+ hooks = {
111
+ ["MAIN" /* Main */]: void 0,
112
+ ["ERROR" /* Error */]: void 0,
113
+ ["POST" /* Post */]: void 0,
114
+ ["PRE" /* Pre */]: void 0
115
+ };
116
+ main;
117
+ pre;
118
+ post;
119
+ error;
120
+ constructor() {
121
+ this.main = this.createMainHookDecorator("MAIN" /* Main */);
122
+ this.pre = this.createMainHookDecorator("PRE" /* Pre */);
123
+ this.post = this.createMainHookDecorator("POST" /* Post */);
124
+ this.error = this.createMainHookDecorator("ERROR" /* Error */);
125
+ }
126
+ setTarget(target) {
127
+ this.target = target;
128
+ }
129
+ createMainHookDecorator(state) {
138
130
  return (target, _key, descriptor) => {
139
- this.addHook(target, state, descriptor.value, entry);
131
+ this.addHook(state, target, descriptor);
140
132
  };
141
133
  }
142
- static createErrorHookDecorator(state, entry) {
134
+ createErrorHookDecorator(state) {
143
135
  return (target, _key, descriptor) => {
144
- this.addHook(target, state, descriptor.value, entry);
136
+ this.addHook(state, target, descriptor);
145
137
  };
146
138
  }
147
- static addHook(target, state, method, entry) {
148
- const { constructor } = target;
149
- const hooks = _BaseCommandEntry.getHooks(constructor, true);
150
- if (typeof method !== "function") {
151
- throw new Error("CommandEntry decorator must be used with a class method.");
152
- }
153
- if (hooks.some((hook) => hook.state === state && hook.entry === entry)) {
154
- throw new Error(`Hook "${state}" is already defined for entry "${entry.options.name}".`);
139
+ addHook(state, target, descriptor) {
140
+ if (target.constructor !== this.target) {
141
+ throw new Error("Hook is used at unknown constructor.");
155
142
  }
156
- if ("parent" in entry) {
157
- const parentHook = hooks.find(
158
- (hook) => hook.entry === entry.parent && hook.state === "main" /* Main */
159
- );
160
- if (parentHook) {
161
- const parentArgs = Arg.getMethodArguments(parentHook.method);
162
- if (parentArgs.at(-1)?.tuple) {
163
- throw new Error(
164
- `Cannot add hook "${state}" to entry "${entry.options.name}" because its parent "${entry.parent.options.name}" has a tuple argument.`
165
- );
166
- }
167
- }
143
+ const { value: method } = descriptor;
144
+ if (typeof method !== "function") {
145
+ throw new Error("Invalid target method for hook.");
168
146
  }
169
- hooks.push({
147
+ const hook = {
170
148
  state,
171
- entry,
172
- method
173
- });
149
+ method,
150
+ entry: this
151
+ };
152
+ this.hooks[state] = hook;
153
+ }
154
+ };
155
+
156
+ // src/command/CommandEntry.ts
157
+ var BaseCommandEntry = class extends BaseEntry {
158
+ constructor(options) {
159
+ super();
160
+ this.options = options;
174
161
  }
175
162
  };
176
163
  var BaseCommandGroupEntry = class extends BaseCommandEntry {
177
164
  children = new Collection();
178
165
  subcommand(options) {
179
- if (typeof options === "string") {
180
- options = { name: options };
166
+ const fullOptions = typeof options === "string" ? { name: options, description: `${options} command` } : { description: `${options.name} command`, ...options };
167
+ if (this.children.has(fullOptions.name)) {
168
+ throw new Error(`Entry "${fullOptions.name}" is already existed.`);
181
169
  }
182
- if (!options.description) {
183
- options.description = options.name;
184
- }
185
- if (this.children.has(options.name)) {
186
- throw new Error(`Entry with name "${options.name}" already exists.`);
187
- }
188
- if (!(this instanceof CommandGroupEntry) && !(this instanceof RootCommandEntry)) {
189
- throw new Error(`Invalid parent "${this.constructor.name}"`);
170
+ const subcommand = new SubcommandEntry(fullOptions, this);
171
+ this.children.set(fullOptions.name, subcommand);
172
+ return subcommand;
173
+ }
174
+ };
175
+ var RootCommandEntry = class extends BaseCommandGroupEntry {
176
+ group(options) {
177
+ const fullOptions = typeof options === "string" ? { name: options, description: `${options} command` } : { description: `${options.name} command`, ...options };
178
+ if (this.children.has(fullOptions.name)) {
179
+ throw new Error(`Entry "${fullOptions.name}" is already existed.`);
190
180
  }
191
- const entry = new SubcommandEntry(options, this);
192
- this.children.set(entry.options.name, entry);
193
- return entry;
181
+ const group = new CommandGroupEntry(fullOptions, this);
182
+ this.children.set(fullOptions.name, group);
183
+ return group;
194
184
  }
195
185
  };
196
186
  var CommandGroupEntry = class extends BaseCommandGroupEntry {
197
187
  constructor(options, parent) {
198
188
  super(options);
199
- this.options = options;
200
189
  this.parent = parent;
201
190
  }
202
191
  };
203
- var RootCommandEntry = class extends BaseCommandGroupEntry {
204
- constructor(options) {
205
- super(options);
206
- this.options = options;
207
- }
208
- group(options) {
209
- if (typeof options === "string") {
210
- options = { name: options };
211
- }
212
- if (!options.description) {
213
- options.description = options.name;
214
- }
215
- if (this.children.has(options.name)) {
216
- throw new Error(`Entry with name "${options.name}" already exists.`);
217
- }
218
- const entry = new CommandGroupEntry(options, this);
219
- this.children.set(entry.options.name, entry);
220
- return entry;
221
- }
222
- };
223
192
  var SubcommandEntry = class extends BaseCommandEntry {
224
193
  constructor(options, parent) {
225
194
  super(options);
226
- this.options = options;
227
195
  this.parent = parent;
228
196
  }
229
197
  };
230
198
 
231
199
  // src/command/Command.ts
232
- var ROOT_KEY = Symbol("root");
233
- function use(command) {
234
- return (target) => {
235
- Reflect.defineMetadata(ROOT_KEY, command, target);
236
- };
237
- }
238
- function getRoot(constructor) {
239
- return Reflect.getMetadata(ROOT_KEY, constructor);
240
- }
200
+ var CommandAPI;
201
+ ((CommandAPI2) => {
202
+ const rootEntries = /* @__PURE__ */ new WeakMap();
203
+ function use(root) {
204
+ return (target) => {
205
+ root.setTarget(target);
206
+ rootEntries.set(target, root);
207
+ };
208
+ }
209
+ CommandAPI2.use = use;
210
+ function getRoot(constructor) {
211
+ return rootEntries.get(constructor);
212
+ }
213
+ CommandAPI2.getRoot = getRoot;
214
+ })(CommandAPI || (CommandAPI = {}));
241
215
  function CommandFactory(options) {
242
216
  if (typeof options === "string") {
243
217
  options = { name: options };
@@ -247,10 +221,7 @@ function CommandFactory(options) {
247
221
  }
248
222
  return new RootCommandEntry(options);
249
223
  }
250
- var Command = Object.assign(CommandFactory, {
251
- use,
252
- getRoot
253
- });
224
+ var Command = Object.assign(CommandFactory, CommandAPI);
254
225
 
255
226
  // src/command/CommandRegistry.ts
256
227
  import {
@@ -264,6 +235,10 @@ import { pathToFileURL } from "url";
264
235
  var CommandRegistry = class _CommandRegistry {
265
236
  static constructors = new Collection2();
266
237
  static instances = new Collection2();
238
+ /**
239
+ * Add a command to the registry.
240
+ * @param constructor The command class you want to add.
241
+ */
267
242
  static add(constructor) {
268
243
  const root = Command.getRoot(constructor);
269
244
  if (!root) {
@@ -273,20 +248,13 @@ var CommandRegistry = class _CommandRegistry {
273
248
  this.constructors.set(options.name, constructor);
274
249
  this.instances.set(options.name, new constructor());
275
250
  }
276
- static buildSlashCommand(constructor) {
277
- const builder = new SlashCommandBuilder();
278
- const root = Command.getRoot(constructor);
279
- if (!root) {
280
- throw new Error(`No root found for "${constructor.name}"`);
281
- }
282
- const { options } = root;
283
- builder.setName(options.name);
284
- builder.setDescription(options.description || "");
285
- builder.setNSFW(Boolean(options.nsfw));
286
- this.buildSlashCommandOptions(builder, constructor);
287
- return builder.toJSON();
288
- }
289
- static async loadDirectory(pattern, parallel = true) {
251
+ /**
252
+ * Load and add all commands which matched provided glob pattern to the registry.
253
+ * @param pattern glob pattern to load.
254
+ * @param parallel load all matched results in parallel, enabled by default.
255
+ * @returns All loaded command constructors.
256
+ */
257
+ static async load(pattern, parallel = true) {
290
258
  const files = await glob(pattern);
291
259
  const loaders = files.map(async (file) => {
292
260
  const fileURL = pathToFileURL(file).toString();
@@ -303,72 +271,102 @@ var CommandRegistry = class _CommandRegistry {
303
271
  }
304
272
  return result;
305
273
  }
306
- static buildSlashCommandOptions(builder, constructor) {
274
+ /**
275
+ * Build a command into application command data.
276
+ * @param constructor The command class you want to build.
277
+ * @returns a REST JSON version of the application command data.
278
+ */
279
+ static buildSlashCommand(constructor) {
307
280
  const root = Command.getRoot(constructor);
308
281
  if (!root) {
309
282
  throw new Error(`No root found for "${constructor.name}"`);
310
283
  }
311
- const hooks = new Collection2(
312
- BaseCommandEntry.getHooks(constructor).filter((hook) => hook.state === "main" /* Main */).map((hook) => [hook.entry, hook])
313
- );
314
- const rootHook = hooks.get(root);
315
- if (!root.children.size && hooks.size) {
316
- if (!rootHook) {
317
- return;
318
- }
319
- for (const arg of Arg.getMethodArguments(rootHook.method)) {
320
- this.buildSlashCommandOption(builder, arg);
321
- }
322
- return;
284
+ const { options } = root;
285
+ const builder = new SlashCommandBuilder().setName(options.name).setDescription(options.description).setNSFW(Boolean(options.nsfw));
286
+ const args = this.getMainHookArguments(root);
287
+ if (root.children.size) {
288
+ this.buildSlashCommandSubcommands(builder, root, args);
289
+ } else {
290
+ this.buildSlashCommandOptions(builder, args);
323
291
  }
324
- for (const child of root.children.values()) {
325
- const hook = hooks.get(child);
326
- const inheritedArgs = [];
327
- if (rootHook) {
328
- inheritedArgs.push(...Arg.getMethodArguments(rootHook.method));
329
- }
330
- if (child instanceof SubcommandEntry) {
331
- this.buildSubcommand(builder, child, hook, inheritedArgs);
332
- } else if (child instanceof CommandGroupEntry) {
333
- if (hook) {
334
- inheritedArgs.push(...Arg.getMethodArguments(hook.method));
335
- }
292
+ return builder.toJSON();
293
+ }
294
+ static getMainHookArguments(entry) {
295
+ const { hooks } = entry;
296
+ const mainHook = hooks["MAIN" /* Main */];
297
+ return mainHook ? Arg.getMethodArguments(mainHook.method) : [];
298
+ }
299
+ static buildSlashCommandSubcommands(parent, entry, inheritedArgs) {
300
+ const { children } = entry;
301
+ for (const child of children.values()) {
302
+ if (child instanceof CommandGroupEntry && parent instanceof SlashCommandBuilder) {
336
303
  const { options } = child;
337
304
  const group = new SlashCommandSubcommandGroupBuilder().setName(options.name).setDescription(options.description);
338
- for (const subChild of child.children.values()) {
339
- this.buildSubcommand(group, subChild, hooks.get(subChild), inheritedArgs);
340
- }
341
- builder.addSubcommandGroup(group);
305
+ this.buildSlashCommandSubcommands(group, child, [
306
+ ...inheritedArgs,
307
+ ...this.getMainHookArguments(child)
308
+ ]);
309
+ parent.addSubcommandGroup(group);
310
+ } else if (child instanceof SubcommandEntry) {
311
+ const { options } = child;
312
+ const subcommand = new SlashCommandSubcommandBuilder().setName(options.name).setDescription(options.description);
313
+ this.buildSlashCommandOptions(subcommand, [
314
+ ...inheritedArgs,
315
+ ...this.getMainHookArguments(child)
316
+ ]);
317
+ parent.addSubcommand(subcommand);
342
318
  }
343
319
  }
344
320
  }
345
- static buildSubcommand(parent, entry, hook, inheritedArgs) {
346
- const { options } = entry;
347
- const subcommand = new SlashCommandSubcommandBuilder().setName(options.name).setDescription(options.description);
348
- const args = [...inheritedArgs];
349
- if (hook) {
350
- args.push(...Arg.getMethodArguments(hook.method));
351
- }
352
- for (const arg of args) {
353
- this.buildSlashCommandOption(subcommand, arg);
321
+ static buildSlashCommandOptions(builder, args) {
322
+ const argGroup = Object.groupBy(args, ({ required }) => required ? "required" : "optional");
323
+ const orderedArgs = [...argGroup.required || [], ...argGroup.optional || []];
324
+ for (const arg of orderedArgs) {
325
+ this.attachSlashCommandOption(builder, arg);
354
326
  }
355
- parent.addSubcommand(subcommand);
356
327
  }
357
- static buildSlashCommandOption(builder, arg) {
328
+ static attachSlashCommandOption(builder, arg) {
358
329
  const setupOption = (option) => {
359
330
  return option.setName(arg.name).setDescription(arg.description || arg.name).setRequired(Boolean(arg.required));
360
331
  };
361
332
  switch (arg.type) {
362
333
  case "string" /* String */: {
363
- builder.addStringOption((option) => setupOption(option));
334
+ builder.addStringOption((data) => {
335
+ const option = setupOption(data);
336
+ if (arg.maxLength) {
337
+ option.setMaxLength(arg.maxLength);
338
+ }
339
+ if (arg.minLength) {
340
+ option.setMinLength(arg.minLength);
341
+ }
342
+ return option;
343
+ });
364
344
  break;
365
345
  }
366
346
  case "integer" /* Integer */: {
367
- builder.addIntegerOption((option) => setupOption(option));
347
+ builder.addIntegerOption((data) => {
348
+ const option = setupOption(data);
349
+ if (arg.maxValue) {
350
+ option.setMaxValue(arg.maxValue);
351
+ }
352
+ if (arg.minValue) {
353
+ option.setMinValue(arg.minValue);
354
+ }
355
+ return option;
356
+ });
368
357
  break;
369
358
  }
370
359
  case "number" /* Number */: {
371
- builder.addNumberOption((option) => setupOption(option));
360
+ builder.addNumberOption((data) => {
361
+ const option = setupOption(data);
362
+ if (arg.maxValue) {
363
+ option.setMaxValue(arg.maxValue);
364
+ }
365
+ if (arg.minValue) {
366
+ option.setMinValue(arg.minValue);
367
+ }
368
+ return option;
369
+ });
372
370
  break;
373
371
  }
374
372
  case "user" /* User */:
@@ -465,11 +463,10 @@ export {
465
463
  BaseContext,
466
464
  ChatInputContext,
467
465
  Command,
466
+ CommandAPI,
468
467
  CommandFactory,
469
468
  CommandGroupEntry,
470
- CommandHookExecutionState,
471
469
  CommandRegistry,
472
- HOOKS_KEY,
473
470
  MessageContext,
474
471
  RootCommandEntry,
475
472
  SubcommandEntry