gralonium 0.4.0 → 0.4.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/lib/classes/Client.d.ts +6 -2
- package/lib/classes/Client.js +1 -1
- package/lib/classes/Context.d.ts +2 -0
- package/lib/classes/Context.js +26 -2
- package/lib/classes/Cooldowns.d.ts +2 -0
- package/lib/classes/Cooldowns.js +28 -0
- package/lib/classes/Loader.d.ts +8 -1
- package/lib/classes/Loader.js +101 -19
- package/lib/classes/Plugins.js +3 -2
- package/lib/classes/Utils.d.ts +1 -1
- package/lib/classes/Utils.js +73 -13
- package/lib/classes/builders/CommandBuilder.d.ts +14 -0
- package/lib/classes/builders/CommandBuilder.js +50 -2
- package/lib/classes/builders/ComponentsV2Builder.d.ts +3 -0
- package/lib/classes/builders/ComponentsV2Builder.js +3 -1
- package/lib/classes/builders/GroupBuilder.d.ts +11 -0
- package/lib/classes/builders/GroupBuilder.js +21 -1
- package/lib/classes/builders/InteractionBuilder.d.ts +46 -0
- package/lib/classes/builders/InteractionBuilder.js +112 -2
- package/lib/classes/builders/ParamsBuilder.d.ts +118 -76
- package/lib/classes/builders/ParamsBuilder.js +167 -1
- package/lib/classes/builders/components/StyleMapper.d.ts +13 -1
- package/lib/classes/builders/components/StyleMapper.js +33 -8
- package/package.json +1 -1
package/lib/classes/Client.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Client, ClientOptions, CommandInteraction, Message, Snowflake } from "discord.js";
|
|
2
2
|
import { Context } from "./Context.js";
|
|
3
|
-
import { Loader, Plugin } from "./Loader.js";
|
|
3
|
+
import { CommandLocalizer, Loader, Plugin } from "./Loader.js";
|
|
4
4
|
import { HelpCommand, HelpCommandOptions } from "./HelpCommand.js";
|
|
5
5
|
import { Cooldowns } from "./Cooldowns.js";
|
|
6
6
|
|
|
@@ -25,6 +25,10 @@ export interface GraloniumOptions extends ClientOptions {
|
|
|
25
25
|
debug?: boolean;
|
|
26
26
|
bindProcessHandlers?: boolean;
|
|
27
27
|
retryOnRateLimit?: boolean;
|
|
28
|
+
defaultLocale?: string;
|
|
29
|
+
localizeCommand?: CommandLocalizer;
|
|
30
|
+
beforeExecute?: (ctx: Context, command: any, options: any) => boolean | void | Promise<boolean | void>;
|
|
31
|
+
afterExecute?: (ctx: Context, command: any, options: any) => void | Promise<void>;
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
export declare class Gralonium extends Client {
|
|
@@ -35,7 +39,7 @@ export declare class Gralonium extends Client {
|
|
|
35
39
|
addGlobalPlugins(plugins: Plugin[]): this;
|
|
36
40
|
getContext(data: CommandInteraction | Message): Context;
|
|
37
41
|
load(dir: string, reload?: boolean): Promise<import("./Loader.js").ModuleData<import("./builders/InteractionBuilder.js").InteractionBuilder | import("./builders/CommandBuilder.js").CommandBuilder | import("./builders/EventBuilder.js").EventBuilder | import("./builders/GroupBuilder.js").GroupBuilder, any[]>[]>;
|
|
38
|
-
sync(guildIDs?: string[]): Promise<
|
|
42
|
+
sync(guildIDs?: string[]): Promise<Record<string, any>[]>;
|
|
39
43
|
handleFrameworkError(error: unknown, ctx?: Context | null): void;
|
|
40
44
|
login(token: string): Promise<string>;
|
|
41
45
|
destroy(): this;
|
package/lib/classes/Client.js
CHANGED
package/lib/classes/Context.d.ts
CHANGED
|
@@ -55,6 +55,8 @@ export declare class Context {
|
|
|
55
55
|
get channel(): DJS.TextBasedChannel | null;
|
|
56
56
|
get member(): DJS.GuildMember | DJS.APIInteractionGuildMember | null;
|
|
57
57
|
get guild(): DJS.Guild | null;
|
|
58
|
+
get locale(): DJS.Locale | string | null;
|
|
59
|
+
get guildLocale(): DJS.Locale | string | null;
|
|
58
60
|
defer(options?: ContextDeferOptions): Promise<DJS.Message<boolean> | DJS.InteractionResponse<boolean> | null>;
|
|
59
61
|
send(payload: ContextPayload, options?: ContextSendOptions): Promise<DJS.Message<boolean>>;
|
|
60
62
|
reply(payload: ContextPayload, options?: ContextSendOptions): Promise<DJS.Message<boolean>>;
|
package/lib/classes/Context.js
CHANGED
|
@@ -71,6 +71,14 @@ class Context {
|
|
|
71
71
|
return this.data.guild || null;
|
|
72
72
|
}
|
|
73
73
|
|
|
74
|
+
get locale() {
|
|
75
|
+
return this.data?.locale ?? null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
get guildLocale() {
|
|
79
|
+
return this.data?.guildLocale ?? this.guild?.preferredLocale ?? null;
|
|
80
|
+
}
|
|
81
|
+
|
|
74
82
|
static #isRateLimitError(error) {
|
|
75
83
|
if (!error) return false;
|
|
76
84
|
if (error?.code === 429) return true;
|
|
@@ -364,12 +372,28 @@ class Context {
|
|
|
364
372
|
const lowered = param.toLowerCase();
|
|
365
373
|
|
|
366
374
|
if (this.params?.length) {
|
|
367
|
-
|
|
375
|
+
const match = this.params.find((entry) => entry?.name?.toLowerCase() === lowered);
|
|
376
|
+
if (match) return match.value ?? null;
|
|
368
377
|
}
|
|
369
378
|
|
|
370
379
|
if (this.data instanceof DJS.Message) return null;
|
|
371
380
|
|
|
372
|
-
const
|
|
381
|
+
const findOption = (entries) => {
|
|
382
|
+
if (!Array.isArray(entries)) return null;
|
|
383
|
+
for (const entry of entries) {
|
|
384
|
+
if ((entry?.type === 1 || entry?.type === 2) && Array.isArray(entry.options)) {
|
|
385
|
+
const nested = findOption(entry.options);
|
|
386
|
+
if (nested) return nested;
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
if (typeof entry?.name === "string" && entry.name.toLowerCase() === lowered) {
|
|
390
|
+
return entry;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return null;
|
|
394
|
+
};
|
|
395
|
+
|
|
396
|
+
const option = findOption(this.data.options?.data) ?? this.data.options?.get?.(param) ?? null;
|
|
373
397
|
if (!option) return null;
|
|
374
398
|
|
|
375
399
|
return option.attachment ?? option.member ?? option.channel ?? option.role ?? option.user ?? option.value ?? null;
|
|
@@ -13,6 +13,8 @@ export declare class Cooldowns {
|
|
|
13
13
|
expiresAt: number;
|
|
14
14
|
}>;
|
|
15
15
|
constructor(bot: Gralonium);
|
|
16
|
+
static resolveKey(command: unknown, group?: unknown): string;
|
|
17
|
+
sweep(now?: number): number;
|
|
16
18
|
getCooldownSource(command: string, id: string, bucket: Bucket): Promise<number | undefined>;
|
|
17
19
|
setCooldownSource(command: string, id: string, bucket: Bucket, time: number): Promise<void>;
|
|
18
20
|
check(command: string, id: string, cooldown: number, bucket: Bucket): Promise<{
|
package/lib/classes/Cooldowns.js
CHANGED
|
@@ -23,6 +23,30 @@ class Cooldowns {
|
|
|
23
23
|
return `${command}#${bucket}#${id}`;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
static resolveKey(command, group = null) {
|
|
27
|
+
const commandName =
|
|
28
|
+
typeof command === "string"
|
|
29
|
+
? command
|
|
30
|
+
: command?.data?.name || command?.name || "unknown";
|
|
31
|
+
const groupName =
|
|
32
|
+
typeof group === "string"
|
|
33
|
+
? group
|
|
34
|
+
: group?.data?.name || group?.name || "";
|
|
35
|
+
|
|
36
|
+
return groupName ? `${groupName}:${commandName}` : commandName;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
sweep(now = Date.now()) {
|
|
40
|
+
let cleared = 0;
|
|
41
|
+
for (const [key, entry] of this.track.entries()) {
|
|
42
|
+
if (!entry || now >= entry.expiresAt) {
|
|
43
|
+
this.track.delete(key);
|
|
44
|
+
cleared += 1;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return cleared;
|
|
48
|
+
}
|
|
49
|
+
|
|
26
50
|
async getCooldownSource(command, id, bucket) {
|
|
27
51
|
const key = this.#key(command, id, bucket);
|
|
28
52
|
const tracked = this.track.get(key);
|
|
@@ -38,6 +62,10 @@ class Cooldowns {
|
|
|
38
62
|
|
|
39
63
|
async setCooldownSource(command, id, bucket, time) {
|
|
40
64
|
const now = Date.now();
|
|
65
|
+
if (this.track.size >= 500) {
|
|
66
|
+
this.sweep(now);
|
|
67
|
+
}
|
|
68
|
+
|
|
41
69
|
this.track.set(this.#key(command, id, bucket), {
|
|
42
70
|
startedAt: now,
|
|
43
71
|
expiresAt: now + time,
|
package/lib/classes/Loader.d.ts
CHANGED
|
@@ -76,16 +76,23 @@ type InteractionTypes =
|
|
|
76
76
|
| "mentionableSelectMenu"
|
|
77
77
|
| "anyInteraction";
|
|
78
78
|
|
|
79
|
+
export type CommandLocalizer = (
|
|
80
|
+
commandJSON: Record<string, any>
|
|
81
|
+
) => Record<string, any> | void | Promise<Record<string, any> | void>;
|
|
82
|
+
|
|
79
83
|
export declare class Loader {
|
|
80
84
|
client: Gralonium;
|
|
81
85
|
commands: GraloniumCollected;
|
|
82
86
|
interactions: Record<InteractionTypes, Collection<string, ModuleData<InteractionBuilder>>>;
|
|
83
87
|
interactionMatchers: Map<InteractionTypes, ModuleData<InteractionBuilder>[]>;
|
|
88
|
+
localizer: CommandLocalizer | null;
|
|
84
89
|
globalPlugins: Plugin[];
|
|
85
90
|
rest: REST;
|
|
86
91
|
constructor(options: LoaderOptions);
|
|
92
|
+
setLocalizer(localizer: CommandLocalizer | null): this;
|
|
87
93
|
load(dir: string, reload?: boolean): Promise<ModuleData<InteractionBuilder | CommandBuilder | EventBuilder | GroupBuilder, any[]>[]>;
|
|
88
94
|
resolvePatternInteraction(type: InteractionTypes, key: string, interaction?: any): ModuleData<InteractionBuilder> | null;
|
|
89
95
|
walkCommands(callable?: (command: Command<Types>) => number): CommandHelpingObject[];
|
|
90
|
-
|
|
96
|
+
buildApplicationCommands(): Promise<Record<string, any>[]>;
|
|
97
|
+
sync(guildIDs?: string[]): Promise<Record<string, any>[]>;
|
|
91
98
|
}
|
package/lib/classes/Loader.js
CHANGED
|
@@ -57,10 +57,19 @@ class Loader {
|
|
|
57
57
|
this.globalPlugins = [];
|
|
58
58
|
this.rest = new discord_js_1.REST();
|
|
59
59
|
this.interactionMatchers = new Map();
|
|
60
|
+
this.localizer = options.client?.ops?.localizeCommand ?? null;
|
|
60
61
|
this.#eventHandlers = new Map();
|
|
61
62
|
this.#loadedSignature = new Set();
|
|
62
63
|
}
|
|
63
64
|
|
|
65
|
+
/**
|
|
66
|
+
* Registers a global command localization transformer executed before syncing application commands.
|
|
67
|
+
*/
|
|
68
|
+
setLocalizer(localizer) {
|
|
69
|
+
this.localizer = typeof localizer === "function" ? localizer : null;
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
|
|
64
73
|
#toAbsolutePath(inputPath) {
|
|
65
74
|
if ((0, path_1.isAbsolute)(inputPath)) return inputPath;
|
|
66
75
|
return (0, path_1.join)((0, process_1.cwd)(), inputPath);
|
|
@@ -91,14 +100,39 @@ class Loader {
|
|
|
91
100
|
const mod = require(absolute)?.data;
|
|
92
101
|
if (!mod) continue;
|
|
93
102
|
|
|
94
|
-
if (Array.isArray(mod))
|
|
95
|
-
|
|
103
|
+
if (Array.isArray(mod)) {
|
|
104
|
+
for (const item of mod) {
|
|
105
|
+
const normalized = this.#normalizeLoadedModule(item);
|
|
106
|
+
if (normalized) output.push(normalized);
|
|
107
|
+
}
|
|
108
|
+
} else {
|
|
109
|
+
const normalized = this.#normalizeLoadedModule(mod);
|
|
110
|
+
if (normalized) output.push(normalized);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
#normalizeLoadedModule(item) {
|
|
116
|
+
if (!item || typeof item !== "object") return null;
|
|
117
|
+
if (!item.data && typeof item.name === "string" && typeof item.code === "function") {
|
|
118
|
+
return {
|
|
119
|
+
data: new EventBuilder_js_1.EventBuilder({
|
|
120
|
+
name: item.name,
|
|
121
|
+
once: Boolean(item.once),
|
|
122
|
+
description: item.description,
|
|
123
|
+
}),
|
|
124
|
+
code: item.code,
|
|
125
|
+
};
|
|
96
126
|
}
|
|
127
|
+
return item;
|
|
97
128
|
}
|
|
98
129
|
|
|
99
130
|
#moduleSignature(moduleData) {
|
|
100
131
|
const data = moduleData?.data;
|
|
101
132
|
const type = data?.constructor?.name || "Unknown";
|
|
133
|
+
if (data instanceof EventBuilder_js_1.EventBuilder) {
|
|
134
|
+
return `${type}:${data.name}:${(0, crypto_1.randomUUID)()}`;
|
|
135
|
+
}
|
|
102
136
|
if (data instanceof InteractionBuilder_js_1.InteractionBuilder) {
|
|
103
137
|
const kind = data.type || "unknown";
|
|
104
138
|
const patternTag =
|
|
@@ -274,9 +308,18 @@ class Loader {
|
|
|
274
308
|
return output.sort(sorter);
|
|
275
309
|
}
|
|
276
310
|
|
|
311
|
+
#serializeCommandParams(paramsBuilder) {
|
|
312
|
+
if (!paramsBuilder) return [];
|
|
313
|
+
if (typeof paramsBuilder.toJSON === "function") {
|
|
314
|
+
return paramsBuilder.toJSON();
|
|
315
|
+
}
|
|
316
|
+
return paramsBuilder.params || [];
|
|
317
|
+
}
|
|
318
|
+
|
|
277
319
|
#buildTopLevelSlash(command) {
|
|
278
320
|
const payload = { ...command.data.toJSON() };
|
|
279
|
-
payload.
|
|
321
|
+
payload.type = discord_js_1.ApplicationCommandType.ChatInput;
|
|
322
|
+
payload.options = this.#serializeCommandParams(command.params);
|
|
280
323
|
return payload;
|
|
281
324
|
}
|
|
282
325
|
|
|
@@ -285,7 +328,7 @@ class Loader {
|
|
|
285
328
|
const subCommandJSON = { ...commandRef.data.toJSON() };
|
|
286
329
|
|
|
287
330
|
subCommandJSON.type = 1;
|
|
288
|
-
subCommandJSON.options = commandRef.params
|
|
331
|
+
subCommandJSON.options = this.#serializeCommandParams(commandRef.params);
|
|
289
332
|
|
|
290
333
|
if (subCommandJSON.default_member_permissions === undefined && groupJSON.default_member_permissions !== undefined) {
|
|
291
334
|
subCommandJSON.default_member_permissions = groupJSON.default_member_permissions;
|
|
@@ -315,46 +358,85 @@ class Loader {
|
|
|
315
358
|
payloads.push(groupPayload);
|
|
316
359
|
}
|
|
317
360
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
if (!this.client.user?.id) {
|
|
324
|
-
throw new Error("Cannot sync application commands before client is ready.");
|
|
325
|
-
}
|
|
326
|
-
|
|
327
|
-
this.rest.setToken(this.client.token);
|
|
328
|
-
|
|
361
|
+
/**
|
|
362
|
+
* Builds all application command payloads (ChatInput slash commands + User/Message Context Menus)
|
|
363
|
+
* and applies the configured localization hook if set.
|
|
364
|
+
*/
|
|
365
|
+
async buildApplicationCommands() {
|
|
329
366
|
const commands = this.walkCommands();
|
|
330
367
|
const payload = [];
|
|
331
|
-
const
|
|
368
|
+
const registeredSignatures = new Set();
|
|
332
369
|
|
|
333
370
|
for (const command of commands) {
|
|
334
371
|
if (!command.data.as_slash) continue;
|
|
335
372
|
|
|
336
373
|
if (!command.group) {
|
|
337
374
|
const built = this.#buildTopLevelSlash(command);
|
|
338
|
-
|
|
375
|
+
const sig = `${built.type}:${built.name}`;
|
|
376
|
+
if (registeredSignatures.has(sig)) {
|
|
339
377
|
throw new Error(`Duplicate slash command name detected during sync: ${built.name}`);
|
|
340
378
|
}
|
|
341
|
-
|
|
379
|
+
registeredSignatures.add(sig);
|
|
342
380
|
payload.push(built);
|
|
343
381
|
continue;
|
|
344
382
|
}
|
|
345
383
|
|
|
346
384
|
if (!command.group.data.as_slash) continue;
|
|
347
385
|
this.#mergeGroupSlashPayload(payload, command.group, command);
|
|
386
|
+
registeredSignatures.add(`${discord_js_1.ApplicationCommandType.ChatInput}:${command.group.data.name}`);
|
|
348
387
|
}
|
|
349
388
|
|
|
389
|
+
const contextMenuCollections = [this.interactions.userContextMenu, this.interactions.messageContextMenu];
|
|
390
|
+
for (const collection of contextMenuCollections) {
|
|
391
|
+
for (const entry of collection.values()) {
|
|
392
|
+
if (entry?.data?.as_command === false || !entry?.data?.name) continue;
|
|
393
|
+
const built = entry.data.toCommandJSON();
|
|
394
|
+
if (!built) continue;
|
|
395
|
+
const sig = `${built.type}:${built.name}`;
|
|
396
|
+
if (registeredSignatures.has(sig)) {
|
|
397
|
+
throw new Error(`Duplicate context menu command detected during sync: ${built.name}`);
|
|
398
|
+
}
|
|
399
|
+
registeredSignatures.add(sig);
|
|
400
|
+
payload.push(built);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
const localizer = this.localizer ?? this.client?.ops?.localizeCommand;
|
|
405
|
+
if (typeof localizer === "function") {
|
|
406
|
+
for (let i = 0; i < payload.length; i += 1) {
|
|
407
|
+
const transformed = await localizer(payload[i]);
|
|
408
|
+
if (transformed && typeof transformed === "object") {
|
|
409
|
+
payload[i] = transformed;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
return payload;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
async sync(guildIDs) {
|
|
418
|
+
if (!this.client.token) {
|
|
419
|
+
throw new Error("Cannot sync application commands before login token is set.");
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
if (!this.client.user?.id) {
|
|
423
|
+
throw new Error("Cannot sync application commands before client is ready.");
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
this.rest.setToken(this.client.token);
|
|
427
|
+
|
|
428
|
+
const payload = await this.buildApplicationCommands();
|
|
429
|
+
|
|
350
430
|
if (!guildIDs?.length) {
|
|
351
431
|
await this.rest.put(discord_js_1.Routes.applicationCommands(this.client.user.id), { body: payload });
|
|
352
|
-
return;
|
|
432
|
+
return payload;
|
|
353
433
|
}
|
|
354
434
|
|
|
355
435
|
for (const guildID of guildIDs) {
|
|
356
436
|
await this.rest.put(discord_js_1.Routes.applicationGuildCommands(this.client.user.id, guildID), { body: payload });
|
|
357
437
|
}
|
|
438
|
+
|
|
439
|
+
return payload;
|
|
358
440
|
}
|
|
359
441
|
}
|
|
360
442
|
exports.Loader = Loader;
|
package/lib/classes/Plugins.js
CHANGED
|
@@ -91,11 +91,12 @@ function cooldown(seconds, bucket) {
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
93
|
const cooldownInMs = 1000 * seconds;
|
|
94
|
+
const commandKey = Cooldowns_js_1.Cooldowns.resolveKey(ctx.command, ctx.parent);
|
|
94
95
|
|
|
95
|
-
const active = await ctx.bot.cooldowns.check(
|
|
96
|
+
const active = await ctx.bot.cooldowns.check(commandKey, source, cooldownInMs, bucket);
|
|
96
97
|
if (active) throw new Errors.CommandInCooldown(ctx, active.left);
|
|
97
98
|
|
|
98
|
-
await ctx.bot.cooldowns.setCooldownSource(
|
|
99
|
+
await ctx.bot.cooldowns.setCooldownSource(commandKey, source, bucket, cooldownInMs);
|
|
99
100
|
return true;
|
|
100
101
|
};
|
|
101
102
|
}
|
package/lib/classes/Utils.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ export declare class Utils {
|
|
|
10
10
|
static getChannel<T = DJS.Channel>(query: string, options: { guild: DJS.Guild; force?: boolean }): Promise<T | null>;
|
|
11
11
|
static getRole(query: string, options: { guild: DJS.Guild; force?: boolean }): Promise<DJS.Role | null>;
|
|
12
12
|
static getUser(query: string, bot: Gralonium): Promise<DJS.User | null>;
|
|
13
|
-
static transform(input: string, param: P.BaseParam, ctx: Context, seeable?: boolean): Promise<{ break: boolean; value: any }>;
|
|
13
|
+
static transform(input: string, param: P.BaseParam, ctx: Context, seeable?: boolean, attachmentIndex?: number): Promise<{ break: boolean; value: any }>;
|
|
14
14
|
static executeCommand(
|
|
15
15
|
ctx: Context,
|
|
16
16
|
command: Command<Types.Normal>,
|
package/lib/classes/Utils.js
CHANGED
|
@@ -133,7 +133,7 @@ class Utils {
|
|
|
133
133
|
return paramsBuilder.params.map((param) => ({ ...param, value: undefined }));
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
-
static async transform(input, param, ctx, seeable = true) {
|
|
136
|
+
static async transform(input, param, ctx, seeable = true, attachmentIndex = 0) {
|
|
137
137
|
if (!input && seeable) {
|
|
138
138
|
return { break: false, value: null };
|
|
139
139
|
}
|
|
@@ -149,27 +149,38 @@ class Utils {
|
|
|
149
149
|
|
|
150
150
|
if (param.type === DJS.ApplicationCommandOptionType.String) {
|
|
151
151
|
const value = String(input ?? "");
|
|
152
|
+
const maxLength = typeof param.max_length === "number" ? param.max_length : param.maxLength;
|
|
153
|
+
const minLength = typeof param.min_length === "number" ? param.min_length : param.minLength;
|
|
152
154
|
|
|
153
|
-
if (typeof
|
|
155
|
+
if (typeof maxLength === "number" && value.length > maxLength) {
|
|
154
156
|
throw new Errors.InvalidParam(ctx, `${param.name} exceeds max length.`, { param });
|
|
155
157
|
}
|
|
156
158
|
|
|
157
|
-
if (typeof
|
|
159
|
+
if (typeof minLength === "number" && value.length < minLength) {
|
|
158
160
|
throw new Errors.InvalidParam(ctx, `${param.name} is under min length.`, { param });
|
|
159
161
|
}
|
|
160
162
|
|
|
161
163
|
return { break: false, value };
|
|
162
164
|
}
|
|
163
165
|
|
|
164
|
-
if (param.type === DJS.ApplicationCommandOptionType.Number) {
|
|
166
|
+
if (param.type === DJS.ApplicationCommandOptionType.Integer || param.type === DJS.ApplicationCommandOptionType.Number) {
|
|
165
167
|
const value = Number(input);
|
|
166
|
-
if (
|
|
168
|
+
if (String(input ?? "").trim() === "" || Number.isNaN(value)) {
|
|
169
|
+
throw new Errors.InvalidParamNumber(ctx, param);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (param.type === DJS.ApplicationCommandOptionType.Integer && !Number.isInteger(value)) {
|
|
173
|
+
throw new Errors.InvalidParamNumber(ctx, param);
|
|
174
|
+
}
|
|
167
175
|
|
|
168
|
-
|
|
176
|
+
const maxValue = typeof param.max_value === "number" ? param.max_value : param.maxValue;
|
|
177
|
+
const minValue = typeof param.min_value === "number" ? param.min_value : param.minValue;
|
|
178
|
+
|
|
179
|
+
if (typeof maxValue === "number" && value > maxValue) {
|
|
169
180
|
throw new Errors.InvalidParam(ctx, `${param.name} exceeds max value.`, { param });
|
|
170
181
|
}
|
|
171
182
|
|
|
172
|
-
if (typeof
|
|
183
|
+
if (typeof minValue === "number" && value < minValue) {
|
|
173
184
|
throw new Errors.InvalidParam(ctx, `${param.name} is under min value.`, { param });
|
|
174
185
|
}
|
|
175
186
|
|
|
@@ -188,12 +199,33 @@ class Utils {
|
|
|
188
199
|
throw new Errors.InvalidParamMember(ctx, param);
|
|
189
200
|
}
|
|
190
201
|
|
|
202
|
+
if (param.type === DJS.ApplicationCommandOptionType.Mentionable) {
|
|
203
|
+
const rawInput = String(input ?? "").trim();
|
|
204
|
+
if (/^<@&\d+>$/.test(rawInput) && ctx.guild) {
|
|
205
|
+
const role = await Utils.getRole(rawInput, { guild: ctx.guild, force: true });
|
|
206
|
+
if (role) return { break: false, value: role };
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const memberOrUser = ctx.guild
|
|
210
|
+
? await Utils.getMember(rawInput, { guild: ctx.guild, force: true })
|
|
211
|
+
: await Utils.getUser(rawInput, ctx.bot);
|
|
212
|
+
if (memberOrUser) return { break: false, value: memberOrUser };
|
|
213
|
+
|
|
214
|
+
if (ctx.guild) {
|
|
215
|
+
const role = await Utils.getRole(rawInput, { guild: ctx.guild, force: true });
|
|
216
|
+
if (role) return { break: false, value: role };
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
throw new Errors.InvalidParam(ctx, `${param.name} must resolve to a valid user or role.`, { param });
|
|
220
|
+
}
|
|
221
|
+
|
|
191
222
|
if (param.type === DJS.ApplicationCommandOptionType.Channel) {
|
|
192
223
|
const channel = await Utils.getChannel(String(input), { guild: ctx.guild, force: true });
|
|
193
224
|
if (!channel) throw new Errors.InvalidParamChannel(ctx, param);
|
|
194
225
|
|
|
195
|
-
|
|
196
|
-
|
|
226
|
+
const channelTypes = param.channel_types ?? param.channelTypes;
|
|
227
|
+
if (channelTypes?.length && !channelTypes.includes(channel.type)) {
|
|
228
|
+
throw new Errors.InvalidChannelType(ctx, param, channel.type, channelTypes);
|
|
197
229
|
}
|
|
198
230
|
|
|
199
231
|
return { break: false, value: channel };
|
|
@@ -206,7 +238,18 @@ class Utils {
|
|
|
206
238
|
}
|
|
207
239
|
|
|
208
240
|
if (param.type === DJS.ApplicationCommandOptionType.Attachment) {
|
|
209
|
-
const
|
|
241
|
+
const attachments = ctx.message?.attachments;
|
|
242
|
+
const list = attachments
|
|
243
|
+
? typeof attachments.values === "function"
|
|
244
|
+
? [...attachments.values()]
|
|
245
|
+
: Array.isArray(attachments)
|
|
246
|
+
? attachments
|
|
247
|
+
: []
|
|
248
|
+
: [];
|
|
249
|
+
const attachment =
|
|
250
|
+
list[attachmentIndex] ??
|
|
251
|
+
(attachmentIndex === 0 && typeof attachments?.first === "function" ? attachments.first() : null) ??
|
|
252
|
+
null;
|
|
210
253
|
if (!attachment && param.required) throw new Errors.InvalidParamAttachment(ctx, param);
|
|
211
254
|
return { break: false, value: attachment };
|
|
212
255
|
}
|
|
@@ -223,12 +266,14 @@ class Utils {
|
|
|
223
266
|
}
|
|
224
267
|
|
|
225
268
|
let argIndex = 0;
|
|
269
|
+
let attachmentIndex = 0;
|
|
226
270
|
|
|
227
271
|
for (let paramIndex = 0; paramIndex < params.length; paramIndex += 1) {
|
|
228
272
|
const param = params[paramIndex];
|
|
229
273
|
|
|
230
274
|
if (param.type === DJS.ApplicationCommandOptionType.Attachment) {
|
|
231
|
-
const transformed = await Utils.transform("", param, ctx, false);
|
|
275
|
+
const transformed = await Utils.transform("", param, ctx, false, attachmentIndex);
|
|
276
|
+
attachmentIndex += 1;
|
|
232
277
|
param.value = transformed.value;
|
|
233
278
|
continue;
|
|
234
279
|
}
|
|
@@ -277,12 +322,16 @@ class Utils {
|
|
|
277
322
|
switch (param.type) {
|
|
278
323
|
case DJS.ApplicationCommandOptionType.String:
|
|
279
324
|
return interaction.options.getString(name, required);
|
|
325
|
+
case DJS.ApplicationCommandOptionType.Integer:
|
|
326
|
+
return interaction.options.getInteger(name, required);
|
|
280
327
|
case DJS.ApplicationCommandOptionType.Number:
|
|
281
328
|
return interaction.options.getNumber(name, required);
|
|
282
329
|
case DJS.ApplicationCommandOptionType.Boolean:
|
|
283
330
|
return interaction.options.getBoolean(name, required);
|
|
284
331
|
case DJS.ApplicationCommandOptionType.User:
|
|
285
332
|
return interaction.options.getMember(name) ?? interaction.options.getUser(name, required);
|
|
333
|
+
case DJS.ApplicationCommandOptionType.Mentionable:
|
|
334
|
+
return interaction.options.getMentionable(name, required);
|
|
286
335
|
case DJS.ApplicationCommandOptionType.Channel:
|
|
287
336
|
return interaction.options.getChannel(name, required);
|
|
288
337
|
case DJS.ApplicationCommandOptionType.Role:
|
|
@@ -391,11 +440,12 @@ class Utils {
|
|
|
391
440
|
const bucket = cooldown.bucket || Cooldowns_js_1.Bucket.User;
|
|
392
441
|
const source = Utils.#resolveCooldownSource(ctx, bucket);
|
|
393
442
|
const cooldownMs = cooldown.seconds * 1000;
|
|
443
|
+
const commandKey = Cooldowns_js_1.Cooldowns.resolveKey(command, ctx.parent);
|
|
394
444
|
|
|
395
|
-
const active = await ctx.bot.cooldowns.check(
|
|
445
|
+
const active = await ctx.bot.cooldowns.check(commandKey, source, cooldownMs, bucket);
|
|
396
446
|
if (active) throw new Errors.CommandInCooldown(ctx, active.left);
|
|
397
447
|
|
|
398
|
-
await ctx.bot.cooldowns.setCooldownSource(
|
|
448
|
+
await ctx.bot.cooldowns.setCooldownSource(commandKey, source, bucket, cooldownMs);
|
|
399
449
|
}
|
|
400
450
|
|
|
401
451
|
static #validateContextRestrictions(ctx) {
|
|
@@ -471,7 +521,17 @@ class Utils {
|
|
|
471
521
|
if (!canProceed) return;
|
|
472
522
|
|
|
473
523
|
await Utils.#resolveCommandInput(ctx, command, options);
|
|
524
|
+
|
|
525
|
+
if (typeof ctx.bot?.ops?.beforeExecute === "function") {
|
|
526
|
+
const shouldContinue = await ctx.bot.ops.beforeExecute(ctx, command, options);
|
|
527
|
+
if (shouldContinue === false) return;
|
|
528
|
+
}
|
|
529
|
+
|
|
474
530
|
await command.code(ctx);
|
|
531
|
+
|
|
532
|
+
if (typeof ctx.bot?.ops?.afterExecute === "function") {
|
|
533
|
+
await ctx.bot.ops.afterExecute(ctx, command, options);
|
|
534
|
+
}
|
|
475
535
|
}
|
|
476
536
|
|
|
477
537
|
static async #resolvePrefix(bot, ctx, message) {
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { ApplicationIntegrationType, InteractionContextType } from "discord.js";
|
|
2
2
|
|
|
3
|
+
export type LocalizationMap = Record<string, string>;
|
|
4
|
+
|
|
3
5
|
export declare function validateDefaultMemberPermissions(permissions: unknown): string | null | undefined;
|
|
6
|
+
export declare function normalizeNameLocalizations(map?: LocalizationMap | null): LocalizationMap | undefined;
|
|
7
|
+
export declare function normalizeDescriptionLocalizations(map?: LocalizationMap | null): LocalizationMap | undefined;
|
|
4
8
|
|
|
5
9
|
export interface CommandDataBuilder {
|
|
6
10
|
name: string;
|
|
@@ -18,6 +22,10 @@ export interface CommandDataBuilder {
|
|
|
18
22
|
cooldown?: { seconds: number; bucket?: string };
|
|
19
23
|
integrationTypes?: Array<ApplicationIntegrationType | number>;
|
|
20
24
|
contexts?: Array<InteractionContextType | number>;
|
|
25
|
+
nameLocalizations?: LocalizationMap;
|
|
26
|
+
name_localizations?: LocalizationMap;
|
|
27
|
+
descriptionLocalizations?: LocalizationMap;
|
|
28
|
+
description_localizations?: LocalizationMap;
|
|
21
29
|
}
|
|
22
30
|
|
|
23
31
|
export declare class CommandBuilder {
|
|
@@ -36,6 +44,8 @@ export declare class CommandBuilder {
|
|
|
36
44
|
cooldown?: { seconds: number; bucket?: string };
|
|
37
45
|
integrationTypes?: Array<ApplicationIntegrationType | number>;
|
|
38
46
|
contexts?: Array<InteractionContextType | number>;
|
|
47
|
+
nameLocalizations?: LocalizationMap;
|
|
48
|
+
descriptionLocalizations?: LocalizationMap;
|
|
39
49
|
constructor(options?: CommandDataBuilder);
|
|
40
50
|
setName(name: string): CommandBuilder;
|
|
41
51
|
setDescription(description: string): CommandBuilder;
|
|
@@ -49,11 +59,15 @@ export declare class CommandBuilder {
|
|
|
49
59
|
setCooldown(seconds: number, bucket?: string): CommandBuilder;
|
|
50
60
|
setIntegrationTypes(...types: Array<ApplicationIntegrationType | number | Array<ApplicationIntegrationType | number>>): CommandBuilder;
|
|
51
61
|
setContexts(...contexts: Array<InteractionContextType | number | Array<InteractionContextType | number>>): CommandBuilder;
|
|
62
|
+
setNameLocalizations(locales?: LocalizationMap | null): CommandBuilder;
|
|
63
|
+
setDescriptionLocalizations(locales?: LocalizationMap | null): CommandBuilder;
|
|
52
64
|
toJSON(): {
|
|
53
65
|
name: string;
|
|
54
66
|
description: string;
|
|
55
67
|
default_member_permissions: string | null | undefined;
|
|
56
68
|
dm_permission: boolean | undefined;
|
|
69
|
+
name_localizations?: LocalizationMap;
|
|
70
|
+
description_localizations?: LocalizationMap;
|
|
57
71
|
integration_types?: Array<ApplicationIntegrationType | number>;
|
|
58
72
|
contexts?: Array<InteractionContextType | number>;
|
|
59
73
|
};
|