bakit 2.0.0-alpha.4 → 2.0.0-alpha.6
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/bin/bakit.js +43 -0
- package/dist/index.d.ts +55 -15
- package/dist/index.js +241 -151
- package/package.json +8 -4
package/bin/bakit.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { config as useEnv } from "dotenv";
|
|
3
|
+
import { program } from "commander";
|
|
4
|
+
|
|
5
|
+
program.name("bakit");
|
|
6
|
+
|
|
7
|
+
program.command("dev").action(async () => {
|
|
8
|
+
useEnv({
|
|
9
|
+
path: [".env.local", ".env"],
|
|
10
|
+
quiet: true,
|
|
11
|
+
});
|
|
12
|
+
const { default: nodemon } = await import("nodemon");
|
|
13
|
+
|
|
14
|
+
nodemon({
|
|
15
|
+
script: "src/index.ts",
|
|
16
|
+
exec: `${process.execPath} --import tsx`,
|
|
17
|
+
ext: "ts,js",
|
|
18
|
+
watch: ["src"],
|
|
19
|
+
env: {
|
|
20
|
+
...process.env,
|
|
21
|
+
FORCE_COLOR: "1",
|
|
22
|
+
NODE_ENV: "development",
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
nodemon.on("start", () => {
|
|
27
|
+
console.log("Starting bakit app...");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
nodemon.on("restart", () => {
|
|
31
|
+
console.log("Bakit detected changes! Restarting...");
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
nodemon.on("exit", () => {
|
|
35
|
+
console.log("Proccess exited! Did you start bakit correctly?");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
nodemon.on("quit", () => {
|
|
39
|
+
process.exit();
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
program.parse();
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import * as discord_js from 'discord.js';
|
|
2
|
-
import { GatewayIntentBits, ClientOptions, ChatInputCommandInteraction, CacheType, Message, User, MessageCreateOptions, InteractionReplyOptions, Awaitable, Collection,
|
|
2
|
+
import { GatewayIntentBits, ClientOptions, ChatInputCommandInteraction, CacheType, Message, User, MessageCreateOptions, InteractionReplyOptions, Awaitable, Collection, Events, IntentsBitField, ClientEvents, Client } from 'discord.js';
|
|
3
3
|
import z$1, { z } from 'zod';
|
|
4
4
|
import { inspect } from 'node:util';
|
|
5
|
+
import * as jiti from 'jiti';
|
|
5
6
|
|
|
6
7
|
declare const ProjectConfigSchema: z.ZodObject<{
|
|
7
8
|
intents: z.ZodDefault<z.ZodUnion<readonly [z.ZodLiteral<"auto">, z.ZodBigInt, z.ZodArray<z.ZodEnum<typeof GatewayIntentBits>>]>>;
|
|
@@ -102,20 +103,48 @@ declare const BaseParamSchema: z.ZodObject<{
|
|
|
102
103
|
description: z.ZodOptional<z.ZodString>;
|
|
103
104
|
required: z.ZodDefault<z.ZodBoolean>;
|
|
104
105
|
}, z.core.$strip>;
|
|
105
|
-
declare const StringParamSchema: z.ZodObject<{
|
|
106
|
+
declare const StringParamSchema: z.ZodPipe<z.ZodObject<{
|
|
106
107
|
name: z.ZodString;
|
|
107
108
|
description: z.ZodOptional<z.ZodString>;
|
|
108
109
|
required: z.ZodDefault<z.ZodBoolean>;
|
|
109
110
|
maxLength: z.ZodOptional<z.ZodNumber>;
|
|
110
111
|
minLength: z.ZodOptional<z.ZodNumber>;
|
|
111
|
-
}, z.core.$strip
|
|
112
|
-
|
|
112
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
113
|
+
name: string;
|
|
114
|
+
required: boolean;
|
|
115
|
+
description?: string | undefined;
|
|
116
|
+
maxLength?: number | undefined;
|
|
117
|
+
minLength?: number | undefined;
|
|
118
|
+
} & {
|
|
119
|
+
description: string;
|
|
120
|
+
}, {
|
|
121
|
+
name: string;
|
|
122
|
+
required: boolean;
|
|
123
|
+
description?: string | undefined;
|
|
124
|
+
maxLength?: number | undefined;
|
|
125
|
+
minLength?: number | undefined;
|
|
126
|
+
}>>;
|
|
127
|
+
declare const NumberParamSchema: z.ZodPipe<z.ZodObject<{
|
|
113
128
|
name: z.ZodString;
|
|
114
129
|
description: z.ZodOptional<z.ZodString>;
|
|
115
130
|
required: z.ZodDefault<z.ZodBoolean>;
|
|
116
131
|
maxValue: z.ZodOptional<z.ZodNumber>;
|
|
117
132
|
minValue: z.ZodOptional<z.ZodNumber>;
|
|
118
|
-
}, z.core.$strip
|
|
133
|
+
}, z.core.$strip>, z.ZodTransform<{
|
|
134
|
+
name: string;
|
|
135
|
+
required: boolean;
|
|
136
|
+
description?: string | undefined;
|
|
137
|
+
maxValue?: number | undefined;
|
|
138
|
+
minValue?: number | undefined;
|
|
139
|
+
} & {
|
|
140
|
+
description: string;
|
|
141
|
+
}, {
|
|
142
|
+
name: string;
|
|
143
|
+
required: boolean;
|
|
144
|
+
description?: string | undefined;
|
|
145
|
+
maxValue?: number | undefined;
|
|
146
|
+
minValue?: number | undefined;
|
|
147
|
+
}>>;
|
|
119
148
|
type BaseParamOptions = z.input<typeof BaseParamSchema>;
|
|
120
149
|
type StringOptions = z.input<typeof StringParamSchema>;
|
|
121
150
|
type NumberOptions = z.input<typeof NumberParamSchema>;
|
|
@@ -192,10 +221,12 @@ type InferParamTuple<T extends readonly BaseParam<any, any, any>[]> = {
|
|
|
192
221
|
[K in keyof T]: T[K] extends AnyParam<any> ? InferParamValue<T[K]> : never;
|
|
193
222
|
};
|
|
194
223
|
|
|
224
|
+
declare function validateParamsOrder(params: readonly AnyParam<boolean>[]): boolean;
|
|
195
225
|
declare const CommandOptionsSchema: z.ZodPipe<z.ZodObject<{
|
|
196
|
-
name: z.ZodString
|
|
197
|
-
description: z.ZodOptional<z.ZodString
|
|
198
|
-
|
|
226
|
+
name: z.ZodReadonly<z.ZodString>;
|
|
227
|
+
description: z.ZodReadonly<z.ZodOptional<z.ZodString>>;
|
|
228
|
+
nsfw: z.ZodReadonly<z.ZodDefault<z.ZodBoolean>>;
|
|
229
|
+
params: z.ZodReadonly<z.ZodDefault<z.ZodArray<z.ZodCustom<BaseParam<{
|
|
199
230
|
name: string;
|
|
200
231
|
description?: string | undefined;
|
|
201
232
|
required?: boolean | undefined;
|
|
@@ -203,12 +234,13 @@ declare const CommandOptionsSchema: z.ZodPipe<z.ZodObject<{
|
|
|
203
234
|
name: string;
|
|
204
235
|
description?: string | undefined;
|
|
205
236
|
required?: boolean | undefined;
|
|
206
|
-
}, unknown, boolean
|
|
207
|
-
quotes: z.ZodDefault<z.ZodBoolean
|
|
237
|
+
}, unknown, boolean>>>>>;
|
|
238
|
+
quotes: z.ZodReadonly<z.ZodDefault<z.ZodBoolean>>;
|
|
208
239
|
}, z.core.$strip>, z.ZodTransform<{
|
|
209
240
|
description: string;
|
|
210
241
|
name: string;
|
|
211
|
-
|
|
242
|
+
nsfw: boolean;
|
|
243
|
+
params: readonly BaseParam<{
|
|
212
244
|
name: string;
|
|
213
245
|
description?: string | undefined;
|
|
214
246
|
required?: boolean | undefined;
|
|
@@ -216,7 +248,8 @@ declare const CommandOptionsSchema: z.ZodPipe<z.ZodObject<{
|
|
|
216
248
|
quotes: boolean;
|
|
217
249
|
}, {
|
|
218
250
|
name: string;
|
|
219
|
-
|
|
251
|
+
nsfw: boolean;
|
|
252
|
+
params: readonly BaseParam<{
|
|
220
253
|
name: string;
|
|
221
254
|
description?: string | undefined;
|
|
222
255
|
required?: boolean | undefined;
|
|
@@ -237,6 +270,9 @@ declare class Command<ParamsList extends readonly AnyParam<any>[] = any[]> exten
|
|
|
237
270
|
params?: ParamsList;
|
|
238
271
|
}) | string);
|
|
239
272
|
private handleSyntaxError;
|
|
273
|
+
toSlashCommandJSON(): discord_js.RESTPostAPIChatInputApplicationCommandsJSONBody;
|
|
274
|
+
private initSlashCommandOptions;
|
|
275
|
+
private initSlashCommandOption;
|
|
240
276
|
}
|
|
241
277
|
/**
|
|
242
278
|
* Define command entry, usually for modules.
|
|
@@ -282,8 +318,10 @@ declare const ListenerOptionsSchema: z$1.ZodObject<{
|
|
|
282
318
|
type ListenerOptions<K extends EventKey = EventKey> = Omit<z$1.input<typeof ListenerOptionsSchema>, "name"> & {
|
|
283
319
|
name: K;
|
|
284
320
|
};
|
|
285
|
-
type EventKey = keyof
|
|
286
|
-
declare class Listener<K extends EventKey = EventKey> extends LifecycleManager<Context, [
|
|
321
|
+
type EventKey = keyof BakitClientEvents;
|
|
322
|
+
declare class Listener<K extends EventKey = EventKey> extends LifecycleManager<Context, [
|
|
323
|
+
...args: BakitClientEvents[K]
|
|
324
|
+
]> {
|
|
287
325
|
options: ListenerOptions<K>;
|
|
288
326
|
constructor(options: K | ListenerOptions<K>);
|
|
289
327
|
}
|
|
@@ -339,6 +377,8 @@ declare const EVENT_INTENT_MAPPING: Record<string, number[]>;
|
|
|
339
377
|
|
|
340
378
|
declare function tokenize(content: string): string[];
|
|
341
379
|
|
|
380
|
+
declare const $jiti: jiti.Jiti;
|
|
381
|
+
|
|
342
382
|
declare class BakitError extends Error {
|
|
343
383
|
constructor(message: string);
|
|
344
384
|
}
|
|
@@ -366,4 +406,4 @@ declare const Params: {
|
|
|
366
406
|
}) => NumberParam<Required>;
|
|
367
407
|
};
|
|
368
408
|
|
|
369
|
-
export { type AnyParam, ArgumentError, BakitClient, type BakitClientEvents, BakitError, BaseClientManager, BaseCommandContext, BaseParam, type BaseParamOptions, BaseParamSchema, ChatInputContext, type ChatInputContextSendOptions, Command, type CommandContext, CommandManager, type CommandOptions, type CommandOptionsInput, CommandOptionsSchema, Context, type ContextSendOptions, EVENT_INTENT_MAPPING, type ErrorHookCallback, type GetPrefixFunction, HookOrder, HookState, type InferParamTuple, type InferParamValue, Instance, LifecycleManager, Listener, ListenerManager, type ListenerOptions, ListenerOptionsSchema, type MainHookCallback, MessageContext, type MessageContextSendOptions, type NumberOptions, NumberParam, NumberParamSchema, type ParamResolvedOutputType, ParamUserType, Params, type ProjectConfig, type ProjectConfigInput, ProjectConfigSchema, type StringOptions, StringParam, StringParamSchema, defineCommand, defineConfig, defineListener, getConfig, loadConfig, tokenize, useApp };
|
|
409
|
+
export { $jiti, type AnyParam, ArgumentError, BakitClient, type BakitClientEvents, BakitError, BaseClientManager, BaseCommandContext, BaseParam, type BaseParamOptions, BaseParamSchema, ChatInputContext, type ChatInputContextSendOptions, Command, type CommandContext, CommandManager, type CommandOptions, type CommandOptionsInput, CommandOptionsSchema, Context, type ContextSendOptions, EVENT_INTENT_MAPPING, type ErrorHookCallback, type GetPrefixFunction, HookOrder, HookState, type InferParamTuple, type InferParamValue, Instance, LifecycleManager, Listener, ListenerManager, type ListenerOptions, ListenerOptionsSchema, type MainHookCallback, MessageContext, type MessageContextSendOptions, type NumberOptions, NumberParam, NumberParamSchema, type ParamResolvedOutputType, ParamUserType, Params, type ProjectConfig, type ProjectConfigInput, ProjectConfigSchema, type StringOptions, StringParam, StringParamSchema, defineCommand, defineConfig, defineListener, getConfig, loadConfig, tokenize, useApp, validateParamsOrder };
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,130 @@
|
|
|
1
|
-
import { GatewayIntentBits, Events, Client, IntentsBitField, Collection, ChatInputCommandInteraction, Message } from 'discord.js';
|
|
1
|
+
import { GatewayIntentBits, Events, Client, IntentsBitField, Collection, SlashCommandBuilder, SlashCommandStringOption, SlashCommandNumberOption, ChatInputCommandInteraction, Message } from 'discord.js';
|
|
2
2
|
import z3, { z } from 'zod';
|
|
3
|
-
import { pathToFileURL } from 'url';
|
|
4
3
|
import glob from 'tiny-glob';
|
|
4
|
+
import { createJiti } from 'jiti';
|
|
5
5
|
import { inspect } from 'util';
|
|
6
6
|
import { posix } from 'path';
|
|
7
7
|
|
|
8
|
+
// src/config.ts
|
|
9
|
+
var INTENT_GROUPS = {
|
|
10
|
+
[GatewayIntentBits.Guilds]: [
|
|
11
|
+
Events.GuildCreate,
|
|
12
|
+
Events.GuildDelete,
|
|
13
|
+
Events.GuildUpdate,
|
|
14
|
+
Events.GuildUnavailable,
|
|
15
|
+
Events.GuildRoleCreate,
|
|
16
|
+
Events.GuildRoleDelete,
|
|
17
|
+
Events.GuildRoleUpdate,
|
|
18
|
+
Events.ChannelCreate,
|
|
19
|
+
Events.ChannelDelete,
|
|
20
|
+
Events.ChannelUpdate,
|
|
21
|
+
Events.ChannelPinsUpdate,
|
|
22
|
+
Events.ThreadCreate,
|
|
23
|
+
Events.ThreadDelete,
|
|
24
|
+
Events.ThreadUpdate,
|
|
25
|
+
Events.ThreadListSync,
|
|
26
|
+
Events.ThreadMemberUpdate,
|
|
27
|
+
Events.ThreadMembersUpdate,
|
|
28
|
+
Events.StageInstanceCreate,
|
|
29
|
+
Events.StageInstanceUpdate,
|
|
30
|
+
Events.StageInstanceDelete
|
|
31
|
+
],
|
|
32
|
+
[GatewayIntentBits.GuildMembers]: [
|
|
33
|
+
Events.GuildMemberAdd,
|
|
34
|
+
Events.GuildMemberUpdate,
|
|
35
|
+
Events.GuildMemberRemove,
|
|
36
|
+
Events.ThreadMembersUpdate
|
|
37
|
+
],
|
|
38
|
+
[GatewayIntentBits.GuildModeration]: [Events.GuildAuditLogEntryCreate, Events.GuildBanAdd, Events.GuildBanRemove],
|
|
39
|
+
[GatewayIntentBits.GuildExpressions]: [
|
|
40
|
+
Events.GuildEmojiCreate,
|
|
41
|
+
Events.GuildEmojiDelete,
|
|
42
|
+
Events.GuildEmojiUpdate,
|
|
43
|
+
Events.GuildStickerCreate,
|
|
44
|
+
Events.GuildStickerDelete,
|
|
45
|
+
Events.GuildStickerUpdate,
|
|
46
|
+
Events.GuildSoundboardSoundCreate,
|
|
47
|
+
Events.GuildSoundboardSoundUpdate,
|
|
48
|
+
Events.GuildSoundboardSoundDelete,
|
|
49
|
+
Events.GuildSoundboardSoundsUpdate
|
|
50
|
+
],
|
|
51
|
+
[GatewayIntentBits.GuildIntegrations]: [Events.GuildIntegrationsUpdate],
|
|
52
|
+
[GatewayIntentBits.GuildWebhooks]: [Events.WebhooksUpdate],
|
|
53
|
+
[GatewayIntentBits.GuildInvites]: [Events.InviteCreate, Events.InviteDelete],
|
|
54
|
+
[GatewayIntentBits.GuildVoiceStates]: [Events.VoiceStateUpdate],
|
|
55
|
+
[GatewayIntentBits.GuildPresences]: [Events.PresenceUpdate],
|
|
56
|
+
[GatewayIntentBits.GuildMessages]: [
|
|
57
|
+
Events.MessageCreate,
|
|
58
|
+
Events.MessageUpdate,
|
|
59
|
+
Events.MessageDelete,
|
|
60
|
+
Events.MessageBulkDelete
|
|
61
|
+
],
|
|
62
|
+
[GatewayIntentBits.GuildMessageReactions]: [
|
|
63
|
+
Events.MessageReactionAdd,
|
|
64
|
+
Events.MessageReactionRemove,
|
|
65
|
+
Events.MessageReactionRemoveAll,
|
|
66
|
+
Events.MessageReactionRemoveEmoji
|
|
67
|
+
],
|
|
68
|
+
[GatewayIntentBits.GuildMessageTyping]: [Events.TypingStart],
|
|
69
|
+
[GatewayIntentBits.DirectMessages]: [
|
|
70
|
+
Events.MessageCreate,
|
|
71
|
+
Events.MessageUpdate,
|
|
72
|
+
Events.MessageDelete,
|
|
73
|
+
Events.ChannelPinsUpdate
|
|
74
|
+
],
|
|
75
|
+
[GatewayIntentBits.DirectMessageReactions]: [
|
|
76
|
+
Events.MessageReactionAdd,
|
|
77
|
+
Events.MessageReactionRemove,
|
|
78
|
+
Events.MessageReactionRemoveAll,
|
|
79
|
+
Events.MessageReactionRemoveEmoji
|
|
80
|
+
],
|
|
81
|
+
[GatewayIntentBits.DirectMessageTyping]: [Events.TypingStart],
|
|
82
|
+
[GatewayIntentBits.MessageContent]: [Events.MessageCreate, Events.MessageUpdate],
|
|
83
|
+
[GatewayIntentBits.GuildScheduledEvents]: [
|
|
84
|
+
Events.GuildScheduledEventCreate,
|
|
85
|
+
Events.GuildScheduledEventDelete,
|
|
86
|
+
Events.GuildScheduledEventUpdate,
|
|
87
|
+
Events.GuildScheduledEventUserAdd,
|
|
88
|
+
Events.GuildScheduledEventUserRemove
|
|
89
|
+
],
|
|
90
|
+
[GatewayIntentBits.AutoModerationConfiguration]: [
|
|
91
|
+
Events.AutoModerationRuleCreate,
|
|
92
|
+
Events.AutoModerationRuleDelete,
|
|
93
|
+
Events.AutoModerationRuleUpdate
|
|
94
|
+
],
|
|
95
|
+
[GatewayIntentBits.AutoModerationExecution]: [Events.AutoModerationActionExecution],
|
|
96
|
+
[GatewayIntentBits.GuildMessagePolls]: [Events.MessagePollVoteAdd, Events.MessagePollVoteRemove],
|
|
97
|
+
[GatewayIntentBits.DirectMessagePolls]: [Events.MessagePollVoteAdd, Events.MessagePollVoteRemove]
|
|
98
|
+
}, EVENT_INTENT_MAPPING = {};
|
|
99
|
+
for (let [intentStr, events] of Object.entries(INTENT_GROUPS)) {
|
|
100
|
+
let intent = Number(intentStr);
|
|
101
|
+
for (let event of events)
|
|
102
|
+
EVENT_INTENT_MAPPING[event] ??= [], EVENT_INTENT_MAPPING[event].includes(intent) || EVENT_INTENT_MAPPING[event].push(intent);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/utils/string.ts
|
|
106
|
+
function tokenize(content) {
|
|
107
|
+
let args = [], current = "", quoteChar = null, isEscaped = false;
|
|
108
|
+
for (let i = 0; i < content.length; i++) {
|
|
109
|
+
let char = content[i];
|
|
110
|
+
if (char === void 0)
|
|
111
|
+
break;
|
|
112
|
+
if (isEscaped) {
|
|
113
|
+
current += char, isEscaped = false;
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
if (char === "\\") {
|
|
117
|
+
isEscaped = true;
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
quoteChar ? char === quoteChar ? quoteChar = null : current += char : char === '"' ? quoteChar = char : /\s/.test(char) ? current.length > 0 && (args.push(current), current = "") : current += char;
|
|
121
|
+
}
|
|
122
|
+
return current.length > 0 && args.push(current), args;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// src/utils/index.ts
|
|
126
|
+
var $jiti = createJiti(import.meta.url);
|
|
127
|
+
|
|
8
128
|
// src/config.ts
|
|
9
129
|
var ProjectConfigSchema = z.object({
|
|
10
130
|
/**
|
|
@@ -49,7 +169,7 @@ async function loadConfig(cwd = process.cwd()) {
|
|
|
49
169
|
if (!configPath)
|
|
50
170
|
throw new Error("Missing config file");
|
|
51
171
|
other && console.warn(`Multiple config files found in ${cwd}. Using ${configPath}.`);
|
|
52
|
-
let
|
|
172
|
+
let config = await $jiti.import(configPath, { default: true });
|
|
53
173
|
return _config = Object.freeze(await ProjectConfigSchema.parseAsync(config)), _config;
|
|
54
174
|
}
|
|
55
175
|
function getConfig() {
|
|
@@ -329,17 +449,29 @@ var BaseParam = class {
|
|
|
329
449
|
return this.setOption("maxValue", value);
|
|
330
450
|
}
|
|
331
451
|
};
|
|
332
|
-
|
|
333
|
-
|
|
452
|
+
function validateParamsOrder(params) {
|
|
453
|
+
let seenOptional = false;
|
|
454
|
+
for (let param of params)
|
|
455
|
+
if (param.options.required) {
|
|
456
|
+
if (seenOptional)
|
|
457
|
+
return false;
|
|
458
|
+
} else
|
|
459
|
+
seenOptional = true;
|
|
460
|
+
return true;
|
|
461
|
+
}
|
|
334
462
|
var CommandOptionsSchema = z.object({
|
|
335
|
-
name: z.string(),
|
|
336
|
-
description: z.string().min(1).max(100).optional(),
|
|
337
|
-
|
|
338
|
-
|
|
463
|
+
name: z.string().readonly(),
|
|
464
|
+
description: z.string().min(1).max(100).optional().readonly(),
|
|
465
|
+
nsfw: z.boolean().default(false).readonly(),
|
|
466
|
+
params: z.array(z.instanceof(BaseParam)).default([]).readonly(),
|
|
467
|
+
quotes: z.boolean().default(true).readonly()
|
|
339
468
|
}).transform((data) => ({
|
|
340
469
|
...data,
|
|
341
470
|
description: data.description ?? `Command ${data.name}`
|
|
342
|
-
}))
|
|
471
|
+
})).refine(({ params }) => validateParamsOrder(params), {
|
|
472
|
+
path: ["params"],
|
|
473
|
+
error: "Required params must be placed before optional params"
|
|
474
|
+
}), Command = class extends LifecycleManager {
|
|
343
475
|
constructor(options) {
|
|
344
476
|
let _options = CommandOptionsSchema.parse(typeof options == "string" ? { name: options } : options);
|
|
345
477
|
super(`command:${_options.name}`), this.options = _options, this.setHook("syntaxError", "ERROR" /* Error */, async (ctx, error, ...args) => {
|
|
@@ -349,6 +481,30 @@ var CommandOptionsSchema = z.object({
|
|
|
349
481
|
async handleSyntaxError(context, error, _args) {
|
|
350
482
|
error instanceof BakitError && await context.send(error.message);
|
|
351
483
|
}
|
|
484
|
+
toSlashCommandJSON() {
|
|
485
|
+
let { name, description, nsfw, params } = this.options, builder = new SlashCommandBuilder().setName(name).setDescription(description).setNSFW(nsfw);
|
|
486
|
+
return this.initSlashCommandOptions(builder, params), builder.toJSON();
|
|
487
|
+
}
|
|
488
|
+
initSlashCommandOptions(builder, params) {
|
|
489
|
+
for (let param of params)
|
|
490
|
+
this.initSlashCommandOption(builder, param);
|
|
491
|
+
}
|
|
492
|
+
initSlashCommandOption(builder, param) {
|
|
493
|
+
let initOption = (builder2) => {
|
|
494
|
+
let { name, description, required } = param.options;
|
|
495
|
+
return builder2.setName(name).setDescription(description).setRequired(required);
|
|
496
|
+
};
|
|
497
|
+
if (param instanceof StringParam) {
|
|
498
|
+
let { maxLength, minLength } = param.options, option = initOption(new SlashCommandStringOption());
|
|
499
|
+
maxLength && option.setMaxLength(maxLength), minLength && option.setMinLength(minLength), builder.addStringOption(option);
|
|
500
|
+
return;
|
|
501
|
+
}
|
|
502
|
+
if (param instanceof NumberParam) {
|
|
503
|
+
let { maxValue, minValue } = param.options, option = initOption(new SlashCommandNumberOption());
|
|
504
|
+
maxValue && option.setMaxValue(maxValue), minValue && option.setMinValue(minValue), builder.addNumberOption(option);
|
|
505
|
+
return;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
352
508
|
};
|
|
353
509
|
function defineCommand(options) {
|
|
354
510
|
return new Command(options);
|
|
@@ -369,7 +525,7 @@ var CommandManager = class extends BaseClientManager {
|
|
|
369
525
|
cwd: process.cwd()
|
|
370
526
|
})).map(async (file) => {
|
|
371
527
|
try {
|
|
372
|
-
let
|
|
528
|
+
let command = await $jiti.import(file, { default: !0 });
|
|
373
529
|
if (!command) {
|
|
374
530
|
console.warn(`[Loader] File has no default export: ${file}`);
|
|
375
531
|
return;
|
|
@@ -417,103 +573,6 @@ var ListenerOptionsSchema = z3.object({
|
|
|
417
573
|
function defineListener(options) {
|
|
418
574
|
return new Listener(options);
|
|
419
575
|
}
|
|
420
|
-
var INTENT_GROUPS = {
|
|
421
|
-
[GatewayIntentBits.Guilds]: [
|
|
422
|
-
Events.GuildCreate,
|
|
423
|
-
Events.GuildDelete,
|
|
424
|
-
Events.GuildUpdate,
|
|
425
|
-
Events.GuildUnavailable,
|
|
426
|
-
Events.GuildRoleCreate,
|
|
427
|
-
Events.GuildRoleDelete,
|
|
428
|
-
Events.GuildRoleUpdate,
|
|
429
|
-
Events.ChannelCreate,
|
|
430
|
-
Events.ChannelDelete,
|
|
431
|
-
Events.ChannelUpdate,
|
|
432
|
-
Events.ChannelPinsUpdate,
|
|
433
|
-
Events.ThreadCreate,
|
|
434
|
-
Events.ThreadDelete,
|
|
435
|
-
Events.ThreadUpdate,
|
|
436
|
-
Events.ThreadListSync,
|
|
437
|
-
Events.ThreadMemberUpdate,
|
|
438
|
-
Events.ThreadMembersUpdate,
|
|
439
|
-
Events.StageInstanceCreate,
|
|
440
|
-
Events.StageInstanceUpdate,
|
|
441
|
-
Events.StageInstanceDelete
|
|
442
|
-
],
|
|
443
|
-
[GatewayIntentBits.GuildMembers]: [
|
|
444
|
-
Events.GuildMemberAdd,
|
|
445
|
-
Events.GuildMemberUpdate,
|
|
446
|
-
Events.GuildMemberRemove,
|
|
447
|
-
Events.ThreadMembersUpdate
|
|
448
|
-
],
|
|
449
|
-
[GatewayIntentBits.GuildModeration]: [Events.GuildAuditLogEntryCreate, Events.GuildBanAdd, Events.GuildBanRemove],
|
|
450
|
-
[GatewayIntentBits.GuildExpressions]: [
|
|
451
|
-
Events.GuildEmojiCreate,
|
|
452
|
-
Events.GuildEmojiDelete,
|
|
453
|
-
Events.GuildEmojiUpdate,
|
|
454
|
-
Events.GuildStickerCreate,
|
|
455
|
-
Events.GuildStickerDelete,
|
|
456
|
-
Events.GuildStickerUpdate,
|
|
457
|
-
Events.GuildSoundboardSoundCreate,
|
|
458
|
-
Events.GuildSoundboardSoundUpdate,
|
|
459
|
-
Events.GuildSoundboardSoundDelete,
|
|
460
|
-
Events.GuildSoundboardSoundsUpdate
|
|
461
|
-
],
|
|
462
|
-
[GatewayIntentBits.GuildIntegrations]: [Events.GuildIntegrationsUpdate],
|
|
463
|
-
[GatewayIntentBits.GuildWebhooks]: [Events.WebhooksUpdate],
|
|
464
|
-
[GatewayIntentBits.GuildInvites]: [Events.InviteCreate, Events.InviteDelete],
|
|
465
|
-
[GatewayIntentBits.GuildVoiceStates]: [Events.VoiceStateUpdate],
|
|
466
|
-
[GatewayIntentBits.GuildPresences]: [Events.PresenceUpdate],
|
|
467
|
-
[GatewayIntentBits.GuildMessages]: [
|
|
468
|
-
Events.MessageCreate,
|
|
469
|
-
Events.MessageUpdate,
|
|
470
|
-
Events.MessageDelete,
|
|
471
|
-
Events.MessageBulkDelete
|
|
472
|
-
],
|
|
473
|
-
[GatewayIntentBits.GuildMessageReactions]: [
|
|
474
|
-
Events.MessageReactionAdd,
|
|
475
|
-
Events.MessageReactionRemove,
|
|
476
|
-
Events.MessageReactionRemoveAll,
|
|
477
|
-
Events.MessageReactionRemoveEmoji
|
|
478
|
-
],
|
|
479
|
-
[GatewayIntentBits.GuildMessageTyping]: [Events.TypingStart],
|
|
480
|
-
[GatewayIntentBits.DirectMessages]: [
|
|
481
|
-
Events.MessageCreate,
|
|
482
|
-
Events.MessageUpdate,
|
|
483
|
-
Events.MessageDelete,
|
|
484
|
-
Events.ChannelPinsUpdate
|
|
485
|
-
],
|
|
486
|
-
[GatewayIntentBits.DirectMessageReactions]: [
|
|
487
|
-
Events.MessageReactionAdd,
|
|
488
|
-
Events.MessageReactionRemove,
|
|
489
|
-
Events.MessageReactionRemoveAll,
|
|
490
|
-
Events.MessageReactionRemoveEmoji
|
|
491
|
-
],
|
|
492
|
-
[GatewayIntentBits.DirectMessageTyping]: [Events.TypingStart],
|
|
493
|
-
[GatewayIntentBits.MessageContent]: [Events.MessageCreate, Events.MessageUpdate],
|
|
494
|
-
[GatewayIntentBits.GuildScheduledEvents]: [
|
|
495
|
-
Events.GuildScheduledEventCreate,
|
|
496
|
-
Events.GuildScheduledEventDelete,
|
|
497
|
-
Events.GuildScheduledEventUpdate,
|
|
498
|
-
Events.GuildScheduledEventUserAdd,
|
|
499
|
-
Events.GuildScheduledEventUserRemove
|
|
500
|
-
],
|
|
501
|
-
[GatewayIntentBits.AutoModerationConfiguration]: [
|
|
502
|
-
Events.AutoModerationRuleCreate,
|
|
503
|
-
Events.AutoModerationRuleDelete,
|
|
504
|
-
Events.AutoModerationRuleUpdate
|
|
505
|
-
],
|
|
506
|
-
[GatewayIntentBits.AutoModerationExecution]: [Events.AutoModerationActionExecution],
|
|
507
|
-
[GatewayIntentBits.GuildMessagePolls]: [Events.MessagePollVoteAdd, Events.MessagePollVoteRemove],
|
|
508
|
-
[GatewayIntentBits.DirectMessagePolls]: [Events.MessagePollVoteAdd, Events.MessagePollVoteRemove]
|
|
509
|
-
}, EVENT_INTENT_MAPPING = {};
|
|
510
|
-
for (let [intentStr, events] of Object.entries(INTENT_GROUPS)) {
|
|
511
|
-
let intent = Number(intentStr);
|
|
512
|
-
for (let event of events)
|
|
513
|
-
EVENT_INTENT_MAPPING[event] ??= [], EVENT_INTENT_MAPPING[event].includes(intent) || EVENT_INTENT_MAPPING[event].push(intent);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
// src/listener/ListenerManager.ts
|
|
517
576
|
var ListenerManager = class extends BaseClientManager {
|
|
518
577
|
listeners = [];
|
|
519
578
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -523,7 +582,7 @@ var ListenerManager = class extends BaseClientManager {
|
|
|
523
582
|
cwd: process.cwd()
|
|
524
583
|
})).map(async (file) => {
|
|
525
584
|
try {
|
|
526
|
-
let
|
|
585
|
+
let listener = await $jiti.import(file, { default: !0 });
|
|
527
586
|
if (!listener) {
|
|
528
587
|
console.warn(`[Loader] File has no default export: ${file}`);
|
|
529
588
|
return;
|
|
@@ -613,6 +672,77 @@ var BakitClient3 = class extends Client {
|
|
|
613
672
|
return `${this.constructor.name} {}`;
|
|
614
673
|
}
|
|
615
674
|
};
|
|
675
|
+
var ParamUserType = /* @__PURE__ */ ((ParamUserType2) => (ParamUserType2.Bot = "bot", ParamUserType2.Normal = "normal", ParamUserType2.Any = "any", ParamUserType2))(ParamUserType || {}), BaseParamSchema = z.object({
|
|
676
|
+
name: z.string(),
|
|
677
|
+
description: z.string().optional(),
|
|
678
|
+
required: z.boolean().default(true)
|
|
679
|
+
}), withDefaultDescription = (data) => ({
|
|
680
|
+
...data,
|
|
681
|
+
description: data.description ?? `${data.name}`
|
|
682
|
+
}), StringParamSchema = BaseParamSchema.extend({
|
|
683
|
+
maxLength: z.number().min(1).optional(),
|
|
684
|
+
minLength: z.number().min(1).optional()
|
|
685
|
+
}).transform(withDefaultDescription), NumberParamSchema = BaseParamSchema.extend({
|
|
686
|
+
maxValue: z.number().optional(),
|
|
687
|
+
minValue: z.number().optional()
|
|
688
|
+
}).transform(withDefaultDescription);
|
|
689
|
+
|
|
690
|
+
// src/command/param/Params.ts
|
|
691
|
+
function createFactory(ctor) {
|
|
692
|
+
return (...args) => new ctor(...args);
|
|
693
|
+
}
|
|
694
|
+
var Params = {
|
|
695
|
+
string: createFactory(StringParam),
|
|
696
|
+
number: createFactory(NumberParam)
|
|
697
|
+
};
|
|
698
|
+
|
|
699
|
+
// src/defaults/command.ts
|
|
700
|
+
var messageCommandHandler = defineListener(Events.MessageCreate), chatInputCommandHandler = defineListener(Events.InteractionCreate), registerCommandsHandler = defineListener({
|
|
701
|
+
name: Events.ClientReady,
|
|
702
|
+
once: true
|
|
703
|
+
});
|
|
704
|
+
registerCommandsHandler.main(async (_, client) => {
|
|
705
|
+
let { commands } = client.managers, data = commands.commands.map((cmd) => cmd.toSlashCommandJSON()), result = await client.application.commands.set(data);
|
|
706
|
+
console.log(`Registered ${result.size} application command(s)`);
|
|
707
|
+
});
|
|
708
|
+
messageCommandHandler.main(async (_, message) => {
|
|
709
|
+
let config = getConfig();
|
|
710
|
+
if (message.author.bot)
|
|
711
|
+
return;
|
|
712
|
+
let { content } = message, client = message.client, lowerContent = content.toLowerCase(), prefix = config.prefixes.find((p) => lowerContent.startsWith(p));
|
|
713
|
+
if (!prefix)
|
|
714
|
+
return;
|
|
715
|
+
let [name, ...args] = content.slice(prefix.length).trim().split(/\s+/g);
|
|
716
|
+
if (!name)
|
|
717
|
+
return;
|
|
718
|
+
let command = client.managers.commands.get(name);
|
|
719
|
+
if (!command)
|
|
720
|
+
return;
|
|
721
|
+
let context = new MessageContext(message), { params, quotes } = command.options, rawArgs = quotes ? tokenize(args.join(" ")) : args, resolvedArgs = [];
|
|
722
|
+
for (let i = 0; i < params.length; i++) {
|
|
723
|
+
let param = params[i], arg = rawArgs[i];
|
|
724
|
+
if (!param)
|
|
725
|
+
break;
|
|
726
|
+
let resolved = await param.resolve(context, arg);
|
|
727
|
+
resolvedArgs.push(resolved);
|
|
728
|
+
}
|
|
729
|
+
await command.execute(context, ...resolvedArgs);
|
|
730
|
+
});
|
|
731
|
+
chatInputCommandHandler.main(async (_, interaction) => {
|
|
732
|
+
if (!interaction.isChatInputCommand())
|
|
733
|
+
return;
|
|
734
|
+
let { commandName } = interaction, command = interaction.client.managers.commands.get(commandName);
|
|
735
|
+
if (!command)
|
|
736
|
+
return;
|
|
737
|
+
let context = new ChatInputContext(interaction), { params } = command.options, resolvedArgs = [];
|
|
738
|
+
for (let param of params) {
|
|
739
|
+
let resolved = await param.resolve(context);
|
|
740
|
+
resolvedArgs.push(resolved);
|
|
741
|
+
}
|
|
742
|
+
await command.execute(context, ...resolvedArgs);
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
// src/Instance.ts
|
|
616
746
|
var Instance = class {
|
|
617
747
|
client;
|
|
618
748
|
async start() {
|
|
@@ -624,7 +754,7 @@ var Instance = class {
|
|
|
624
754
|
}
|
|
625
755
|
loadModules() {
|
|
626
756
|
let { managers } = this.client, { commands, listeners } = managers;
|
|
627
|
-
return Promise.all([commands.loadModules(), listeners.loadModules()]);
|
|
757
|
+
return listeners.add(chatInputCommandHandler), listeners.add(messageCommandHandler), Promise.all([commands.loadModules(), listeners.loadModules()]);
|
|
628
758
|
}
|
|
629
759
|
initIntents() {
|
|
630
760
|
let config = getConfig(), { options, managers } = this.client, { listeners } = managers, intents;
|
|
@@ -635,44 +765,4 @@ function useApp() {
|
|
|
635
765
|
return new Instance();
|
|
636
766
|
}
|
|
637
767
|
|
|
638
|
-
|
|
639
|
-
function tokenize(content) {
|
|
640
|
-
let args = [], current = "", quoteChar = null, isEscaped = false;
|
|
641
|
-
for (let i = 0; i < content.length; i++) {
|
|
642
|
-
let char = content[i];
|
|
643
|
-
if (char === void 0)
|
|
644
|
-
break;
|
|
645
|
-
if (isEscaped) {
|
|
646
|
-
current += char, isEscaped = false;
|
|
647
|
-
continue;
|
|
648
|
-
}
|
|
649
|
-
if (char === "\\") {
|
|
650
|
-
isEscaped = true;
|
|
651
|
-
continue;
|
|
652
|
-
}
|
|
653
|
-
quoteChar ? char === quoteChar ? quoteChar = null : current += char : char === '"' ? quoteChar = char : /\s/.test(char) ? current.length > 0 && (args.push(current), current = "") : current += char;
|
|
654
|
-
}
|
|
655
|
-
return current.length > 0 && args.push(current), args;
|
|
656
|
-
}
|
|
657
|
-
var ParamUserType = /* @__PURE__ */ ((ParamUserType2) => (ParamUserType2.Bot = "bot", ParamUserType2.Normal = "normal", ParamUserType2.Any = "any", ParamUserType2))(ParamUserType || {}), BaseParamSchema = z.object({
|
|
658
|
-
name: z.string(),
|
|
659
|
-
description: z.string().optional(),
|
|
660
|
-
required: z.boolean().default(true)
|
|
661
|
-
}), StringParamSchema = BaseParamSchema.extend({
|
|
662
|
-
maxLength: z.number().min(1).optional(),
|
|
663
|
-
minLength: z.number().min(1).optional()
|
|
664
|
-
}), NumberParamSchema = BaseParamSchema.extend({
|
|
665
|
-
maxValue: z.number().optional(),
|
|
666
|
-
minValue: z.number().optional()
|
|
667
|
-
});
|
|
668
|
-
|
|
669
|
-
// src/command/param/Params.ts
|
|
670
|
-
function createFactory(ctor) {
|
|
671
|
-
return (...args) => new ctor(...args);
|
|
672
|
-
}
|
|
673
|
-
var Params = {
|
|
674
|
-
string: createFactory(StringParam),
|
|
675
|
-
number: createFactory(NumberParam)
|
|
676
|
-
};
|
|
677
|
-
|
|
678
|
-
export { ArgumentError, BakitClient3 as BakitClient, BakitError, BaseClientManager, BaseCommandContext, BaseParam, BaseParamSchema, ChatInputContext, Command, CommandManager, CommandOptionsSchema, Context, EVENT_INTENT_MAPPING, HookOrder, HookState, Instance, LifecycleManager, Listener, ListenerManager, ListenerOptionsSchema, MessageContext, NumberParam, NumberParamSchema, ParamUserType, Params, ProjectConfigSchema, StringParam, StringParamSchema, defineCommand, defineConfig, defineListener, getConfig, loadConfig, tokenize, useApp };
|
|
768
|
+
export { $jiti, ArgumentError, BakitClient3 as BakitClient, BakitError, BaseClientManager, BaseCommandContext, BaseParam, BaseParamSchema, ChatInputContext, Command, CommandManager, CommandOptionsSchema, Context, EVENT_INTENT_MAPPING, HookOrder, HookState, Instance, LifecycleManager, Listener, ListenerManager, ListenerOptionsSchema, MessageContext, NumberParam, NumberParamSchema, ParamUserType, Params, ProjectConfigSchema, StringParam, StringParamSchema, defineCommand, defineConfig, defineListener, getConfig, loadConfig, tokenize, useApp, validateParamsOrder };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "bakit",
|
|
3
|
-
"version": "2.0.0-alpha.
|
|
3
|
+
"version": "2.0.0-alpha.6",
|
|
4
4
|
"description": "A framework for discord.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -10,6 +10,9 @@
|
|
|
10
10
|
"type-check": "tsc --noEmit",
|
|
11
11
|
"test": "vitest run --pass-with-no-tests"
|
|
12
12
|
},
|
|
13
|
+
"bin": {
|
|
14
|
+
"bakit": "./bin/bakit.js"
|
|
15
|
+
},
|
|
13
16
|
"files": [
|
|
14
17
|
"dist"
|
|
15
18
|
],
|
|
@@ -34,11 +37,12 @@
|
|
|
34
37
|
"discord.js": "^14.0.0"
|
|
35
38
|
},
|
|
36
39
|
"dependencies": {
|
|
40
|
+
"commander": "^14.0.2",
|
|
41
|
+
"dotenv": "^17.2.1",
|
|
42
|
+
"jiti": "^2.6.1",
|
|
43
|
+
"nodemon": "^3.1.11",
|
|
37
44
|
"tiny-glob": "^0.2.9",
|
|
38
45
|
"type-fest": "^4.41.0",
|
|
39
46
|
"zod": "^4.1.12"
|
|
40
|
-
},
|
|
41
|
-
"devDependencies": {
|
|
42
|
-
"@swc/core": "^1.13.5"
|
|
43
47
|
}
|
|
44
48
|
}
|