Skip to content

Commit 797e7f7

Browse files
authored
Merge pull request #169 from Josh-Cena/rep-split
feat: use Paginator to split rep history
2 parents 310c42e + 51e5dde commit 797e7f7

File tree

2 files changed

+116
-24
lines changed

2 files changed

+116
-24
lines changed

src/modules/rep.ts

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

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

1516
export class RepModule extends Module {
1617
constructor(client: CookiecordClient) {
@@ -125,35 +126,49 @@ export class RepModule extends Module {
125126
description: "Reputation: View a user's reputation history",
126127
})
127128
async getrep(msg: Message, @optional user?: User) {
129+
if (!msg.member || msg.channel.type !== 'text') {
130+
return;
131+
}
128132
if (!user) user = msg.author;
129133

130134
const targetRU = await this.getOrMakeUser(user);
135+
const records = (await targetRU.got)
136+
.concat(await targetRU.given)
137+
// Decreasing chronologically
138+
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime())
139+
.map(rg => {
140+
if (rg.from.id == targetRU.id)
141+
return `:white_small_square: Gave 1 rep to <@${
142+
rg.to.id
143+
}> (${prettyMilliseconds(
144+
Date.now() - rg.createdAt.getTime(),
145+
)} ago)`;
146+
else
147+
return `:white_small_square: Got 1 rep from <@${
148+
rg.from.id
149+
}> (${prettyMilliseconds(
150+
Date.now() - rg.createdAt.getTime(),
151+
)} ago)`;
152+
});
153+
const recordsPerPage = 30;
154+
const pages = records
155+
.reduce((acc, cur, index) => {
156+
const curChunk = Math.floor(index / recordsPerPage);
157+
acc[curChunk] ??= [];
158+
acc[curChunk].push(cur);
159+
return acc;
160+
}, [] as string[][])
161+
.map(page => page.join('\n'));
131162
const embed = new MessageEmbed()
132163
.setColor(TS_BLUE)
133-
.setAuthor(user.tag, user.displayAvatarURL())
134-
.setDescription(
135-
(
136-
await Promise.all(
137-
(await targetRU.got)
138-
.concat(await targetRU.given)
139-
.map(async rg => {
140-
if (rg.from.id == targetRU.id)
141-
return `:white_small_square: Gave 1 rep to <@${
142-
rg.to.id
143-
}> (${prettyMilliseconds(
144-
Date.now() - rg.createdAt.getTime(),
145-
)} ago)`;
146-
else
147-
return `:white_small_square: Got 1 rep from <@${
148-
rg.from.id
149-
}> (${prettyMilliseconds(
150-
Date.now() - rg.createdAt.getTime(),
151-
)} ago)`;
152-
}),
153-
)
154-
).join('\n'),
155-
);
156-
await msg.channel.send(embed);
164+
.setAuthor(user.tag, user.displayAvatarURL());
165+
await sendPaginatedMessage(
166+
embed,
167+
pages,
168+
msg.member,
169+
msg.channel,
170+
300000,
171+
);
157172
}
158173

159174
@command({

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)