From d6035e4e789ffd29c68decc5aabf245b110ad633 Mon Sep 17 00:00:00 2001 From: Abheek Dhawan Date: Tue, 5 Apr 2022 22:13:59 -0500 Subject: [PATCH] Add basic paginator function for future usage --- src/helpers/util/pagination.ts | 38 ++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/helpers/util/pagination.ts diff --git a/src/helpers/util/pagination.ts b/src/helpers/util/pagination.ts new file mode 100644 index 0000000..7dd59ea --- /dev/null +++ b/src/helpers/util/pagination.ts @@ -0,0 +1,38 @@ +import { Message, MessageActionRow, MessageButton, MessageEmbed } from 'discord.js'; + +export default async function(message : Message, embeds : MessageEmbed[]) { + let index = 0; + + const row = new MessageActionRow; + row.addComponents( + new MessageButton() + .setCustomId('paginator-left') + .setEmoji('868552005977788466') + .setStyle('SECONDARY'), + new MessageButton() + .setCustomId('paginator-right') + .setEmoji('868551772887711754') + .setStyle('SECONDARY') + ); + + await message.reply({ content: `Page 1 of ${embeds.length}:`, embeds: [embeds[index]], components: [row] }) + .then(async paginatorMessage => { + const filter = m => m.author.id === message.author.id; + + const paginatorCollector = paginatorMessage.createMessageComponentCollector({ componentType: 'BUTTON', filter: filter }); + + paginatorCollector.on('collect', async i => { + switch (i.customId) { + case 'paginator-left': + index--; + if (index < 0) index = embeds.length - 1; + break; + case 'paginator-right': + index++; + if (index > embeds.length - 1) index = 0; + break; + } + paginatorMessage.edit({ content: `Page ${index + 1} of ${embeds.length}:`, embeds: [embeds[index]] }); + }); + }); +} \ No newline at end of file