gralonium 0.2.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 (47) hide show
  1. package/lib/classes/Client.d.ts +9 -11
  2. package/lib/classes/Client.js +10 -13
  3. package/lib/classes/Context.d.ts +43 -4
  4. package/lib/classes/Context.js +251 -12
  5. package/lib/classes/Cooldowns.d.ts +3 -3
  6. package/lib/classes/Cooldowns.js +3 -8
  7. package/lib/classes/HelpCommand.d.ts +55 -13
  8. package/lib/classes/HelpCommand.js +319 -1
  9. package/lib/classes/Loader.d.ts +17 -11
  10. package/lib/classes/Loader.js +43 -42
  11. package/lib/classes/Utils.d.ts +4 -5
  12. package/lib/classes/Utils.js +32 -21
  13. package/lib/classes/builders/CommandBuilder.d.ts +10 -0
  14. package/lib/classes/builders/CommandBuilder.js +16 -0
  15. package/lib/classes/builders/ComponentsV2Builder.d.ts +285 -113
  16. package/lib/classes/builders/ComponentsV2Builder.js +1069 -182
  17. package/lib/classes/builders/GroupBuilder.d.ts +9 -0
  18. package/lib/classes/builders/GroupBuilder.js +16 -0
  19. package/lib/classes/builders/InteractionBuilder.d.ts +40 -25
  20. package/lib/classes/builders/InteractionBuilder.js +102 -1
  21. package/lib/classes/builders/components/StyleMapper.d.ts +1 -1
  22. package/lib/classes/builders/components/StyleMapper.js +2 -1
  23. package/lib/classes/builders/components/helpers.d.ts +68 -4
  24. package/lib/classes/builders/components/helpers.js +366 -7
  25. package/lib/classes/builders/components/templates.d.ts +151 -9
  26. package/lib/classes/builders/components/templates.js +376 -17
  27. package/lib/classes/builders/components/text.d.ts +24 -0
  28. package/lib/classes/builders/components/text.js +91 -0
  29. package/lib/classes/experiments/Confirmator.d.ts +45 -50
  30. package/lib/classes/experiments/Confirmator.js +37 -21
  31. package/lib/classes/experiments/Paginator.d.ts +49 -56
  32. package/lib/classes/experiments/Paginator.js +48 -19
  33. package/lib/classes/views/V2Paginator.d.ts +91 -0
  34. package/lib/classes/views/V2Paginator.js +268 -0
  35. package/lib/classes/views/V2View.d.ts +113 -0
  36. package/lib/classes/views/V2View.js +438 -0
  37. package/lib/experiments.d.ts +2 -0
  38. package/lib/experiments.js +9 -1
  39. package/lib/main.d.ts +3 -3
  40. package/lib/main.js +5 -5
  41. package/package.json +6 -15
  42. package/lib/events/interactionCreate.d.ts +0 -4
  43. package/lib/events/interactionCreate.js +0 -17
  44. package/lib/events/messageCreate.d.ts +0 -4
  45. package/lib/events/messageCreate.js +0 -17
  46. package/lib/events/messageUpdate.d.ts +0 -4
  47. package/lib/events/messageUpdate.js +0 -17
@@ -0,0 +1,268 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.V2Paginator = void 0;
4
+
5
+ const { ComponentsV2Builder } = require("../builders/ComponentsV2Builder.js");
6
+ const { V2View } = require("./V2View.js");
7
+
8
+ function chunkItems(items, perPage) {
9
+ const size = Math.max(1, Number(perPage) || 10);
10
+ if (!Array.isArray(items) || items.length === 0) {
11
+ return [[]];
12
+ }
13
+ const chunks = [];
14
+ for (let i = 0; i < items.length; i += size) {
15
+ chunks.push(items.slice(i, i + size));
16
+ }
17
+ return chunks;
18
+ }
19
+
20
+ /**
21
+ * Interactive Components V2 paginator built atop `V2View`.
22
+ * Accepts either explicit `pages` or `items` + `perPage` (auto-sliced),
23
+ * with customizable page rendering, wrap navigation, optional close button,
24
+ * and automatic ephemeral third-party cloning when `thirdPartyMode: "ephemeral-view"`.
25
+ */
26
+ class V2Paginator {
27
+ #pages;
28
+ #perPage;
29
+ #totalItems;
30
+ #wrap;
31
+ #view;
32
+ #options;
33
+
34
+ constructor(options = {}) {
35
+ this.#options = options;
36
+ this.#perPage = Math.max(1, Number(options.perPage) || 10);
37
+ this.#wrap = options.wrap !== false;
38
+
39
+ if (Array.isArray(options.pages) && options.pages.length > 0) {
40
+ this.#pages = options.pages;
41
+ this.#totalItems = options.totalItems ?? options.pages.length;
42
+ } else {
43
+ const items = Array.isArray(options.items) ? options.items : [];
44
+ this.#totalItems = items.length;
45
+ this.#pages = chunkItems(items, this.#perPage);
46
+ }
47
+
48
+ const totalPages = this.#pages.length;
49
+ const initialPage = Math.min(Math.max(0, Number(options.initialPage) || 0), totalPages - 1);
50
+
51
+ this.#view = new V2View({
52
+ idPrefix: options.idPrefix,
53
+ author: options.author,
54
+ authorId: options.authorId,
55
+ ownerId: options.ownerId,
56
+ timeout: options.timeout,
57
+ time: options.time,
58
+ timeoutSeconds: options.timeoutSeconds,
59
+ idle: options.idle,
60
+ thirdPartyMode: options.thirdPartyMode || (options.author || options.authorId || options.ownerId ? "reject" : "allow"),
61
+ thirdPartyMessage: options.thirdPartyMessage,
62
+ onEnd: options.onEnd || "disable",
63
+ onEndHandler: options.onEndHandler,
64
+ isEphemeral: options.isEphemeral,
65
+ state: { pageIndex: initialPage },
66
+ render: (renderCtx) => this.#renderPage(renderCtx),
67
+ actions: {
68
+ first: ({ setState }) => {
69
+ setState({ pageIndex: 0 });
70
+ },
71
+ prev: ({ state, setState }) => {
72
+ const prevIdx =
73
+ state.pageIndex <= 0 ? (this.#wrap ? totalPages - 1 : 0) : state.pageIndex - 1;
74
+ setState({ pageIndex: prevIdx });
75
+ },
76
+ next: ({ state, setState }) => {
77
+ const nextIdx =
78
+ state.pageIndex + 1 >= totalPages ? (this.#wrap ? 0 : totalPages - 1) : state.pageIndex + 1;
79
+ setState({ pageIndex: nextIdx });
80
+ },
81
+ last: ({ setState }) => {
82
+ setState({ pageIndex: totalPages - 1 });
83
+ },
84
+ close: ({ view }) => {
85
+ view.stop(options.closeReason || "closed");
86
+ },
87
+ },
88
+ });
89
+ }
90
+
91
+ get view() {
92
+ return this.#view;
93
+ }
94
+
95
+ get pageIndex() {
96
+ return this.#view.state.pageIndex;
97
+ }
98
+
99
+ get totalPages() {
100
+ return this.#pages.length;
101
+ }
102
+
103
+ get collector() {
104
+ return this.#view.collector;
105
+ }
106
+
107
+ get message() {
108
+ return this.#view.message;
109
+ }
110
+
111
+ #buildControls(view, pageIndex, isEnded) {
112
+ const totalPages = this.#pages.length;
113
+ const disabledAll = Boolean(isEnded);
114
+ const prevDisabled = disabledAll || totalPages <= 1 || (!this.#wrap && pageIndex <= 0);
115
+ const nextDisabled = disabledAll || totalPages <= 1 || (!this.#wrap && pageIndex >= totalPages - 1);
116
+
117
+ const prevCfg = this.#options.prevButton || {};
118
+ const nextCfg = this.#options.nextButton || {};
119
+ const closeCfg = this.#options.closeButton;
120
+
121
+ const buttons = [];
122
+
123
+ if (this.#options.showFirstLast && totalPages > 2) {
124
+ buttons.push({
125
+ label: this.#options.firstButton?.label ?? "<<",
126
+ emoji: this.#options.firstButton?.emoji,
127
+ style: this.#options.firstButton?.style || "secondary",
128
+ customId: view.id("first"),
129
+ disabled: prevDisabled,
130
+ });
131
+ }
132
+
133
+ buttons.push({
134
+ label: prevCfg.label ?? (prevCfg.emoji ? undefined : "Anterior"),
135
+ emoji: prevCfg.emoji,
136
+ style: prevCfg.style || "secondary",
137
+ customId: view.id("prev"),
138
+ disabled: prevDisabled,
139
+ });
140
+
141
+ if (this.#options.showPageIndicator !== false) {
142
+ buttons.push({
143
+ label:
144
+ typeof this.#options.pageIndicatorFormatter === "function"
145
+ ? this.#options.pageIndicatorFormatter(pageIndex + 1, totalPages)
146
+ : `${pageIndex + 1} / ${totalPages}`,
147
+ style: "secondary",
148
+ customId: view.id("noop"),
149
+ disabled: true,
150
+ });
151
+ }
152
+
153
+ buttons.push({
154
+ label: nextCfg.label ?? (nextCfg.emoji ? undefined : "Siguiente"),
155
+ emoji: nextCfg.emoji,
156
+ style: nextCfg.style || "secondary",
157
+ customId: view.id("next"),
158
+ disabled: nextDisabled,
159
+ });
160
+
161
+ if (this.#options.showFirstLast && totalPages > 2) {
162
+ buttons.push({
163
+ label: this.#options.lastButton?.label ?? ">>",
164
+ emoji: this.#options.lastButton?.emoji,
165
+ style: this.#options.lastButton?.style || "secondary",
166
+ customId: view.id("last"),
167
+ disabled: nextDisabled,
168
+ });
169
+ }
170
+
171
+ if (closeCfg) {
172
+ const cfgObj = typeof closeCfg === "object" ? closeCfg : {};
173
+ buttons.push({
174
+ label: cfgObj.label ?? (cfgObj.emoji ? undefined : "Cerrar"),
175
+ emoji: cfgObj.emoji,
176
+ style: cfgObj.style || "danger",
177
+ customId: view.id("close"),
178
+ disabled: disabledAll,
179
+ });
180
+ }
181
+
182
+ return buttons;
183
+ }
184
+
185
+ #renderPage({ state, view, isEnded, isEphemeral }) {
186
+ const totalPages = this.#pages.length;
187
+ const pageIndex = Math.min(Math.max(0, state.pageIndex || 0), totalPages - 1);
188
+ const pageData = this.#pages[pageIndex];
189
+ const controls = this.#buildControls(view, pageIndex, isEnded);
190
+
191
+ if (typeof this.#options.renderPage === "function") {
192
+ const custom = this.#options.renderPage({
193
+ page: pageData,
194
+ pageIndex,
195
+ pageNumber: pageIndex + 1,
196
+ totalPages,
197
+ totalItems: this.#totalItems,
198
+ startIndex: pageIndex * this.#perPage,
199
+ items: Array.isArray(pageData) ? pageData : undefined,
200
+ controls,
201
+ view,
202
+ state,
203
+ isEnded,
204
+ isEphemeral,
205
+ prevId: view.id("prev"),
206
+ nextId: view.id("next"),
207
+ closeId: view.id("close"),
208
+ });
209
+
210
+ if (ComponentsV2Builder.isBuilder(custom)) {
211
+ return custom;
212
+ }
213
+ return custom;
214
+ }
215
+
216
+ if (ComponentsV2Builder.isBuilder(pageData)) {
217
+ const basePayload = pageData.toPayload({ ephemeral: isEphemeral });
218
+ if (this.#options.appendControlsToPrebuilt !== false && controls.length > 0) {
219
+ const controlsRow = new ComponentsV2Builder().outside().addButtonsRow(controls.slice(0, 5)).build()[0];
220
+ return {
221
+ ...basePayload,
222
+ components: [...basePayload.components, controlsRow],
223
+ };
224
+ }
225
+ return basePayload;
226
+ }
227
+
228
+ const listItems = Array.isArray(pageData) ? pageData : [pageData];
229
+ const showNav = totalPages > 1 || Boolean(this.#options.closeButton) || this.#options.alwaysShowControls;
230
+ const controlsSlice = showNav ? controls.slice(0, 5) : [];
231
+
232
+ return ComponentsV2Builder.list({
233
+ color: this.#options.color ?? this.#options.accentColor,
234
+ title: this.#options.title,
235
+ subtitle: this.#options.subtitle,
236
+ thumbnail: this.#options.thumbnail,
237
+ items: listItems,
238
+ renderItem: this.#options.renderItem,
239
+ numbered: this.#options.numbered,
240
+ startIndex: pageIndex * this.#perPage,
241
+ emptyText: this.#options.emptyText,
242
+ page: pageIndex + 1,
243
+ totalPages,
244
+ totalItems: this.#options.pages ? undefined : this.#totalItems,
245
+ footer: this.#options.footer,
246
+ buttons: this.#options.outsideControls ? undefined : controlsSlice,
247
+ outsideButtons: this.#options.outsideControls ? controlsSlice : undefined,
248
+ });
249
+ }
250
+
251
+ toPayload(options = {}) {
252
+ return this.#view.toPayload(options);
253
+ }
254
+
255
+ attach(messageTarget, collectorOptions = {}) {
256
+ return this.#view.attach(messageTarget, collectorOptions);
257
+ }
258
+
259
+ stop(reason = "stopped") {
260
+ return this.#view.stop(reason);
261
+ }
262
+
263
+ async send(target, sendOptions = {}) {
264
+ return this.#view.send(target, sendOptions);
265
+ }
266
+ }
267
+
268
+ exports.V2Paginator = V2Paginator;
@@ -0,0 +1,113 @@
1
+ import {
2
+ BaseMessageOptions,
3
+ CollectedInteraction,
4
+ InteractionCollector,
5
+ Message,
6
+ MessageMentionOptions,
7
+ TextBasedChannel,
8
+ User,
9
+ } from "discord.js";
10
+ import type { ComponentsV2Builder, V2BuiltPayload } from "../builders/ComponentsV2Builder.js";
11
+ import type { Context, ContextSendOptions } from "../Context.js";
12
+
13
+ export type V2ViewThirdPartyMode = "reject" | "allow" | "ephemeral-view";
14
+ export type V2ViewEndMode = "disable" | "static" | "delete" | "none";
15
+
16
+ export interface V2ViewRenderContext<S = Record<string, any>> {
17
+ state: S;
18
+ view: V2View<S>;
19
+ id: (action: string, ...extra: Array<string | number>) => string;
20
+ scopedId: (action: string, ...extra: Array<string | number>) => string;
21
+ ownerId: string | null;
22
+ isEphemeral: boolean;
23
+ isEnded: boolean;
24
+ }
25
+
26
+ export interface V2ViewActionContext<S = Record<string, any>> {
27
+ interaction: CollectedInteraction;
28
+ state: S;
29
+ setState: (updater: Partial<S> | ((prev: S) => S)) => V2View<S>;
30
+ view: V2View<S>;
31
+ action: string;
32
+ subId: string | null;
33
+ parts: string[];
34
+ customId: string;
35
+ values: string[];
36
+ isEphemeral: boolean;
37
+ stop: (reason?: string) => V2View<S>;
38
+ id: (action: string, ...extra: Array<string | number>) => string;
39
+ }
40
+
41
+ export type V2ViewActionHandler<S = Record<string, any>> = (
42
+ ctx: V2ViewActionContext<S>
43
+ ) => unknown | false | Promise<unknown | false>;
44
+
45
+ export interface V2ViewOptions<S = Record<string, any>> {
46
+ idPrefix?: string;
47
+ author?: User;
48
+ authorId?: string;
49
+ ownerId?: string;
50
+ state?: S;
51
+ cloneState?: (state: S) => S;
52
+ render?: (ctx: V2ViewRenderContext<S>) => ComponentsV2Builder | V2BuiltPayload;
53
+ renderStatic?: (ctx: V2ViewRenderContext<S>) => ComponentsV2Builder | V2BuiltPayload;
54
+ actions?: Record<string, V2ViewActionHandler<S>>;
55
+ onAction?: V2ViewActionHandler<S>;
56
+ timeout?: number;
57
+ time?: number;
58
+ timeoutSeconds?: number;
59
+ idle?: number;
60
+ thirdPartyMode?: V2ViewThirdPartyMode;
61
+ thirdPartyMessage?: string;
62
+ onEnd?: V2ViewEndMode;
63
+ onEndHandler?: (info: { view: V2View<S>; message: Message | null; reason: string; state: S }) => unknown | Promise<unknown>;
64
+ isEphemeral?: boolean;
65
+ filter?: (interaction: CollectedInteraction, view: V2View<S>) => boolean | Promise<boolean>;
66
+ }
67
+
68
+ export interface V2ViewAttachOptions {
69
+ time?: number;
70
+ idle?: number;
71
+ onError?: (error: unknown, interaction: CollectedInteraction) => void;
72
+ }
73
+
74
+ export type V2ViewSendOptions = ContextSendOptions &
75
+ V2ViewAttachOptions & {
76
+ files?: BaseMessageOptions["files"];
77
+ allowedMentions?: MessageMentionOptions;
78
+ };
79
+
80
+ export declare class V2View<S = Record<string, any>> {
81
+ constructor(options?: V2ViewOptions<S>);
82
+
83
+ readonly idPrefix: string;
84
+ readonly authorId: string | null;
85
+ readonly state: S;
86
+ readonly message: Message | null;
87
+ readonly collector: InteractionCollector<any> | null;
88
+ readonly isEnded: boolean;
89
+ readonly isEphemeral: boolean;
90
+
91
+ id(action: string, ...extra: Array<string | number>): string;
92
+ scopedId(action: string, ...extra: Array<string | number>): string;
93
+ setState(updater: Partial<S> | ((prev: S) => S)): this;
94
+ setRenderer(renderFn: (ctx: V2ViewRenderContext<S>) => ComponentsV2Builder | V2BuiltPayload): this;
95
+ on(action: string, handler: V2ViewActionHandler<S>): this;
96
+ action(action: string, handler: V2ViewActionHandler<S>): this;
97
+
98
+ toPayload(options?: {
99
+ ephemeral?: boolean;
100
+ static?: boolean;
101
+ isEnded?: boolean;
102
+ safeMentions?: boolean;
103
+ allowedMentions?: MessageMentionOptions;
104
+ files?: BaseMessageOptions["files"];
105
+ }): V2BuiltPayload;
106
+
107
+ stop(reason?: string): this;
108
+ attach(message: Message | any, collectorOptions?: V2ViewAttachOptions): InteractionCollector<any>;
109
+ send(
110
+ target: Context | TextBasedChannel,
111
+ sendOptions?: V2ViewSendOptions
112
+ ): Promise<{ view: V2View<S>; message: Message | null; collector: InteractionCollector<any> | null }>;
113
+ }