/
Pagination.ts
311 lines (291 loc) · 7.49 KB
/
Pagination.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import {
CommandInteraction,
ContextMenuInteraction,
Interaction,
Message,
MessageComponentInteraction,
Snowflake,
} from "discord.js";
import { Options } from "../types";
import { PaginationEmbed } from "./PaginationEmbed";
/**
* The pagination class.
*/
export class Pagination extends PaginationEmbed {
//#region public fields
/**
* The interaction that the paginator is for.
* @readonly
*/
public readonly interaction:
| CommandInteraction<"cached">
| ContextMenuInteraction<"cached">
| MessageComponentInteraction<"cached">
| Message;
/**
* All the authorized users who can use the pagination buttons
*/
public authorizedUsers: Snowflake[];
//#end region
/**
* @param interaction
* @param options
* @example
* ```javascript
* const pagination = new Pagination(interaction, {
* firstEmoji: "⏮",
* prevEmoji: "◀️",
* nextEmoji: "▶️",
* lastEmoji: "⏭",
* limit: 5,
* idle: 5 * 60 * 1000,
* ephemeral: false,
* prevDescription: "",
* postDescription: "",
* attachments: [],
* buttonStyle: "SECONDARY",
* loop: false,
* });
* ```
*
*/
constructor(
interaction:
| CommandInteraction<"cached">
| ContextMenuInteraction<"cached">
| MessageComponentInteraction<"cached">
| Message,
options: Partial<Options> = {}
) {
super(options);
if (
!(interaction instanceof Interaction) &&
!(interaction instanceof Message)
) {
throw new Error(
"The interaction must be an instance of Interaction or Message"
);
}
this.interaction = interaction;
this.authorizedUsers = [
(
(interaction as CommandInteraction).user ??
(interaction as Message).author
).id,
];
}
//#region authorized users related
/**
* Sets authorized users who can use these pagination buttons.
* Leave it a empty array to allow everyone to use the pagination.
* @param authorizedUsers
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction)
* .setAuthorizedUsers([userId1, userId2, userId3]);
* ```
*
*/
setAuthorizedUsers(authorizedUsers: Snowflake[]): this {
this.authorizedUsers = authorizedUsers;
return this;
}
/**
* Adds a authorized user who can use these pagination buttons.
* @param authorizedUser
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction)
* .addAuthorizedUser(userId1);
* ```
*
*/
addAuthorizedUser(authorizedUser: Snowflake): this {
this.authorizedUsers.push(authorizedUser);
return this;
}
/**
* Adds multiple authorized users who can use these pagination buttons.
* @param authorizedUsers
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction)
* .addAuthorizedUsers([userId1, userId2, userId3]);
* ```
*
*/
addAuthorizedUsers(authorizedUsers: Snowflake[]): this {
this.authorizedUsers.push(...authorizedUsers);
return this;
}
//#end region
/**
* Makes the pagination interactive.
* @param message
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction);
* ...
* pagination.paginate(message);
* ```
*
*/
paginate(message: Message): this {
const collector = message.createMessageComponentCollector({
filter: ({ customId }) =>
Object.values(this.buttons).some((b) => b.customId === customId),
idle: this.idle,
});
collector.on("collect", async (i) => {
// here filter isn't used just to avoid the `interaction failed` error
if (
this.authorizedUsers.length &&
!this.authorizedUsers.includes(i.user.id)
) {
return i.deferUpdate();
}
if (!i.isButton()) return;
if (i.customId === this.buttons.first?.customId) {
this.goFirst(i);
}
if (i.customId === this.buttons.prev?.customId) {
this.goPrev(i);
}
if (i.customId === this.buttons.next?.customId) {
this.goNext(i);
}
if (i.customId === this.buttons.last?.customId) {
this.goLast(i);
}
});
return this;
}
/**
* Sends the final message.
* By default, it will send as a reply to the message
* but if the interaction is already replied or deferred then it will `editReply`.
* If you want to send follow-up or update the interaction, then use {@link followUp} or {@link update} instead.
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction);
* ...
* pagination.render();
* ```
*
*/
async render(): Promise<Message> {
if (
this.interaction instanceof Interaction &&
(this.interaction.replied || this.interaction.deferred)
) {
return this.editReply();
}
return this.reply();
}
/**
* Replies the final message.
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction);
* ...
* pagination.reply();
* ```
*
*/
async reply(): Promise<Message> {
const payloads = this.ready();
//TODO: remove assertions
const message = await (
this.interaction as
| CommandInteraction<"cached">
| MessageComponentInteraction<"cached">
| ContextMenuInteraction<"cached">
).reply(payloads);
this.paginate(message);
return message;
}
/**
* Sends the reply as a `followUp`.
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction);
* ...
* pagination.followUp();
* ```
*
*/
async followUp(): Promise<Message> {
const payloads = this.ready();
if (!(this.interaction instanceof Interaction))
throw new Error("The interaction is not an instance of Interaction");
const message = await this.interaction.followUp(payloads);
this.paginate(message);
return message;
}
/**
* Edits the original reply with the final message.
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction);
* ...
* pagination.editReply();
* ```
*
*/
async editReply(): Promise<Message> {
const payloads = this.ready();
if (!(this.interaction instanceof Interaction))
throw new Error("The interaction is not an instance of Interaction");
const message = await this.interaction.editReply(payloads);
this.paginate(message);
return message;
}
/**
* Updates the interaction's pagination.
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction);
* ...
* pagination.update();
* ```
*
*/
async update(): Promise<Message> {
const payloads = this.ready();
if (!(this.interaction instanceof MessageComponentInteraction))
throw new Error(
"The interaction is not an instance of MessageComponentInteraction"
);
const message = await this.interaction.update(payloads);
this.paginate(message);
return message;
}
/**
* Sends the final message in the interaction's channel.
* @returns
* @example
* ```javascript
* const pagination = new Pagination(interaction);
* ...
* pagination.send();
* ```
*
*/
async send(): Promise<Message> {
const payloads = this.ready();
if (!this.interaction.channel)
throw new Error("The interaction or message don't have a channel");
const message = await this.interaction.channel.send(payloads);
this.paginate(message);
return message;
}
}