bakit 1.0.0-beta.8 → 2.0.0-alpha.1
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/README.md +35 -0
- package/dist/index.d.ts +37 -89
- package/dist/index.js +47 -1195
- package/package.json +16 -9
- package/dist/BakitClient-D9kRvFS3.d.ts +0 -224
- package/dist/command/index.d.ts +0 -3
- package/dist/command/index.js +0 -473
package/dist/command/index.js
DELETED
|
@@ -1,473 +0,0 @@
|
|
|
1
|
-
// src/command/argument/Argument.ts
|
|
2
|
-
var ArgumentType = /* @__PURE__ */ ((ArgumentType2) => {
|
|
3
|
-
ArgumentType2["String"] = "string";
|
|
4
|
-
ArgumentType2["Integer"] = "integer";
|
|
5
|
-
ArgumentType2["Number"] = "number";
|
|
6
|
-
ArgumentType2["User"] = "user";
|
|
7
|
-
ArgumentType2["Member"] = "member";
|
|
8
|
-
return ArgumentType2;
|
|
9
|
-
})(ArgumentType || {});
|
|
10
|
-
|
|
11
|
-
// src/command/argument/Arg.ts
|
|
12
|
-
var ARGS_KEY = Symbol("args");
|
|
13
|
-
var cache = /* @__PURE__ */ new WeakMap();
|
|
14
|
-
function getMethodArguments(method, init = false) {
|
|
15
|
-
let args = cache.get(method) ?? Reflect.getMetadata(ARGS_KEY, method);
|
|
16
|
-
if (!args) {
|
|
17
|
-
args = [];
|
|
18
|
-
if (init) {
|
|
19
|
-
Reflect.defineMetadata(ARGS_KEY, args, method);
|
|
20
|
-
cache.set(method, args);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
return init ? args : Object.freeze([...args]);
|
|
24
|
-
}
|
|
25
|
-
function createArgument(type) {
|
|
26
|
-
return function(options) {
|
|
27
|
-
const objOptions = typeof options === "string" ? { name: options } : options;
|
|
28
|
-
const fullOptions = { ...objOptions, type };
|
|
29
|
-
if (!fullOptions.description) {
|
|
30
|
-
fullOptions.description = fullOptions.name;
|
|
31
|
-
}
|
|
32
|
-
if (!("required" in fullOptions)) {
|
|
33
|
-
fullOptions.required = true;
|
|
34
|
-
}
|
|
35
|
-
return function(target, key, _index) {
|
|
36
|
-
const method = Object.getOwnPropertyDescriptor(target, key)?.value;
|
|
37
|
-
if (!method) {
|
|
38
|
-
throw new Error("No method found");
|
|
39
|
-
}
|
|
40
|
-
const args = getMethodArguments(method, true);
|
|
41
|
-
args.unshift(fullOptions);
|
|
42
|
-
};
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
var string = createArgument("string" /* String */);
|
|
46
|
-
var integer = createArgument("integer" /* Integer */);
|
|
47
|
-
var number = createArgument("number" /* Number */);
|
|
48
|
-
var user = createArgument("user" /* User */);
|
|
49
|
-
var member = createArgument("member" /* Member */);
|
|
50
|
-
function describeArgumentExpectation(arg) {
|
|
51
|
-
const parts = [arg.type];
|
|
52
|
-
switch (arg.type) {
|
|
53
|
-
case "string" /* String */: {
|
|
54
|
-
if (arg.minLength && !arg.maxLength) {
|
|
55
|
-
parts.push(`\u2265 ${String(arg.minLength)}`);
|
|
56
|
-
}
|
|
57
|
-
if (!arg.minLength && arg.maxLength) {
|
|
58
|
-
parts.push(`\u2264 ${String(arg.maxLength)}`);
|
|
59
|
-
}
|
|
60
|
-
if (arg.minLength && arg.maxLength) {
|
|
61
|
-
parts.push(`${String(arg.minLength)} - ${String(arg.maxLength)}`);
|
|
62
|
-
}
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
case "number" /* Number */:
|
|
66
|
-
case "integer" /* Integer */: {
|
|
67
|
-
if (arg.minValue !== void 0 && arg.maxValue === void 0) {
|
|
68
|
-
parts.push(`\u2265 ${String(arg.minValue)}`);
|
|
69
|
-
}
|
|
70
|
-
if (arg.minValue === void 0 && arg.maxValue !== void 0) {
|
|
71
|
-
parts.push(`\u2264 ${String(arg.maxValue)}`);
|
|
72
|
-
}
|
|
73
|
-
if (arg.minValue !== void 0 && arg.maxValue !== void 0) {
|
|
74
|
-
parts.push(`${String(arg.minValue)} - ${String(arg.maxValue)}`);
|
|
75
|
-
}
|
|
76
|
-
break;
|
|
77
|
-
}
|
|
78
|
-
case "user" /* User */:
|
|
79
|
-
case "member" /* Member */: {
|
|
80
|
-
break;
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
return parts.join(", ");
|
|
84
|
-
}
|
|
85
|
-
function format(arg) {
|
|
86
|
-
const { name, required, tuple } = arg;
|
|
87
|
-
const opening = required ? "<" : "[";
|
|
88
|
-
const closing = required ? ">" : "]";
|
|
89
|
-
const prefix = tuple ? "..." : "";
|
|
90
|
-
return `${opening}${prefix}${name}: ${describeArgumentExpectation(arg)}${closing}`;
|
|
91
|
-
}
|
|
92
|
-
var Arg = {
|
|
93
|
-
getMethodArguments,
|
|
94
|
-
createArgument,
|
|
95
|
-
describeArgumentExpectation,
|
|
96
|
-
format,
|
|
97
|
-
string,
|
|
98
|
-
number,
|
|
99
|
-
integer,
|
|
100
|
-
user,
|
|
101
|
-
member
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
// src/command/CommandEntry.ts
|
|
105
|
-
import { Collection } from "discord.js";
|
|
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) {
|
|
130
|
-
return (target, _key, descriptor) => {
|
|
131
|
-
this.addHook(state, target, descriptor);
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
createErrorHookDecorator(state) {
|
|
135
|
-
return (target, _key, descriptor) => {
|
|
136
|
-
this.addHook(state, target, descriptor);
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
addHook(state, target, descriptor) {
|
|
140
|
-
if (target.constructor !== this.target) {
|
|
141
|
-
throw new Error("Hook is used at unknown constructor.");
|
|
142
|
-
}
|
|
143
|
-
const { value: method } = descriptor;
|
|
144
|
-
if (typeof method !== "function") {
|
|
145
|
-
throw new Error("Invalid target method for hook.");
|
|
146
|
-
}
|
|
147
|
-
const hook = {
|
|
148
|
-
state,
|
|
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;
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
var BaseCommandGroupEntry = class extends BaseCommandEntry {
|
|
164
|
-
children = new Collection();
|
|
165
|
-
subcommand(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.`);
|
|
169
|
-
}
|
|
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.`);
|
|
180
|
-
}
|
|
181
|
-
const group = new CommandGroupEntry(fullOptions, this);
|
|
182
|
-
this.children.set(fullOptions.name, group);
|
|
183
|
-
return group;
|
|
184
|
-
}
|
|
185
|
-
};
|
|
186
|
-
var CommandGroupEntry = class extends BaseCommandGroupEntry {
|
|
187
|
-
constructor(options, parent) {
|
|
188
|
-
super(options);
|
|
189
|
-
this.parent = parent;
|
|
190
|
-
}
|
|
191
|
-
};
|
|
192
|
-
var SubcommandEntry = class extends BaseCommandEntry {
|
|
193
|
-
constructor(options, parent) {
|
|
194
|
-
super(options);
|
|
195
|
-
this.parent = parent;
|
|
196
|
-
}
|
|
197
|
-
};
|
|
198
|
-
|
|
199
|
-
// src/command/Command.ts
|
|
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 = {}));
|
|
215
|
-
function CommandFactory(options) {
|
|
216
|
-
if (typeof options === "string") {
|
|
217
|
-
options = { name: options };
|
|
218
|
-
}
|
|
219
|
-
if (!options.description) {
|
|
220
|
-
options.description = options.name;
|
|
221
|
-
}
|
|
222
|
-
return new RootCommandEntry(options);
|
|
223
|
-
}
|
|
224
|
-
var Command = Object.assign(CommandFactory, CommandAPI);
|
|
225
|
-
|
|
226
|
-
// src/command/CommandRegistry.ts
|
|
227
|
-
import {
|
|
228
|
-
Collection as Collection2,
|
|
229
|
-
SlashCommandBuilder,
|
|
230
|
-
SlashCommandSubcommandBuilder,
|
|
231
|
-
SlashCommandSubcommandGroupBuilder
|
|
232
|
-
} from "discord.js";
|
|
233
|
-
import glob from "tiny-glob";
|
|
234
|
-
import { pathToFileURL } from "url";
|
|
235
|
-
var CommandRegistry = class _CommandRegistry {
|
|
236
|
-
static constructors = new Collection2();
|
|
237
|
-
static instances = new Collection2();
|
|
238
|
-
/**
|
|
239
|
-
* Add a command to the registry.
|
|
240
|
-
* @param constructor The command class you want to add.
|
|
241
|
-
*/
|
|
242
|
-
static add(constructor) {
|
|
243
|
-
const root = Command.getRoot(constructor);
|
|
244
|
-
if (!root) {
|
|
245
|
-
throw new Error(`No root found for "${constructor.name}"`);
|
|
246
|
-
}
|
|
247
|
-
const { options } = root;
|
|
248
|
-
this.constructors.set(options.name, constructor);
|
|
249
|
-
this.instances.set(options.name, new constructor());
|
|
250
|
-
}
|
|
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) {
|
|
258
|
-
const files = await glob(pattern);
|
|
259
|
-
const loaders = files.map(async (file) => {
|
|
260
|
-
const fileURL = pathToFileURL(file).toString();
|
|
261
|
-
const { default: constructor } = await import(fileURL);
|
|
262
|
-
_CommandRegistry.add(constructor);
|
|
263
|
-
return constructor;
|
|
264
|
-
});
|
|
265
|
-
if (parallel) {
|
|
266
|
-
return await Promise.all(loaders);
|
|
267
|
-
}
|
|
268
|
-
const result = [];
|
|
269
|
-
for (const loader of loaders) {
|
|
270
|
-
result.push(await loader);
|
|
271
|
-
}
|
|
272
|
-
return result;
|
|
273
|
-
}
|
|
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) {
|
|
280
|
-
const root = Command.getRoot(constructor);
|
|
281
|
-
if (!root) {
|
|
282
|
-
throw new Error(`No root found for "${constructor.name}"`);
|
|
283
|
-
}
|
|
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);
|
|
291
|
-
}
|
|
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) {
|
|
303
|
-
const { options } = child;
|
|
304
|
-
const group = new SlashCommandSubcommandGroupBuilder().setName(options.name).setDescription(options.description);
|
|
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);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
}
|
|
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);
|
|
326
|
-
}
|
|
327
|
-
}
|
|
328
|
-
static attachSlashCommandOption(builder, arg) {
|
|
329
|
-
const setupOption = (option) => {
|
|
330
|
-
return option.setName(arg.name).setDescription(arg.description || arg.name).setRequired(Boolean(arg.required));
|
|
331
|
-
};
|
|
332
|
-
switch (arg.type) {
|
|
333
|
-
case "string" /* String */: {
|
|
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
|
-
});
|
|
344
|
-
break;
|
|
345
|
-
}
|
|
346
|
-
case "integer" /* Integer */: {
|
|
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
|
-
});
|
|
357
|
-
break;
|
|
358
|
-
}
|
|
359
|
-
case "number" /* Number */: {
|
|
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
|
-
});
|
|
370
|
-
break;
|
|
371
|
-
}
|
|
372
|
-
case "user" /* User */:
|
|
373
|
-
case "member" /* Member */: {
|
|
374
|
-
builder.addUserOption((option) => setupOption(option));
|
|
375
|
-
break;
|
|
376
|
-
}
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
};
|
|
380
|
-
|
|
381
|
-
// src/command/Context.ts
|
|
382
|
-
import {
|
|
383
|
-
ChatInputCommandInteraction,
|
|
384
|
-
Message
|
|
385
|
-
} from "discord.js";
|
|
386
|
-
var BaseContext = class {
|
|
387
|
-
constructor(source) {
|
|
388
|
-
this.source = source;
|
|
389
|
-
}
|
|
390
|
-
get client() {
|
|
391
|
-
return this.source.client;
|
|
392
|
-
}
|
|
393
|
-
get channel() {
|
|
394
|
-
return this.source.channel;
|
|
395
|
-
}
|
|
396
|
-
get channelId() {
|
|
397
|
-
return this.source.channelId;
|
|
398
|
-
}
|
|
399
|
-
get guild() {
|
|
400
|
-
return this.source.guild;
|
|
401
|
-
}
|
|
402
|
-
get guildId() {
|
|
403
|
-
return this.source.guildId;
|
|
404
|
-
}
|
|
405
|
-
inGuild() {
|
|
406
|
-
return Boolean(this.guildId);
|
|
407
|
-
}
|
|
408
|
-
inCachedGuild() {
|
|
409
|
-
if (this.isChatInput()) {
|
|
410
|
-
return this.source.inCachedGuild();
|
|
411
|
-
} else if (this.isMessage()) {
|
|
412
|
-
return this.source.inGuild();
|
|
413
|
-
} else {
|
|
414
|
-
throw new Error("Invalid source");
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
get author() {
|
|
418
|
-
if (this.isChatInput()) {
|
|
419
|
-
return this.source.user;
|
|
420
|
-
} else if (this.isMessage()) {
|
|
421
|
-
return this.source.author;
|
|
422
|
-
} else {
|
|
423
|
-
throw new Error("Invalid source");
|
|
424
|
-
}
|
|
425
|
-
}
|
|
426
|
-
isChatInput() {
|
|
427
|
-
return this.source instanceof ChatInputCommandInteraction;
|
|
428
|
-
}
|
|
429
|
-
isMessage() {
|
|
430
|
-
return this.source instanceof Message;
|
|
431
|
-
}
|
|
432
|
-
};
|
|
433
|
-
var ChatInputContext = class extends BaseContext {
|
|
434
|
-
async send(options) {
|
|
435
|
-
if (typeof options === "string") {
|
|
436
|
-
options = { content: options };
|
|
437
|
-
}
|
|
438
|
-
const sendOptions = {
|
|
439
|
-
...options,
|
|
440
|
-
withResponse: true
|
|
441
|
-
};
|
|
442
|
-
if (this.source.deferred || this.source.replied) {
|
|
443
|
-
return await this.source.followUp(sendOptions);
|
|
444
|
-
}
|
|
445
|
-
const response = await this.source.reply(sendOptions);
|
|
446
|
-
return response.resource?.message;
|
|
447
|
-
}
|
|
448
|
-
};
|
|
449
|
-
var MessageContext = class extends BaseContext {
|
|
450
|
-
async send(options) {
|
|
451
|
-
const { channel } = this;
|
|
452
|
-
if (!channel?.isSendable()) {
|
|
453
|
-
throw new Error("Invalid channel or channel is not sendable");
|
|
454
|
-
}
|
|
455
|
-
return await channel.send(options);
|
|
456
|
-
}
|
|
457
|
-
};
|
|
458
|
-
export {
|
|
459
|
-
Arg,
|
|
460
|
-
ArgumentType,
|
|
461
|
-
BaseCommandEntry,
|
|
462
|
-
BaseCommandGroupEntry,
|
|
463
|
-
BaseContext,
|
|
464
|
-
ChatInputContext,
|
|
465
|
-
Command,
|
|
466
|
-
CommandAPI,
|
|
467
|
-
CommandFactory,
|
|
468
|
-
CommandGroupEntry,
|
|
469
|
-
CommandRegistry,
|
|
470
|
-
MessageContext,
|
|
471
|
-
RootCommandEntry,
|
|
472
|
-
SubcommandEntry
|
|
473
|
-
};
|