|
| 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