Skip to content

Commit 51e5dde

Browse files
committed
refactor: use functional form for paginated message
1 parent c815d10 commit 51e5dde

File tree

3 files changed

+85
-103
lines changed

3 files changed

+85
-103
lines changed

src/modules/rep.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { TS_BLUE } from '../env';
1111

1212
import { RepGive } from '../entities/RepGive';
1313
import { RepUser } from '../entities/RepUser';
14-
import { Paginator } from '../util/Paginator';
14+
import { sendPaginatedMessage } from '../util/sendPaginatedMessage';
1515

1616
export class RepModule extends Module {
1717
constructor(client: CookiecordClient) {
@@ -162,7 +162,13 @@ export class RepModule extends Module {
162162
const embed = new MessageEmbed()
163163
.setColor(TS_BLUE)
164164
.setAuthor(user.tag, user.displayAvatarURL());
165-
new Paginator(embed, pages, msg.member, msg.channel, 300000);
165+
await sendPaginatedMessage(
166+
embed,
167+
pages,
168+
msg.member,
169+
msg.channel,
170+
300000,
171+
);
166172
}
167173

168174
@command({

src/util/Paginator.ts

Lines changed: 0 additions & 101 deletions
This file was deleted.

src/util/sendPaginatedMessage.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {
2+
GuildMember,
3+
MessageEmbed,
4+
MessageReaction,
5+
TextChannel,
6+
User,
7+
} from 'discord.js';
8+
9+
const emojis = {
10+
back: '◀',
11+
first: '⏮',
12+
last: '⏭',
13+
next: '▶',
14+
stop: '⏹',
15+
};
16+
17+
export async function sendPaginatedMessage(
18+
embed: MessageEmbed,
19+
pages: string[],
20+
member: GuildMember,
21+
channel: TextChannel,
22+
timeout: number = 100000,
23+
) {
24+
let curPage = 0;
25+
const message = await channel.send(
26+
embed
27+
.setDescription(pages[curPage])
28+
.setFooter(`Page ${curPage + 1} of ${pages.length}`),
29+
);
30+
if (pages.length === 1) return;
31+
32+
await message.react(emojis.first);
33+
await message.react(emojis.back);
34+
await message.react(emojis.stop);
35+
await message.react(emojis.next);
36+
await message.react(emojis.last);
37+
38+
const collector = message.createReactionCollector(
39+
(reaction, user) =>
40+
user.id === member.id && user.id !== message.author.id,
41+
{ time: timeout },
42+
);
43+
44+
collector.on('collect', async (reaction: MessageReaction, user: User) => {
45+
await reaction.users.remove(user);
46+
47+
switch (reaction.emoji.toString()) {
48+
case emojis.first:
49+
curPage = 0;
50+
break;
51+
case emojis.last:
52+
curPage = pages.length - 1;
53+
break;
54+
case emojis.stop:
55+
await message.reactions.removeAll();
56+
break;
57+
case emojis.back:
58+
curPage--;
59+
if (curPage < 0) curPage = pages.length - 1;
60+
break;
61+
case emojis.next:
62+
curPage++;
63+
if (curPage > pages.length - 1) curPage = 0;
64+
break;
65+
}
66+
67+
await message.edit(
68+
embed
69+
.setDescription(pages[curPage])
70+
.setFooter(`Page ${curPage + 1} of ${pages.length}`),
71+
);
72+
});
73+
74+
collector.on('end', () => {
75+
message.reactions.removeAll();
76+
});
77+
}

0 commit comments

Comments
 (0)