gralonium 0.3.0 → 0.4.0

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.
Files changed (38) hide show
  1. package/lib/classes/Client.d.ts +3 -1
  2. package/lib/classes/Client.js +1 -1
  3. package/lib/classes/Context.d.ts +40 -1
  4. package/lib/classes/Context.js +250 -11
  5. package/lib/classes/HelpCommand.d.ts +55 -13
  6. package/lib/classes/HelpCommand.js +319 -1
  7. package/lib/classes/Loader.d.ts +2 -0
  8. package/lib/classes/Loader.js +42 -39
  9. package/lib/classes/Utils.js +30 -11
  10. package/lib/classes/builders/CommandBuilder.d.ts +10 -0
  11. package/lib/classes/builders/CommandBuilder.js +16 -0
  12. package/lib/classes/builders/ComponentsV2Builder.d.ts +285 -113
  13. package/lib/classes/builders/ComponentsV2Builder.js +1069 -180
  14. package/lib/classes/builders/GroupBuilder.d.ts +9 -0
  15. package/lib/classes/builders/GroupBuilder.js +16 -0
  16. package/lib/classes/builders/InteractionBuilder.d.ts +40 -25
  17. package/lib/classes/builders/InteractionBuilder.js +102 -1
  18. package/lib/classes/builders/components/StyleMapper.d.ts +1 -1
  19. package/lib/classes/builders/components/StyleMapper.js +2 -1
  20. package/lib/classes/builders/components/helpers.d.ts +68 -4
  21. package/lib/classes/builders/components/helpers.js +366 -7
  22. package/lib/classes/builders/components/templates.d.ts +151 -9
  23. package/lib/classes/builders/components/templates.js +376 -17
  24. package/lib/classes/builders/components/text.d.ts +24 -0
  25. package/lib/classes/builders/components/text.js +91 -0
  26. package/lib/classes/experiments/Confirmator.d.ts +44 -26
  27. package/lib/classes/experiments/Confirmator.js +37 -21
  28. package/lib/classes/experiments/Paginator.d.ts +48 -29
  29. package/lib/classes/experiments/Paginator.js +48 -19
  30. package/lib/classes/views/V2Paginator.d.ts +91 -0
  31. package/lib/classes/views/V2Paginator.js +268 -0
  32. package/lib/classes/views/V2View.d.ts +113 -0
  33. package/lib/classes/views/V2View.js +438 -0
  34. package/lib/experiments.d.ts +2 -0
  35. package/lib/experiments.js +9 -1
  36. package/lib/main.d.ts +3 -0
  37. package/lib/main.js +3 -0
  38. package/package.json +3 -1
@@ -1,7 +1,7 @@
1
1
  import { Client, ClientOptions, CommandInteraction, Message, Snowflake } from "discord.js";
2
2
  import { Context } from "./Context.js";
3
3
  import { Loader, Plugin } from "./Loader.js";
4
- import { HelpCommand } from "./HelpCommand.js";
4
+ import { HelpCommand, HelpCommandOptions } from "./HelpCommand.js";
5
5
  import { Cooldowns } from "./Cooldowns.js";
6
6
 
7
7
  export interface GraloniumRestrictions {
@@ -18,7 +18,9 @@ export interface GraloniumOptions extends ClientOptions {
18
18
  owners?: Snowflake[];
19
19
  prefix: string | string[] | ((ctx: Context) => MaybePromise<string | string[]>);
20
20
  replyOnEdit?: boolean;
21
+ replyOnPrefix?: boolean;
21
22
  helpCommand?: typeof HelpCommand;
23
+ helpCommandOptions?: HelpCommandOptions;
22
24
  restrictions?: GraloniumRestrictions;
23
25
  debug?: boolean;
24
26
  bindProcessHandlers?: boolean;
@@ -121,7 +121,7 @@ class Gralonium extends discord_js_1.Client {
121
121
  #registerHelpCommand() {
122
122
  if (!this.ops.helpCommand) return;
123
123
 
124
- const help = new this.ops.helpCommand();
124
+ const help = new this.ops.helpCommand(this.ops.helpCommandOptions);
125
125
  if (!this.loader.commands.normal) this.loader.commands.normal = new discord_js_1.Collection();
126
126
 
127
127
  this.loader.commands.normal.set("help", {
@@ -1,8 +1,40 @@
1
1
  import { Command, Types } from "../classes/Loader.js";
2
2
  import * as P from "./builders/ParamsBuilder.js";
3
+ import type { ColorResolvable, ComponentsV2Builder, V2BuiltPayload } from "./builders/ComponentsV2Builder.js";
3
4
  import { Gralonium } from "./Client.js";
4
5
  import * as DJS from "discord.js";
5
6
 
7
+ export type ContextPayload =
8
+ | string
9
+ | ComponentsV2Builder
10
+ | V2BuiltPayload
11
+ | DJS.MessagePayload
12
+ | DJS.MessageCreateOptions
13
+ | DJS.InteractionReplyOptions
14
+ | DJS.InteractionEditReplyOptions;
15
+
16
+ export interface ContextDeferOptions {
17
+ ephemeral?: boolean;
18
+ thinking?: boolean;
19
+ thinkingText?: string;
20
+ color?: ColorResolvable;
21
+ reply?: boolean;
22
+ typing?: boolean;
23
+ flags?: number;
24
+ allowedMentions?: DJS.MessageMentionOptions;
25
+ }
26
+
27
+ export interface ContextSendOptions {
28
+ ephemeral?: boolean;
29
+ suppressNotifications?: boolean;
30
+ safeMentions?: boolean;
31
+ allowedMentions?: DJS.MessageMentionOptions;
32
+ files?: DJS.BaseMessageOptions["files"];
33
+ reply?: boolean;
34
+ followUp?: boolean;
35
+ forceChannelSend?: boolean;
36
+ }
37
+
6
38
  export declare class Context {
7
39
  bot: Gralonium;
8
40
  args: string[] | null;
@@ -16,10 +48,17 @@ export declare class Context {
16
48
  get message(): DJS.Message | null;
17
49
  get interaction(): DJS.CommandInteraction | null;
18
50
  get author(): DJS.User;
51
+ get user(): DJS.User;
52
+ get isPrefix(): boolean;
53
+ get isInteraction(): boolean;
54
+ get isSlash(): boolean;
19
55
  get channel(): DJS.TextBasedChannel | null;
20
56
  get member(): DJS.GuildMember | DJS.APIInteractionGuildMember | null;
21
57
  get guild(): DJS.Guild | null;
22
- send(payload: string | DJS.MessagePayload | DJS.MessageCreateOptions | DJS.InteractionReplyOptions | DJS.InteractionEditReplyOptions): Promise<DJS.Message<boolean>>;
58
+ defer(options?: ContextDeferOptions): Promise<DJS.Message<boolean> | DJS.InteractionResponse<boolean> | null>;
59
+ send(payload: ContextPayload, options?: ContextSendOptions): Promise<DJS.Message<boolean>>;
60
+ reply(payload: ContextPayload, options?: ContextSendOptions): Promise<DJS.Message<boolean>>;
61
+ ephemeral(payload: ContextPayload, options?: ContextSendOptions): Promise<DJS.Message<boolean>>;
23
62
  react(...emojis: string[]): Promise<DJS.MessageReaction | undefined>;
24
63
  get<T>(param: string): T | null;
25
64
  }
@@ -4,6 +4,8 @@ exports.Context = void 0;
4
4
 
5
5
  const tslib_1 = require("tslib");
6
6
  const DJS = tslib_1.__importStar(require("discord.js"));
7
+ const ComponentsV2Builder_js_1 = require("./builders/ComponentsV2Builder.js");
8
+ const helpers_js_1 = require("./builders/components/helpers.js");
7
9
 
8
10
  class Context {
9
11
  bot;
@@ -14,6 +16,9 @@ class Context {
14
16
  command;
15
17
  parent = null;
16
18
  params;
19
+ #prefixThinkingMessage = null;
20
+ #prefixThinkingIsV2 = false;
21
+ #deferredEphemeral = false;
17
22
 
18
23
  constructor(bot, data) {
19
24
  this.bot = bot;
@@ -35,6 +40,25 @@ class Context {
35
40
  return this.data instanceof DJS.Message ? this.data.author : this.data.user;
36
41
  }
37
42
 
43
+ get user() {
44
+ return this.author;
45
+ }
46
+
47
+ get isPrefix() {
48
+ return this.data instanceof DJS.Message;
49
+ }
50
+
51
+ get isInteraction() {
52
+ return !(this.data instanceof DJS.Message);
53
+ }
54
+
55
+ get isSlash() {
56
+ return (
57
+ !(this.data instanceof DJS.Message) &&
58
+ (typeof this.data.isChatInputCommand !== "function" || this.data.isChatInputCommand())
59
+ );
60
+ }
61
+
38
62
  get channel() {
39
63
  return this.data.channel || null;
40
64
  }
@@ -87,27 +111,242 @@ class Context {
87
111
  }
88
112
  }
89
113
 
90
- async send(payload) {
114
+ static #detectV2Payload(payload) {
115
+ if (!payload || typeof payload !== "object") return false;
116
+ const v2Flag = DJS.MessageFlags?.IsComponentsV2;
117
+ if (v2Flag !== undefined) {
118
+ if (typeof payload.flags === "number" && (payload.flags & v2Flag) === v2Flag) return true;
119
+ if (typeof payload.flags?.has === "function" && payload.flags.has(v2Flag)) return true;
120
+ }
121
+ if (Array.isArray(payload.components)) {
122
+ return payload.components.some((c) => helpers_js_1.isV2ComponentType((c?.data ?? c)?.type));
123
+ }
124
+ return false;
125
+ }
126
+
127
+ static #assertV2Compatibility(payload) {
128
+ const hasContent = payload.content !== undefined && payload.content !== null && payload.content !== "";
129
+ const hasEmbeds = Array.isArray(payload.embeds) && payload.embeds.length > 0;
130
+ const hasStickers = Array.isArray(payload.stickers) && payload.stickers.length > 0;
131
+ const hasPoll = payload.poll !== undefined && payload.poll !== null;
132
+
133
+ if (hasContent || hasEmbeds || hasStickers || hasPoll) {
134
+ throw new Error(
135
+ "[Context] Payloads using MessageFlags.IsComponentsV2 cannot include content, embeds, stickers, or poll."
136
+ );
137
+ }
138
+ }
139
+
140
+ static #hasEphemeralFlag(payload) {
141
+ const eph = DJS.MessageFlags?.Ephemeral;
142
+ if (payload?.ephemeral) return true;
143
+ if (eph === undefined) return false;
144
+ if (typeof payload?.flags === "number" && (payload.flags & eph) === eph) return true;
145
+ if (typeof payload?.flags?.has === "function" && payload.flags.has(eph)) return true;
146
+ return false;
147
+ }
148
+
149
+ static #stripEphemeralForMessage(payload) {
150
+ if (!payload || typeof payload !== "object" || payload instanceof DJS.MessagePayload) return;
151
+ if ("ephemeral" in payload) {
152
+ delete payload.ephemeral;
153
+ }
154
+ const eph = DJS.MessageFlags?.Ephemeral;
155
+ if (eph === undefined || payload.flags === undefined || payload.flags === null) return;
156
+
157
+ if (typeof payload.flags === "number") {
158
+ payload.flags = payload.flags & ~eph;
159
+ if (payload.flags === 0) delete payload.flags;
160
+ } else if (Array.isArray(payload.flags)) {
161
+ payload.flags = payload.flags.filter((f) => f !== eph && f !== "Ephemeral");
162
+ if (payload.flags.length === 0) delete payload.flags;
163
+ } else if (typeof payload.flags?.remove === "function") {
164
+ payload.flags = payload.flags.remove(eph);
165
+ }
166
+ }
167
+
168
+ #normalizePayload(payload, options = {}) {
169
+ if (payload instanceof DJS.MessagePayload) {
170
+ return { normalized: payload, isV2: Context.#detectV2Payload(payload.options), wantsEphemeral: Boolean(options.ephemeral) };
171
+ }
172
+
173
+ let normalized;
174
+ if (ComponentsV2Builder_js_1.ComponentsV2Builder.isBuilder(payload)) {
175
+ normalized = payload.toPayload({
176
+ ephemeral: options.ephemeral,
177
+ suppressNotifications: options.suppressNotifications,
178
+ safeMentions: options.safeMentions,
179
+ allowedMentions: options.allowedMentions,
180
+ files: options.files,
181
+ });
182
+ } else if (typeof payload === "string") {
183
+ normalized = { content: payload };
184
+ } else if (payload && typeof payload === "object") {
185
+ normalized = { ...payload };
186
+ } else {
187
+ normalized = { content: String(payload ?? "") };
188
+ }
189
+
190
+ if (options.allowedMentions !== undefined && normalized.allowedMentions === undefined) {
191
+ normalized.allowedMentions = options.allowedMentions;
192
+ }
193
+ if (options.files !== undefined && normalized.files === undefined) {
194
+ normalized.files = options.files;
195
+ }
196
+ if (options.ephemeral === true && DJS.MessageFlags?.Ephemeral !== undefined) {
197
+ if (typeof normalized.flags === "number") {
198
+ normalized.flags |= DJS.MessageFlags.Ephemeral;
199
+ } else if (normalized.flags === undefined) {
200
+ normalized.flags = DJS.MessageFlags.Ephemeral;
201
+ }
202
+ }
203
+
204
+ const isV2 = Context.#detectV2Payload(normalized);
205
+ if (isV2) {
206
+ if (typeof normalized.flags === "number") {
207
+ normalized.flags |= DJS.MessageFlags.IsComponentsV2;
208
+ } else if (normalized.flags === undefined) {
209
+ normalized.flags = DJS.MessageFlags.IsComponentsV2;
210
+ }
211
+ Context.#assertV2Compatibility(normalized);
212
+ }
213
+
214
+ const wantsEphemeral = Boolean(options.ephemeral || Context.#hasEphemeralFlag(normalized));
215
+ return { normalized, isV2, wantsEphemeral };
216
+ }
217
+
218
+ /**
219
+ * Defers the interaction reply (respecting `ephemeral`) or optionally sends a
220
+ * thinking placeholder in prefix command mode.
221
+ */
222
+ async defer(options = {}) {
91
223
  if (this.data instanceof DJS.Message) {
92
- return this.#sendWithOptionalRetry(() => this.data.channel.send(payload));
224
+ if (options.thinking) {
225
+ const placeholder = new ComponentsV2Builder_js_1.ComponentsV2Builder({
226
+ color: options.color ?? null,
227
+ }).addText(options.thinkingText || "⏳ Procesando...");
228
+
229
+ const sent = await this.send(placeholder, {
230
+ reply: options.reply,
231
+ allowedMentions: options.allowedMentions,
232
+ });
233
+ this.#prefixThinkingMessage = sent || null;
234
+ this.#prefixThinkingIsV2 = true;
235
+ return this.#prefixThinkingMessage;
236
+ }
237
+ if (options.typing !== false) {
238
+ await this.data.channel?.sendTyping?.().catch(() => null);
239
+ }
240
+ return null;
93
241
  }
94
242
 
95
243
  if (this.data.isAutocomplete?.()) {
96
- throw new Error("Autocomplete interactions cannot send message replies. Use interaction.respond() instead.");
244
+ throw new Error("Autocomplete interactions cannot be deferred.");
245
+ }
246
+
247
+ if (this.data.deferred || this.data.replied) {
248
+ return null;
249
+ }
250
+
251
+ let flags = typeof options.flags === "number" ? options.flags : 0;
252
+ if (options.ephemeral && DJS.MessageFlags?.Ephemeral !== undefined) {
253
+ flags |= DJS.MessageFlags.Ephemeral;
254
+ }
255
+
256
+ this.#deferredEphemeral = Boolean(
257
+ options.ephemeral || (DJS.MessageFlags?.Ephemeral !== undefined && (flags & DJS.MessageFlags.Ephemeral) !== 0)
258
+ );
259
+
260
+ const deferOptions = flags ? { flags } : {};
261
+ return this.#sendWithOptionalRetry(() => this.data.deferReply(deferOptions));
262
+ }
263
+
264
+ /**
265
+ * Sends a reply in both prefix (`Message`) and slash/interaction modes.
266
+ * Accepts strings, message option objects, or `ComponentsV2Builder` instances.
267
+ */
268
+ async send(payload, options = {}) {
269
+ const { normalized, isV2, wantsEphemeral } = this.#normalizePayload(payload, options);
270
+
271
+ if (this.data instanceof DJS.Message) {
272
+ Context.#stripEphemeralForMessage(normalized);
273
+
274
+ if (this.#prefixThinkingMessage) {
275
+ const thinkingMsg = this.#prefixThinkingMessage;
276
+ const thinkingWasV2 = this.#prefixThinkingIsV2;
277
+ this.#prefixThinkingMessage = null;
278
+ this.#prefixThinkingIsV2 = false;
279
+
280
+ const hasPoll = Boolean(normalized && typeof normalized === "object" && normalized.poll !== undefined);
281
+ if (thinkingWasV2 === isV2 && !hasPoll && typeof thinkingMsg.edit === "function") {
282
+ const editData = normalized instanceof DJS.MessagePayload ? normalized : { ...normalized };
283
+ if (editData && typeof editData === "object" && "reply" in editData) {
284
+ delete editData.reply;
285
+ }
286
+ try {
287
+ return await this.#sendWithOptionalRetry(() => thinkingMsg.edit(editData));
288
+ } catch {
289
+ // Fall through to sending a fresh message if placeholder can no longer be edited.
290
+ }
291
+ } else if (typeof thinkingMsg.delete === "function") {
292
+ await thinkingMsg.delete().catch(() => null);
293
+ }
294
+ }
295
+
296
+ const shouldReply = Boolean(options.reply ?? normalized?.reply ?? this.bot?.ops?.replyOnPrefix);
297
+ if (shouldReply && !(normalized instanceof DJS.MessagePayload)) {
298
+ if (!normalized.reply && this.data.id) {
299
+ normalized.reply = { messageReference: this.data.id, failIfNotExists: false };
300
+ }
301
+ if (normalized.allowedMentions === undefined) {
302
+ normalized.allowedMentions = { parse: [], repliedUser: false };
303
+ }
304
+ if (typeof this.data.reply === "function" && !options.forceChannelSend) {
305
+ const replyObj = { ...normalized };
306
+ delete replyObj.reply;
307
+ return this.#sendWithOptionalRetry(() => this.data.reply(replyObj));
308
+ }
309
+ }
310
+
311
+ return this.#sendWithOptionalRetry(() => this.data.channel.send(normalized));
97
312
  }
98
313
 
99
- const replyPayload =
100
- typeof payload === "string"
101
- ? { content: payload }
102
- : payload && typeof payload === "object"
103
- ? payload
104
- : { content: String(payload ?? "") };
314
+ if (this.data.isAutocomplete?.()) {
315
+ throw new Error("Autocomplete interactions cannot send message replies. Use interaction.respond() instead.");
316
+ }
105
317
 
106
318
  if (this.data.deferred || this.data.replied) {
107
- return this.#sendWithOptionalRetry(() => this.data.editReply(replyPayload));
319
+ const wasPublicDefer = !this.#deferredEphemeral && !this.data.ephemeral;
320
+ if (wantsEphemeral && wasPublicDefer) {
321
+ if (this.data.deferred && !this.data.replied && typeof this.data.deleteReply === "function") {
322
+ await this.data.deleteReply().catch(() => null);
323
+ }
324
+ return this.#sendWithOptionalRetry(() => this.data.followUp(normalized));
325
+ }
326
+
327
+ if (options.followUp) {
328
+ return this.#sendWithOptionalRetry(() => this.data.followUp(normalized));
329
+ }
330
+
331
+ return this.#sendWithOptionalRetry(() => this.data.editReply(normalized));
108
332
  }
109
333
 
110
- return this.#sendWithOptionalRetry(() => this.data.reply(replyPayload));
334
+ return this.#sendWithOptionalRetry(() => this.data.reply(normalized));
335
+ }
336
+
337
+ /**
338
+ * Sends a message quoting the invoking message when in prefix mode.
339
+ */
340
+ async reply(payload, options = {}) {
341
+ return this.send(payload, { ...options, reply: true });
342
+ }
343
+
344
+ /**
345
+ * Sends an ephemeral message on interactions, automatically stripping the
346
+ * Ephemeral flag when executed from a prefix message command.
347
+ */
348
+ async ephemeral(payload, options = {}) {
349
+ return this.send(payload, { ...options, ephemeral: true });
111
350
  }
112
351
 
113
352
  async react(...emojis) {
@@ -1,15 +1,57 @@
1
- import { type Plugin, CommandBuilder, Context, CommandHelpingObject, GroupHelpingObject, ParamsBuilder } from "../main.js";
1
+ import { EmbedBuilder } from "discord.js";
2
+ import type { ColorResolvable } from "./builders/ComponentsV2Builder.js";
3
+ import type { CommandBuilder } from "./builders/CommandBuilder.js";
4
+ import type { ParamsBuilder } from "./builders/ParamsBuilder.js";
5
+ import type { Context } from "./Context.js";
6
+ import type { CommandHelpingObject, GroupHelpingObject, Plugin } from "./Loader.js";
7
+ import type { V2ViewThirdPartyMode } from "./views/V2View.js";
8
+
9
+ export interface HelpCommandLabels {
10
+ welcomeTitle?: string;
11
+ welcomeDescription?: string;
12
+ notFound?: string;
13
+ usage?: string;
14
+ arguments?: string;
15
+ noArguments?: string;
16
+ subcommands?: string;
17
+ noSubcommands?: string;
18
+ commandsHeader?: string;
19
+ footerSuffix?: string;
20
+ }
21
+
22
+ export interface HelpCommandOptions {
23
+ useV2?: boolean;
24
+ color?: ColorResolvable;
25
+ errorColor?: ColorResolvable;
26
+ ephemeral?: boolean;
27
+ paginated?: boolean;
28
+ perPage?: number;
29
+ thirdPartyMode?: V2ViewThirdPartyMode;
30
+ closeButton?: boolean | { label?: string; style?: any };
31
+ name?: string;
32
+ aliases?: string[];
33
+ description?: string;
34
+ queryDescription?: string;
35
+ plugins?: Plugin[];
36
+ hiddenCommands?: string[];
37
+ filterCommand?: (command: CommandHelpingObject, ctx: Context) => boolean;
38
+ labels?: HelpCommandLabels;
39
+ }
40
+
2
41
  export declare class HelpCommand {
3
- data: CommandBuilder;
4
- plugins: Plugin[];
5
- params: ParamsBuilder;
6
- code(ctx: Context): Promise<void>;
7
- sendEmpty(ctx: Context): Promise<void>;
8
- sendNotFound(ctx: Context): Promise<void>;
9
- sendCommand(ctx: Context, command: CommandHelpingObject): Promise<void>;
10
- sendGroup(ctx: Context, group: GroupHelpingObject): Promise<void>;
11
- getCommand(ctx: Context, q: string): CommandHelpingObject | null;
12
- getGroup(ctx: Context, q: string): GroupHelpingObject | null;
13
- private doEmbed;
14
- private c;
42
+ options: HelpCommandOptions;
43
+ labels: Required<HelpCommandLabels>;
44
+ data: CommandBuilder;
45
+ plugins: Plugin[];
46
+ params: ParamsBuilder;
47
+ constructor(options?: HelpCommandOptions);
48
+ code(ctx: Context): Promise<void>;
49
+ sendEmpty(ctx: Context): Promise<void>;
50
+ sendNotFound(ctx: Context): Promise<void>;
51
+ sendCommand(ctx: Context, command: CommandHelpingObject): Promise<void>;
52
+ sendGroup(ctx: Context, group: GroupHelpingObject): Promise<void>;
53
+ getCommand(ctx: Context, q: string): CommandHelpingObject | null;
54
+ getGroup(ctx: Context, q: string): GroupHelpingObject | null;
55
+ protected doEmbed(ctx: Context): EmbedBuilder;
56
+ protected c(str: string): string;
15
57
  }