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
|
import { DeleteButton, type BushMessage, type BushSlashMessage } from '#lib';
import {
Constants,
MessageActionRow,
MessageButton,
MessageEmbed,
type MessageComponentInteraction,
type MessageEmbedOptions
} from 'discord.js';
export class ButtonPaginator {
protected message: BushMessage | BushSlashMessage;
protected embeds: MessageEmbed[] | MessageEmbedOptions[];
protected text: string | null;
protected deleteOnExit: boolean;
protected curPage: number;
protected sentMessage: BushMessage | undefined;
/**
* Sends multiple embeds with controls to switch between them
* @param message The message to respond to
* @param embeds The embeds to switch between
* @param text The text send with the embeds (optional)
* @param deleteOnExit Whether to delete the message when the exit button is clicked (defaults to true)
* @param startOn The page to start from (**not** the index)
*/
public static async send(
message: BushMessage | BushSlashMessage,
embeds: MessageEmbed[] | MessageEmbedOptions[],
text: string | null = null,
deleteOnExit = true,
startOn = 1
) {
// no need to paginate if there is only one page
if (embeds.length === 1) return DeleteButton.send(message, { embeds: embeds });
return await new ButtonPaginator(message, embeds, text, deleteOnExit, startOn).send();
}
/**
* The number of pages in the paginator
*/
protected get numPages(): number {
return this.embeds.length;
}
protected constructor(
message: BushMessage | BushSlashMessage,
embeds: MessageEmbed[] | MessageEmbedOptions[],
text: string | null,
deleteOnExit: boolean,
startOn: number
) {
this.message = message;
this.embeds = embeds;
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
this.text = text || null;
this.deleteOnExit = deleteOnExit;
this.curPage = startOn - 1;
// add footers
for (let i = 0; i < embeds.length; i++) {
if (embeds[i] instanceof MessageEmbed) {
(embeds[i] as MessageEmbed).setFooter(`Page ${(i + 1).toLocaleString()}/${embeds.length.toLocaleString()}`);
} else {
(embeds[i] as MessageEmbedOptions).footer = {
text: `Page ${(i + 1).toLocaleString()}/${embeds.length.toLocaleString()}`
};
}
}
}
protected async send() {
this.sentMessage = (await this.message.util.reply({
content: this.text,
embeds: [this.embeds[this.curPage]],
components: [this.getPaginationRow()]
})) as BushMessage;
const collector = this.sentMessage.createMessageComponentCollector({
filter: (i) => i.customId.startsWith('paginate_') && i.message?.id === this.sentMessage!.id,
time: 300000
});
collector.on('collect', (i) => void this.collect(i));
collector.on('end', () => void this.end());
}
protected async collect(interaction: MessageComponentInteraction) {
if (interaction.user.id !== this.message.author.id && !client.config.owners.includes(interaction.user.id))
return await interaction?.deferUpdate().catch(() => null);
switch (interaction.customId) {
case 'paginate_beginning':
this.curPage = 0;
return this.edit(interaction);
case 'paginate_back':
this.curPage--;
return await this.edit(interaction);
case 'paginate_stop':
if (this.deleteOnExit) {
await interaction.deferUpdate().catch(() => null);
return await this.sentMessage!.delete().catch(() => null);
} else {
return await interaction
?.update({
content: `${this.text ? `${this.text}\n` : ''}Command closed by user.`,
embeds: [],
components: []
})
.catch(() => null);
}
case 'paginate_next':
this.curPage++;
return await this.edit(interaction);
case 'paginate_end':
this.curPage = this.embeds.length - 1;
return await this.edit(interaction);
}
}
protected async end() {
if (this.sentMessage && !this.sentMessage.deleted)
return await this.sentMessage
.edit({
content: this.text,
embeds: [this.embeds[this.curPage]],
components: [this.getPaginationRow(true)]
})
.catch(() => null);
}
protected async edit(interaction: MessageComponentInteraction) {
return interaction
?.update({
content: this.text,
embeds: [this.embeds[this.curPage]],
components: [this.getPaginationRow()]
})
.catch(() => null);
}
protected getPaginationRow(disableAll = false): MessageActionRow {
return new MessageActionRow().addComponents(
new MessageButton({
style: Constants.MessageButtonStyles.PRIMARY,
customId: 'paginate_beginning',
emoji: PaginateEmojis.BEGGING,
disabled: disableAll || this.curPage === 0
}),
new MessageButton({
style: Constants.MessageButtonStyles.PRIMARY,
customId: 'paginate_back',
emoji: PaginateEmojis.BACK,
disabled: disableAll || this.curPage === 0
}),
new MessageButton({
style: Constants.MessageButtonStyles.PRIMARY,
customId: 'paginate_stop',
emoji: PaginateEmojis.STOP,
disabled: disableAll
}),
new MessageButton({
style: Constants.MessageButtonStyles.PRIMARY,
customId: 'paginate_next',
emoji: PaginateEmojis.FORWARD,
disabled: disableAll || this.curPage === this.numPages - 1
}),
new MessageButton({
style: Constants.MessageButtonStyles.PRIMARY,
customId: 'paginate_end',
emoji: PaginateEmojis.END,
disabled: disableAll || this.curPage === this.numPages - 1
})
);
}
}
export const enum PaginateEmojis {
BEGGING = '853667381335162910',
BACK = '853667410203770881',
STOP = '853667471110570034',
FORWARD = '853667492680564747',
END = '853667514915225640'
}
|