Browse Source

Use separate functions for message and interaction pagination replies

development
Abheek Dhawan 3 years ago
parent
commit
400c4c345e
Signed by: abheekd GPG Key ID: 7BE81B8C14475B67
  1. 42
      src/helpers/util/pagination.ts

42
src/helpers/util/pagination.ts

@ -1,6 +1,6 @@
import { Message, MessageActionRow, MessageButton, MessageEmbed } from 'discord.js'; import { CommandInteraction, Message, MessageActionRow, MessageButton, MessageEmbed } from 'discord.js';
export default async function(message : Message, embeds : MessageEmbed[]) { export async function paginateMessage(message : Message, embeds : MessageEmbed[]) {
let index = 0; let index = 0;
const row = new MessageActionRow; const row = new MessageActionRow;
@ -36,3 +36,41 @@ export default async function(message : Message, embeds : MessageEmbed[]) {
}); });
}); });
} }
export async function paginateInteraction(interaction : CommandInteraction, 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 interaction.followUp({ content: `Page 1 of ${embeds.length}:`, embeds: [embeds[index]], components: [row], fetchReply: true })
.then(async p => {
const paginatorMessage = p as Message;
const filter = i => i.user.id === interaction.user.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;
}
await i.update({ content: `Page ${index + 1} of ${embeds.length}:`, embeds: [embeds[index]] });
});
});
}
Loading…
Cancel
Save