bakit 1.0.0-beta.5 → 1.0.0-beta.7
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.
- package/dist/{BakitClient-CgtDS6Hr.d.ts → BakitClient-CU9viQZL.d.ts} +72 -52
- package/dist/command/index.d.ts +1 -1
- package/dist/command/index.js +163 -163
- package/dist/index.d.ts +67 -50
- package/dist/index.js +452 -470
- package/package.json +1 -1
|
@@ -1,7 +1,35 @@
|
|
|
1
1
|
import * as discord_js from 'discord.js';
|
|
2
|
-
import { ChatInputCommandInteraction, CacheType, Message, InteractionReplyOptions, MessageCreateOptions, User,
|
|
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
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
92
|
-
|
|
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(command: 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,
|
|
155
|
+
static constructors: Collection<string, ConstructorLike>;
|
|
153
156
|
static instances: Collection<string, object>;
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
|
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,
|
|
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 };
|
package/dist/command/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { A as Arg,
|
|
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-CU9viQZL.js';
|
|
2
2
|
import 'discord.js';
|
|
3
3
|
import 'type-fest';
|
package/dist/command/index.js
CHANGED
|
@@ -106,138 +106,115 @@ var Arg = {
|
|
|
106
106
|
|
|
107
107
|
// src/command/CommandEntry.ts
|
|
108
108
|
import { Collection } from "discord.js";
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
return init ? hooks : Object.freeze([...hooks]);
|
|
136
|
-
}
|
|
137
|
-
static createMainHookDecorator(state, entry) {
|
|
109
|
+
|
|
110
|
+
// src/base/BaseEntry.ts
|
|
111
|
+
var BaseEntry = class {
|
|
112
|
+
target;
|
|
113
|
+
hooks = {
|
|
114
|
+
["MAIN" /* Main */]: void 0,
|
|
115
|
+
["ERROR" /* Error */]: void 0,
|
|
116
|
+
["POST" /* Post */]: void 0,
|
|
117
|
+
["PRE" /* Pre */]: void 0
|
|
118
|
+
};
|
|
119
|
+
main;
|
|
120
|
+
pre;
|
|
121
|
+
post;
|
|
122
|
+
error;
|
|
123
|
+
constructor() {
|
|
124
|
+
this.main = this.createMainHookDecorator("MAIN" /* Main */);
|
|
125
|
+
this.pre = this.createMainHookDecorator("PRE" /* Pre */);
|
|
126
|
+
this.post = this.createMainHookDecorator("POST" /* Post */);
|
|
127
|
+
this.error = this.createMainHookDecorator("ERROR" /* Error */);
|
|
128
|
+
}
|
|
129
|
+
setTarget(target) {
|
|
130
|
+
this.target = target;
|
|
131
|
+
}
|
|
132
|
+
createMainHookDecorator(state) {
|
|
138
133
|
return (target, _key, descriptor) => {
|
|
139
|
-
this.addHook(
|
|
134
|
+
this.addHook(state, target, descriptor);
|
|
140
135
|
};
|
|
141
136
|
}
|
|
142
|
-
|
|
137
|
+
createErrorHookDecorator(state) {
|
|
143
138
|
return (target, _key, descriptor) => {
|
|
144
|
-
this.addHook(
|
|
139
|
+
this.addHook(state, target, descriptor);
|
|
145
140
|
};
|
|
146
141
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (typeof method !== "function") {
|
|
151
|
-
throw new Error("CommandEntry decorator must be used with a class method.");
|
|
142
|
+
addHook(state, target, descriptor) {
|
|
143
|
+
if (target.constructor !== this.target) {
|
|
144
|
+
throw new Error("Hook is used at unknown constructor.");
|
|
152
145
|
}
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
}
|
|
146
|
+
const { value: method } = descriptor;
|
|
147
|
+
if (typeof method !== "function") {
|
|
148
|
+
throw new Error("Invalid target method for hook.");
|
|
168
149
|
}
|
|
169
|
-
|
|
150
|
+
const hook = {
|
|
170
151
|
state,
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
}
|
|
152
|
+
method,
|
|
153
|
+
entry: this
|
|
154
|
+
};
|
|
155
|
+
this.hooks[state] = hook;
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
// src/command/CommandEntry.ts
|
|
160
|
+
var BaseCommandEntry = class extends BaseEntry {
|
|
161
|
+
constructor(options) {
|
|
162
|
+
super();
|
|
163
|
+
this.options = options;
|
|
174
164
|
}
|
|
175
165
|
};
|
|
176
166
|
var BaseCommandGroupEntry = class extends BaseCommandEntry {
|
|
177
167
|
children = new Collection();
|
|
178
168
|
subcommand(options) {
|
|
179
|
-
|
|
180
|
-
|
|
169
|
+
const fullOptions = typeof options === "string" ? { name: options, description: `${options} command` } : { description: `${options.name} command`, ...options };
|
|
170
|
+
if (this.children.has(fullOptions.name)) {
|
|
171
|
+
throw new Error(`Entry "${fullOptions.name}" is already existed.`);
|
|
181
172
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
173
|
+
const subcommand = new SubcommandEntry(fullOptions, this);
|
|
174
|
+
this.children.set(fullOptions.name, subcommand);
|
|
175
|
+
return subcommand;
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
var RootCommandEntry = class extends BaseCommandGroupEntry {
|
|
179
|
+
group(options) {
|
|
180
|
+
const fullOptions = typeof options === "string" ? { name: options, description: `${options} command` } : { description: `${options.name} command`, ...options };
|
|
181
|
+
if (this.children.has(fullOptions.name)) {
|
|
182
|
+
throw new Error(`Entry "${fullOptions.name}" is already existed.`);
|
|
190
183
|
}
|
|
191
|
-
const
|
|
192
|
-
this.children.set(
|
|
193
|
-
return
|
|
184
|
+
const group = new CommandGroupEntry(fullOptions, this);
|
|
185
|
+
this.children.set(fullOptions.name, group);
|
|
186
|
+
return group;
|
|
194
187
|
}
|
|
195
188
|
};
|
|
196
189
|
var CommandGroupEntry = class extends BaseCommandGroupEntry {
|
|
197
190
|
constructor(options, parent) {
|
|
198
191
|
super(options);
|
|
199
|
-
this.options = options;
|
|
200
192
|
this.parent = parent;
|
|
201
193
|
}
|
|
202
194
|
};
|
|
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
195
|
var SubcommandEntry = class extends BaseCommandEntry {
|
|
224
196
|
constructor(options, parent) {
|
|
225
197
|
super(options);
|
|
226
|
-
this.options = options;
|
|
227
198
|
this.parent = parent;
|
|
228
199
|
}
|
|
229
200
|
};
|
|
230
201
|
|
|
231
202
|
// src/command/Command.ts
|
|
232
|
-
var
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
}
|
|
203
|
+
var CommandAPI;
|
|
204
|
+
((CommandAPI2) => {
|
|
205
|
+
const ROOT_KEY = Symbol("root");
|
|
206
|
+
function use(command) {
|
|
207
|
+
return (target) => {
|
|
208
|
+
command.setTarget(target);
|
|
209
|
+
Reflect.defineMetadata(ROOT_KEY, command, target);
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
CommandAPI2.use = use;
|
|
213
|
+
function getRoot(constructor) {
|
|
214
|
+
return Reflect.getMetadata(ROOT_KEY, constructor);
|
|
215
|
+
}
|
|
216
|
+
CommandAPI2.getRoot = getRoot;
|
|
217
|
+
})(CommandAPI || (CommandAPI = {}));
|
|
241
218
|
function CommandFactory(options) {
|
|
242
219
|
if (typeof options === "string") {
|
|
243
220
|
options = { name: options };
|
|
@@ -247,10 +224,7 @@ function CommandFactory(options) {
|
|
|
247
224
|
}
|
|
248
225
|
return new RootCommandEntry(options);
|
|
249
226
|
}
|
|
250
|
-
var Command = Object.assign(CommandFactory,
|
|
251
|
-
use,
|
|
252
|
-
getRoot
|
|
253
|
-
});
|
|
227
|
+
var Command = Object.assign(CommandFactory, CommandAPI);
|
|
254
228
|
|
|
255
229
|
// src/command/CommandRegistry.ts
|
|
256
230
|
import {
|
|
@@ -264,6 +238,10 @@ import { pathToFileURL } from "url";
|
|
|
264
238
|
var CommandRegistry = class _CommandRegistry {
|
|
265
239
|
static constructors = new Collection2();
|
|
266
240
|
static instances = new Collection2();
|
|
241
|
+
/**
|
|
242
|
+
* Add a command to the registry.
|
|
243
|
+
* @param constructor The command class you want to add.
|
|
244
|
+
*/
|
|
267
245
|
static add(constructor) {
|
|
268
246
|
const root = Command.getRoot(constructor);
|
|
269
247
|
if (!root) {
|
|
@@ -273,20 +251,13 @@ var CommandRegistry = class _CommandRegistry {
|
|
|
273
251
|
this.constructors.set(options.name, constructor);
|
|
274
252
|
this.instances.set(options.name, new constructor());
|
|
275
253
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
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) {
|
|
254
|
+
/**
|
|
255
|
+
* Load and add all commands which matched provided glob pattern to the registry.
|
|
256
|
+
* @param pattern glob pattern to load.
|
|
257
|
+
* @param parallel load all matched results in parallel, enabled by default.
|
|
258
|
+
* @returns All loaded command constructors.
|
|
259
|
+
*/
|
|
260
|
+
static async load(pattern, parallel = true) {
|
|
290
261
|
const files = await glob(pattern);
|
|
291
262
|
const loaders = files.map(async (file) => {
|
|
292
263
|
const fileURL = pathToFileURL(file).toString();
|
|
@@ -303,72 +274,102 @@ var CommandRegistry = class _CommandRegistry {
|
|
|
303
274
|
}
|
|
304
275
|
return result;
|
|
305
276
|
}
|
|
306
|
-
|
|
277
|
+
/**
|
|
278
|
+
* Build a command into application command data.
|
|
279
|
+
* @param constructor The command class you want to build.
|
|
280
|
+
* @returns a REST JSON version of the application command data.
|
|
281
|
+
*/
|
|
282
|
+
static buildSlashCommand(constructor) {
|
|
307
283
|
const root = Command.getRoot(constructor);
|
|
308
284
|
if (!root) {
|
|
309
285
|
throw new Error(`No root found for "${constructor.name}"`);
|
|
310
286
|
}
|
|
311
|
-
const
|
|
312
|
-
|
|
313
|
-
);
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
}
|
|
319
|
-
for (const arg of Arg.getMethodArguments(rootHook.method)) {
|
|
320
|
-
this.buildSlashCommandOption(builder, arg);
|
|
321
|
-
}
|
|
322
|
-
return;
|
|
287
|
+
const { options } = root;
|
|
288
|
+
const builder = new SlashCommandBuilder().setName(options.name).setDescription(options.description).setNSFW(Boolean(options.nsfw));
|
|
289
|
+
const args = this.getMainHookArguments(root);
|
|
290
|
+
if (root.children.size) {
|
|
291
|
+
this.buildSlashCommandSubcommands(builder, root, args);
|
|
292
|
+
} else {
|
|
293
|
+
this.buildSlashCommandOptions(builder, args);
|
|
323
294
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
}
|
|
295
|
+
return builder.toJSON();
|
|
296
|
+
}
|
|
297
|
+
static getMainHookArguments(entry) {
|
|
298
|
+
const { hooks } = entry;
|
|
299
|
+
const mainHook = hooks["MAIN" /* Main */];
|
|
300
|
+
return mainHook ? Arg.getMethodArguments(mainHook.method) : [];
|
|
301
|
+
}
|
|
302
|
+
static buildSlashCommandSubcommands(parent, entry, inheritedArgs) {
|
|
303
|
+
const { children } = entry;
|
|
304
|
+
for (const child of children.values()) {
|
|
305
|
+
if (child instanceof CommandGroupEntry && parent instanceof SlashCommandBuilder) {
|
|
336
306
|
const { options } = child;
|
|
337
307
|
const group = new SlashCommandSubcommandGroupBuilder().setName(options.name).setDescription(options.description);
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
308
|
+
this.buildSlashCommandSubcommands(group, child, [
|
|
309
|
+
...inheritedArgs,
|
|
310
|
+
...this.getMainHookArguments(child)
|
|
311
|
+
]);
|
|
312
|
+
parent.addSubcommandGroup(group);
|
|
313
|
+
} else if (child instanceof SubcommandEntry) {
|
|
314
|
+
const { options } = child;
|
|
315
|
+
const subcommand = new SlashCommandSubcommandBuilder().setName(options.name).setDescription(options.description);
|
|
316
|
+
this.buildSlashCommandOptions(subcommand, [
|
|
317
|
+
...inheritedArgs,
|
|
318
|
+
...this.getMainHookArguments(child)
|
|
319
|
+
]);
|
|
320
|
+
parent.addSubcommand(subcommand);
|
|
342
321
|
}
|
|
343
322
|
}
|
|
344
323
|
}
|
|
345
|
-
static
|
|
346
|
-
const {
|
|
347
|
-
const
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
args.push(...Arg.getMethodArguments(hook.method));
|
|
351
|
-
}
|
|
352
|
-
for (const arg of args) {
|
|
353
|
-
this.buildSlashCommandOption(subcommand, arg);
|
|
324
|
+
static buildSlashCommandOptions(builder, args) {
|
|
325
|
+
const argGroup = Object.groupBy(args, ({ required }) => required ? "required" : "optional");
|
|
326
|
+
const orderedArgs = [...argGroup.required || [], ...argGroup.optional || []];
|
|
327
|
+
for (const arg of orderedArgs) {
|
|
328
|
+
this.attachSlashCommandOption(builder, arg);
|
|
354
329
|
}
|
|
355
|
-
parent.addSubcommand(subcommand);
|
|
356
330
|
}
|
|
357
|
-
static
|
|
331
|
+
static attachSlashCommandOption(builder, arg) {
|
|
358
332
|
const setupOption = (option) => {
|
|
359
333
|
return option.setName(arg.name).setDescription(arg.description || arg.name).setRequired(Boolean(arg.required));
|
|
360
334
|
};
|
|
361
335
|
switch (arg.type) {
|
|
362
336
|
case "string" /* String */: {
|
|
363
|
-
builder.addStringOption((
|
|
337
|
+
builder.addStringOption((data) => {
|
|
338
|
+
const option = setupOption(data);
|
|
339
|
+
if (arg.maxLength) {
|
|
340
|
+
option.setMaxLength(arg.maxLength);
|
|
341
|
+
}
|
|
342
|
+
if (arg.minLength) {
|
|
343
|
+
option.setMinLength(arg.minLength);
|
|
344
|
+
}
|
|
345
|
+
return option;
|
|
346
|
+
});
|
|
364
347
|
break;
|
|
365
348
|
}
|
|
366
349
|
case "integer" /* Integer */: {
|
|
367
|
-
builder.addIntegerOption((
|
|
350
|
+
builder.addIntegerOption((data) => {
|
|
351
|
+
const option = setupOption(data);
|
|
352
|
+
if (arg.maxValue) {
|
|
353
|
+
option.setMaxValue(arg.maxValue);
|
|
354
|
+
}
|
|
355
|
+
if (arg.minValue) {
|
|
356
|
+
option.setMinValue(arg.minValue);
|
|
357
|
+
}
|
|
358
|
+
return option;
|
|
359
|
+
});
|
|
368
360
|
break;
|
|
369
361
|
}
|
|
370
362
|
case "number" /* Number */: {
|
|
371
|
-
builder.addNumberOption((
|
|
363
|
+
builder.addNumberOption((data) => {
|
|
364
|
+
const option = setupOption(data);
|
|
365
|
+
if (arg.maxValue) {
|
|
366
|
+
option.setMaxValue(arg.maxValue);
|
|
367
|
+
}
|
|
368
|
+
if (arg.minValue) {
|
|
369
|
+
option.setMinValue(arg.minValue);
|
|
370
|
+
}
|
|
371
|
+
return option;
|
|
372
|
+
});
|
|
372
373
|
break;
|
|
373
374
|
}
|
|
374
375
|
case "user" /* User */:
|
|
@@ -465,11 +466,10 @@ export {
|
|
|
465
466
|
BaseContext,
|
|
466
467
|
ChatInputContext,
|
|
467
468
|
Command,
|
|
469
|
+
CommandAPI,
|
|
468
470
|
CommandFactory,
|
|
469
471
|
CommandGroupEntry,
|
|
470
|
-
CommandHookExecutionState,
|
|
471
472
|
CommandRegistry,
|
|
472
|
-
HOOKS_KEY,
|
|
473
473
|
MessageContext,
|
|
474
474
|
RootCommandEntry,
|
|
475
475
|
SubcommandEntry
|