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.
- package/lib/classes/Client.d.ts +3 -1
- package/lib/classes/Client.js +1 -1
- package/lib/classes/Context.d.ts +40 -1
- package/lib/classes/Context.js +250 -11
- package/lib/classes/HelpCommand.d.ts +55 -13
- package/lib/classes/HelpCommand.js +319 -1
- package/lib/classes/Loader.d.ts +2 -0
- package/lib/classes/Loader.js +42 -39
- package/lib/classes/Utils.js +30 -11
- package/lib/classes/builders/CommandBuilder.d.ts +10 -0
- package/lib/classes/builders/CommandBuilder.js +16 -0
- package/lib/classes/builders/ComponentsV2Builder.d.ts +285 -113
- package/lib/classes/builders/ComponentsV2Builder.js +1069 -180
- package/lib/classes/builders/GroupBuilder.d.ts +9 -0
- package/lib/classes/builders/GroupBuilder.js +16 -0
- package/lib/classes/builders/InteractionBuilder.d.ts +40 -25
- package/lib/classes/builders/InteractionBuilder.js +102 -1
- package/lib/classes/builders/components/StyleMapper.d.ts +1 -1
- package/lib/classes/builders/components/StyleMapper.js +2 -1
- package/lib/classes/builders/components/helpers.d.ts +68 -4
- package/lib/classes/builders/components/helpers.js +366 -7
- package/lib/classes/builders/components/templates.d.ts +151 -9
- package/lib/classes/builders/components/templates.js +376 -17
- package/lib/classes/builders/components/text.d.ts +24 -0
- package/lib/classes/builders/components/text.js +91 -0
- package/lib/classes/experiments/Confirmator.d.ts +44 -26
- package/lib/classes/experiments/Confirmator.js +37 -21
- package/lib/classes/experiments/Paginator.d.ts +48 -29
- package/lib/classes/experiments/Paginator.js +48 -19
- package/lib/classes/views/V2Paginator.d.ts +91 -0
- package/lib/classes/views/V2Paginator.js +268 -0
- package/lib/classes/views/V2View.d.ts +113 -0
- package/lib/classes/views/V2View.js +438 -0
- package/lib/experiments.d.ts +2 -0
- package/lib/experiments.js +9 -1
- package/lib/main.d.ts +3 -0
- package/lib/main.js +3 -0
- package/package.json +3 -1
|
@@ -3,13 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Confirmator = void 0;
|
|
4
4
|
|
|
5
5
|
const discord_js_1 = require("discord.js");
|
|
6
|
+
const helpers_js_1 = require("../builders/components/helpers.js");
|
|
7
|
+
const StyleMapper_js_1 = require("../builders/components/StyleMapper.js");
|
|
6
8
|
|
|
7
9
|
class Confirmator {
|
|
8
10
|
_author;
|
|
9
11
|
_continue = async () => null;
|
|
10
|
-
_cancel = async (interaction) => interaction.reply({ content: "Cancelled process.",
|
|
11
|
-
_filter = async (interaction) =>
|
|
12
|
+
_cancel = async (interaction) => interaction.reply({ content: "Cancelled process.", flags: discord_js_1.MessageFlags.Ephemeral });
|
|
13
|
+
_filter = async (interaction) =>
|
|
14
|
+
interaction.reply({ content: "This interaction isn't for you!", flags: discord_js_1.MessageFlags.Ephemeral });
|
|
12
15
|
filter = async (interaction) => interaction.user.id !== this._author.id;
|
|
16
|
+
_continueId = "continueButton";
|
|
17
|
+
_cancelId = "cancelButton";
|
|
18
|
+
_disableOnEnd = true;
|
|
13
19
|
timeout;
|
|
14
20
|
message;
|
|
15
21
|
bot;
|
|
@@ -17,6 +23,10 @@ class Confirmator {
|
|
|
17
23
|
constructor(bot, options) {
|
|
18
24
|
this._author = options.author;
|
|
19
25
|
this.bot = bot;
|
|
26
|
+
if (options.filter) this.filter = options.filter;
|
|
27
|
+
if (options.continueId) this._continueId = options.continueId;
|
|
28
|
+
if (options.cancelId) this._cancelId = options.cancelId;
|
|
29
|
+
if (options.disableOnEnd !== undefined) this._disableOnEnd = Boolean(options.disableOnEnd);
|
|
20
30
|
this.timeout = options.timeout ?? 60;
|
|
21
31
|
}
|
|
22
32
|
|
|
@@ -46,31 +56,35 @@ class Confirmator {
|
|
|
46
56
|
}
|
|
47
57
|
|
|
48
58
|
static Buttons(options) {
|
|
59
|
+
const continueStyle =
|
|
60
|
+
options?.continue?.style !== undefined ? StyleMapper_js_1.mapButtonStyle(options.continue.style) : discord_js_1.ButtonStyle.Danger;
|
|
61
|
+
const cancelStyle =
|
|
62
|
+
options?.cancel?.style !== undefined ? StyleMapper_js_1.mapButtonStyle(options.cancel.style) : discord_js_1.ButtonStyle.Success;
|
|
63
|
+
|
|
49
64
|
const continueButton = new discord_js_1.ButtonBuilder()
|
|
50
65
|
.setLabel(options?.continue?.label || "Continue")
|
|
51
|
-
.setStyle(
|
|
52
|
-
.setCustomId("continueButton");
|
|
66
|
+
.setStyle(continueStyle)
|
|
67
|
+
.setCustomId(options?.continue?.customId || "continueButton");
|
|
68
|
+
if (options?.continue?.emoji) {
|
|
69
|
+
const parsed = helpers_js_1.parseEmoji(options.continue.emoji);
|
|
70
|
+
if (parsed) continueButton.setEmoji(parsed);
|
|
71
|
+
}
|
|
53
72
|
|
|
54
73
|
const cancelButton = new discord_js_1.ButtonBuilder()
|
|
55
74
|
.setLabel(options?.cancel?.label || "Cancel")
|
|
56
|
-
.setStyle(
|
|
57
|
-
.setCustomId("cancelButton");
|
|
75
|
+
.setStyle(cancelStyle)
|
|
76
|
+
.setCustomId(options?.cancel?.customId || "cancelButton");
|
|
77
|
+
if (options?.cancel?.emoji) {
|
|
78
|
+
const parsed = helpers_js_1.parseEmoji(options.cancel.emoji);
|
|
79
|
+
if (parsed) cancelButton.setEmoji(parsed);
|
|
80
|
+
}
|
|
58
81
|
|
|
59
82
|
return new discord_js_1.ActionRowBuilder().addComponents(continueButton, cancelButton).toJSON();
|
|
60
83
|
}
|
|
61
84
|
|
|
62
85
|
async #disableMessageComponents() {
|
|
63
|
-
if (!this.
|
|
64
|
-
|
|
65
|
-
const components = this.message.components.map((row) => {
|
|
66
|
-
const cloned = discord_js_1.ActionRowBuilder.from(row);
|
|
67
|
-
for (const component of cloned.components) {
|
|
68
|
-
component.setDisabled(true);
|
|
69
|
-
}
|
|
70
|
-
return cloned;
|
|
71
|
-
});
|
|
72
|
-
|
|
73
|
-
await this.message.edit({ components }).catch(() => null);
|
|
86
|
+
if (!this._disableOnEnd) return;
|
|
87
|
+
await helpers_js_1.disableMessageComponents(this.message);
|
|
74
88
|
}
|
|
75
89
|
|
|
76
90
|
start() {
|
|
@@ -84,7 +98,7 @@ class Confirmator {
|
|
|
84
98
|
});
|
|
85
99
|
|
|
86
100
|
collector.on("collect", async (interaction) => {
|
|
87
|
-
if (interaction.customId !==
|
|
101
|
+
if (interaction.customId !== this._continueId && interaction.customId !== this._cancelId) {
|
|
88
102
|
return;
|
|
89
103
|
}
|
|
90
104
|
|
|
@@ -93,21 +107,23 @@ class Confirmator {
|
|
|
93
107
|
return;
|
|
94
108
|
}
|
|
95
109
|
|
|
96
|
-
if (interaction.customId ===
|
|
110
|
+
if (interaction.customId === this._continueId) {
|
|
97
111
|
await interaction.deferUpdate();
|
|
98
|
-
|
|
112
|
+
collector.stop("confirmed");
|
|
99
113
|
await this._continue(interaction);
|
|
100
114
|
return;
|
|
101
115
|
}
|
|
102
116
|
|
|
103
117
|
await interaction.deferUpdate();
|
|
104
|
-
|
|
118
|
+
collector.stop("cancelled");
|
|
105
119
|
await this._cancel(interaction);
|
|
106
120
|
});
|
|
107
121
|
|
|
108
122
|
collector.on("end", async () => {
|
|
109
123
|
await this.#disableMessageComponents();
|
|
110
124
|
});
|
|
125
|
+
|
|
126
|
+
return collector;
|
|
111
127
|
}
|
|
112
128
|
}
|
|
113
129
|
|
|
@@ -1,39 +1,58 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ButtonStyle, CollectedInteraction, InteractionCollector, Message, User } from "discord.js";
|
|
2
|
+
import type { ButtonStyleResolvable } from "../builders/ComponentsV2Builder.js";
|
|
3
|
+
import type { EmojiInput } from "../builders/components/helpers.js";
|
|
2
4
|
import { AsyncFunction } from "../Loader.js";
|
|
3
5
|
import { Gralonium } from "../Client.js";
|
|
6
|
+
|
|
4
7
|
export type ButtonSchema = {
|
|
5
|
-
|
|
6
|
-
|
|
8
|
+
style?: ButtonStyle | ButtonStyleResolvable;
|
|
9
|
+
label?: string;
|
|
10
|
+
emoji?: EmojiInput;
|
|
11
|
+
customId?: string;
|
|
12
|
+
disabled?: boolean;
|
|
7
13
|
};
|
|
8
|
-
|
|
14
|
+
|
|
15
|
+
export type PaginatorProcess = AsyncFunction<CollectedInteraction, any>;
|
|
16
|
+
|
|
9
17
|
export type PaginatorOptions<P = any> = {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
18
|
+
pages: P[];
|
|
19
|
+
author: User;
|
|
20
|
+
filter?: PaginatorProcess;
|
|
21
|
+
timeout?: number;
|
|
22
|
+
previousId?: string;
|
|
23
|
+
nextId?: string;
|
|
24
|
+
disableOnEnd?: boolean;
|
|
14
25
|
};
|
|
26
|
+
|
|
15
27
|
export type PaginatorButtonsOptions = {
|
|
16
|
-
|
|
17
|
-
|
|
28
|
+
next?: ButtonSchema;
|
|
29
|
+
previous?: ButtonSchema;
|
|
18
30
|
};
|
|
31
|
+
|
|
19
32
|
export declare class Paginator<P = any> {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
33
|
+
pages: P[];
|
|
34
|
+
filter: PaginatorProcess;
|
|
35
|
+
bot: Gralonium;
|
|
36
|
+
message?: Message;
|
|
37
|
+
timeout: number;
|
|
38
|
+
constructor(bot: Gralonium, options: PaginatorOptions<P>);
|
|
39
|
+
get page(): P;
|
|
40
|
+
get pageNumber(): number;
|
|
41
|
+
get pagesFooter(): string;
|
|
42
|
+
setUpdateProcess(predicate: AsyncFunction<P, any>): this;
|
|
43
|
+
setFilterProcess(predicate: PaginatorProcess): this;
|
|
44
|
+
setFilterCheckProcess(predicate: PaginatorProcess): this;
|
|
45
|
+
setMessage(message: Message): this;
|
|
46
|
+
static Buttons(
|
|
47
|
+
options?: PaginatorButtonsOptions
|
|
48
|
+
): import("discord.js").APIActionRowComponent<
|
|
49
|
+
| import("discord.js").APIStringSelectComponent
|
|
50
|
+
| import("discord.js").APIChannelSelectComponent
|
|
51
|
+
| import("discord.js").APIMentionableSelectComponent
|
|
52
|
+
| import("discord.js").APIUserSelectComponent
|
|
53
|
+
| import("discord.js").APIRoleSelectComponent
|
|
54
|
+
| import("discord.js").APIButtonComponent
|
|
55
|
+
| import("discord.js").APITextInputComponent
|
|
56
|
+
>;
|
|
57
|
+
start(): InteractionCollector<any>;
|
|
39
58
|
}
|
|
@@ -3,6 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.Paginator = void 0;
|
|
4
4
|
|
|
5
5
|
const discord_js_1 = require("discord.js");
|
|
6
|
+
const helpers_js_1 = require("../builders/components/helpers.js");
|
|
7
|
+
const StyleMapper_js_1 = require("../builders/components/StyleMapper.js");
|
|
8
|
+
const ComponentsV2Builder_js_1 = require("../builders/ComponentsV2Builder.js");
|
|
6
9
|
|
|
7
10
|
class Paginator {
|
|
8
11
|
pages;
|
|
@@ -12,14 +15,29 @@ class Paginator {
|
|
|
12
15
|
timeout;
|
|
13
16
|
_author;
|
|
14
17
|
_page = 0;
|
|
15
|
-
|
|
16
|
-
|
|
18
|
+
_previousId = "previousButton";
|
|
19
|
+
_nextId = "continueButton";
|
|
20
|
+
_disableOnEnd = true;
|
|
21
|
+
_filter = async (interaction) =>
|
|
22
|
+
interaction.reply({ content: "This interaction isn't for you!", flags: discord_js_1.MessageFlags.Ephemeral });
|
|
23
|
+
_update = async (content) => {
|
|
24
|
+
if (ComponentsV2Builder_js_1.ComponentsV2Builder.isBuilder(content)) {
|
|
25
|
+
return this.message.edit(content.toPayload());
|
|
26
|
+
}
|
|
27
|
+
if (content && typeof content === "object" && (content.components || content.embeds || content.content !== undefined)) {
|
|
28
|
+
return this.message.edit(content);
|
|
29
|
+
}
|
|
30
|
+
return this.message.edit({ content: content.toString() });
|
|
31
|
+
};
|
|
17
32
|
|
|
18
33
|
constructor(bot, options) {
|
|
19
34
|
this.bot = bot;
|
|
20
35
|
this.pages = options.pages;
|
|
21
36
|
this._author = options.author;
|
|
22
37
|
if (options.filter) this.filter = options.filter;
|
|
38
|
+
if (options.previousId) this._previousId = options.previousId;
|
|
39
|
+
if (options.nextId) this._nextId = options.nextId;
|
|
40
|
+
if (options.disableOnEnd !== undefined) this._disableOnEnd = Boolean(options.disableOnEnd);
|
|
23
41
|
this.timeout = options.timeout ?? 60;
|
|
24
42
|
}
|
|
25
43
|
|
|
@@ -56,31 +74,40 @@ class Paginator {
|
|
|
56
74
|
}
|
|
57
75
|
|
|
58
76
|
static Buttons(options) {
|
|
77
|
+
const nextStyle = options?.next?.style !== undefined ? StyleMapper_js_1.mapButtonStyle(options.next.style) : discord_js_1.ButtonStyle.Primary;
|
|
78
|
+
const prevStyle =
|
|
79
|
+
options?.previous?.style !== undefined ? StyleMapper_js_1.mapButtonStyle(options.previous.style) : discord_js_1.ButtonStyle.Primary;
|
|
80
|
+
|
|
59
81
|
const nextButton = new discord_js_1.ButtonBuilder()
|
|
60
|
-
.setCustomId("continueButton")
|
|
61
|
-
.setStyle(
|
|
82
|
+
.setCustomId(options?.next?.customId || "continueButton")
|
|
83
|
+
.setStyle(nextStyle)
|
|
62
84
|
.setLabel(options?.next?.label || "Next");
|
|
85
|
+
if (options?.next?.emoji) {
|
|
86
|
+
const parsed = helpers_js_1.parseEmoji(options.next.emoji);
|
|
87
|
+
if (parsed) nextButton.setEmoji(parsed);
|
|
88
|
+
}
|
|
89
|
+
if (options?.next?.disabled !== undefined) {
|
|
90
|
+
nextButton.setDisabled(Boolean(options.next.disabled));
|
|
91
|
+
}
|
|
63
92
|
|
|
64
93
|
const previousButton = new discord_js_1.ButtonBuilder()
|
|
65
|
-
.setCustomId("previousButton")
|
|
66
|
-
.setStyle(
|
|
94
|
+
.setCustomId(options?.previous?.customId || "previousButton")
|
|
95
|
+
.setStyle(prevStyle)
|
|
67
96
|
.setLabel(options?.previous?.label || "Previous");
|
|
97
|
+
if (options?.previous?.emoji) {
|
|
98
|
+
const parsed = helpers_js_1.parseEmoji(options.previous.emoji);
|
|
99
|
+
if (parsed) previousButton.setEmoji(parsed);
|
|
100
|
+
}
|
|
101
|
+
if (options?.previous?.disabled !== undefined) {
|
|
102
|
+
previousButton.setDisabled(Boolean(options.previous.disabled));
|
|
103
|
+
}
|
|
68
104
|
|
|
69
105
|
return new discord_js_1.ActionRowBuilder().addComponents(previousButton, nextButton).toJSON();
|
|
70
106
|
}
|
|
71
107
|
|
|
72
108
|
async #disableMessageComponents() {
|
|
73
|
-
if (!this.
|
|
74
|
-
|
|
75
|
-
const components = this.message.components.map((row) => {
|
|
76
|
-
const cloned = discord_js_1.ActionRowBuilder.from(row);
|
|
77
|
-
for (const component of cloned.components) {
|
|
78
|
-
component.setDisabled(true);
|
|
79
|
-
}
|
|
80
|
-
return cloned;
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
await this.message.edit({ components }).catch(() => null);
|
|
109
|
+
if (!this._disableOnEnd) return;
|
|
110
|
+
await helpers_js_1.disableMessageComponents(this.message);
|
|
84
111
|
}
|
|
85
112
|
|
|
86
113
|
start() {
|
|
@@ -94,7 +121,7 @@ class Paginator {
|
|
|
94
121
|
});
|
|
95
122
|
|
|
96
123
|
collector.on("collect", async (interaction) => {
|
|
97
|
-
if (interaction.customId !==
|
|
124
|
+
if (interaction.customId !== this._previousId && interaction.customId !== this._nextId) {
|
|
98
125
|
return;
|
|
99
126
|
}
|
|
100
127
|
|
|
@@ -103,7 +130,7 @@ class Paginator {
|
|
|
103
130
|
return;
|
|
104
131
|
}
|
|
105
132
|
|
|
106
|
-
if (interaction.customId ===
|
|
133
|
+
if (interaction.customId === this._previousId) {
|
|
107
134
|
await interaction.deferUpdate();
|
|
108
135
|
this._page = this._page <= 0 ? this.pages.length - 1 : this._page - 1;
|
|
109
136
|
await this._update(this.pages[this._page]);
|
|
@@ -118,6 +145,8 @@ class Paginator {
|
|
|
118
145
|
collector.on("end", async () => {
|
|
119
146
|
await this.#disableMessageComponents();
|
|
120
147
|
});
|
|
148
|
+
|
|
149
|
+
return collector;
|
|
121
150
|
}
|
|
122
151
|
}
|
|
123
152
|
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { InteractionCollector, Message, TextBasedChannel, User } from "discord.js";
|
|
2
|
+
import type { ColorResolvable, ComponentsV2Builder, V2BuiltPayload, V2ButtonObjectOptions } from "../builders/ComponentsV2Builder.js";
|
|
3
|
+
import type { EmojiInput } from "../builders/components/helpers.js";
|
|
4
|
+
import type { Context } from "../Context.js";
|
|
5
|
+
import type { V2View, V2ViewAttachOptions, V2ViewEndMode, V2ViewSendOptions, V2ViewThirdPartyMode } from "./V2View.js";
|
|
6
|
+
|
|
7
|
+
export interface V2PaginatorButtonConfig {
|
|
8
|
+
label?: string;
|
|
9
|
+
emoji?: EmojiInput;
|
|
10
|
+
style?: V2ButtonObjectOptions["style"];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface V2PaginatorPageContext<T = any> {
|
|
14
|
+
page: T;
|
|
15
|
+
pageIndex: number;
|
|
16
|
+
pageNumber: number;
|
|
17
|
+
totalPages: number;
|
|
18
|
+
totalItems: number;
|
|
19
|
+
startIndex: number;
|
|
20
|
+
items?: T[];
|
|
21
|
+
controls: V2ButtonObjectOptions[];
|
|
22
|
+
view: V2View<{ pageIndex: number }>;
|
|
23
|
+
state: { pageIndex: number };
|
|
24
|
+
isEnded: boolean;
|
|
25
|
+
isEphemeral: boolean;
|
|
26
|
+
prevId: string;
|
|
27
|
+
nextId: string;
|
|
28
|
+
closeId: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface V2PaginatorOptions<T = any> {
|
|
32
|
+
pages?: T[];
|
|
33
|
+
items?: T[];
|
|
34
|
+
perPage?: number;
|
|
35
|
+
totalItems?: number;
|
|
36
|
+
initialPage?: number;
|
|
37
|
+
wrap?: boolean;
|
|
38
|
+
idPrefix?: string;
|
|
39
|
+
author?: User;
|
|
40
|
+
authorId?: string;
|
|
41
|
+
ownerId?: string;
|
|
42
|
+
timeout?: number;
|
|
43
|
+
time?: number;
|
|
44
|
+
timeoutSeconds?: number;
|
|
45
|
+
idle?: number;
|
|
46
|
+
thirdPartyMode?: V2ViewThirdPartyMode;
|
|
47
|
+
thirdPartyMessage?: string;
|
|
48
|
+
onEnd?: V2ViewEndMode;
|
|
49
|
+
onEndHandler?: (info: { view: V2View<{ pageIndex: number }>; message: Message | null; reason: string }) => unknown;
|
|
50
|
+
isEphemeral?: boolean;
|
|
51
|
+
color?: ColorResolvable;
|
|
52
|
+
accentColor?: ColorResolvable;
|
|
53
|
+
title?: string;
|
|
54
|
+
subtitle?: string;
|
|
55
|
+
thumbnail?: string | null;
|
|
56
|
+
numbered?: boolean;
|
|
57
|
+
emptyText?: string;
|
|
58
|
+
footer?: string;
|
|
59
|
+
renderItem?: (item: T, index: number) => string;
|
|
60
|
+
renderPage?: (ctx: V2PaginatorPageContext<T>) => ComponentsV2Builder | V2BuiltPayload;
|
|
61
|
+
appendControlsToPrebuilt?: boolean;
|
|
62
|
+
alwaysShowControls?: boolean;
|
|
63
|
+
outsideControls?: boolean;
|
|
64
|
+
showPageIndicator?: boolean;
|
|
65
|
+
showFirstLast?: boolean;
|
|
66
|
+
pageIndicatorFormatter?: (page: number, totalPages: number) => string;
|
|
67
|
+
prevButton?: V2PaginatorButtonConfig;
|
|
68
|
+
nextButton?: V2PaginatorButtonConfig;
|
|
69
|
+
firstButton?: V2PaginatorButtonConfig;
|
|
70
|
+
lastButton?: V2PaginatorButtonConfig;
|
|
71
|
+
closeButton?: boolean | V2PaginatorButtonConfig;
|
|
72
|
+
closeReason?: string;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export declare class V2Paginator<T = any> {
|
|
76
|
+
constructor(options: V2PaginatorOptions<T>);
|
|
77
|
+
|
|
78
|
+
readonly view: V2View<{ pageIndex: number }>;
|
|
79
|
+
readonly pageIndex: number;
|
|
80
|
+
readonly totalPages: number;
|
|
81
|
+
readonly collector: InteractionCollector<any> | null;
|
|
82
|
+
readonly message: Message | null;
|
|
83
|
+
|
|
84
|
+
toPayload(options?: { ephemeral?: boolean }): V2BuiltPayload;
|
|
85
|
+
attach(message: Message | any, collectorOptions?: V2ViewAttachOptions): InteractionCollector<any>;
|
|
86
|
+
stop(reason?: string): V2View<{ pageIndex: number }>;
|
|
87
|
+
send(
|
|
88
|
+
target: Context | TextBasedChannel,
|
|
89
|
+
sendOptions?: V2ViewSendOptions
|
|
90
|
+
): Promise<{ view: V2View<{ pageIndex: number }>; message: Message | null; collector: InteractionCollector<any> | null }>;
|
|
91
|
+
}
|
|
@@ -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;
|