Advertisement
sambeano7

Buton Pagination

Jul 18th, 2025 (edited)
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const { ActionRowBuilder, ButtonBuilder, ButtonStyle, ComponentType, Component, } = require('discord.js');
  2.  
  3. module.exports = async (interaction, pages, time = 30 * 1000) => {
  4.     try {
  5.         if (!interaction || !pages || !pages > 0) throw new Error(`Invalid arguments 3!`);
  6.         await interaction.deferReply();
  7.         if (pages.length === 1) {
  8.             return await interaction.editReply({
  9.                 embeds: pages,
  10.                 components: [],
  11.                 fetchReply: true
  12.             })
  13.         }
  14.         const prev = new ButtonBuilder()
  15.             .setCustomId('prevmenu')
  16.             .setEmoji('⬅')
  17.             .setStyle(ButtonStyle.Primary)
  18.             .setDisabled(true)
  19.         const home = new ButtonBuilder()
  20.             .setCustomId('homemenu')
  21.             .setEmoji('🏠')
  22.             .setStyle(ButtonStyle.Secondary)
  23.             .setDisabled(true)
  24.         const next = new ButtonBuilder()
  25.             .setCustomId('nextmenu')
  26.             .setEmoji('➡')
  27.             .setStyle(ButtonStyle.Primary)
  28.         const buttons = new ActionRowBuilder()
  29.             .addComponents([prev, home, next]);
  30.         let index = 0
  31.         const msg = await interaction.editReply({
  32.             embeds: [pages[index]],
  33.             components: [buttons],
  34.             fetchReply: true
  35.         });
  36.         const mc = await msg.createMessageComponentCollector({
  37.             componentType: ComponentType.Button,
  38.             time,
  39.         });
  40.         mc.on(`collect`, async (i) => {
  41.             await console.log(0)
  42.             if (i.user.id !== interaction.user.id) return await i.reply({ content: `You are not allowed to do this`, ephemeral: true });
  43.             await console.log(1)
  44.             await i.deferUpdate();
  45.             await console.log(2)
  46.             if (i.setCustomId === 'prevmenu') {
  47.                 await console.log(3)
  48.                 if (index > 0) {
  49.                     await console.log(4)
  50.                     await index--
  51.                     await console.log(5)
  52.                 } else if (i.CustomId === 'homemenu') {
  53.                     await console.log(6)
  54.                     index = 0
  55.                     await console.log(7)
  56.                 } else if (i.CustomId === 'nextmenu') {
  57.                     await console.log(8)
  58.                     if (index < pages.length - 1) {
  59.                         await console.log(9)
  60.                         await index++
  61.                         await console.log(10)
  62.                     }
  63.                     await console.log(11)
  64.                 }
  65.                 await console.log(12)
  66.                 if (index === 0) {
  67.                     await console.log(13)
  68.                     await prev.setDisabled(true);
  69.                     await console.log(14)
  70.                     await home.setDisabled(true);
  71.                     await console.log(15)
  72.                 } else {
  73.                     await console.log(16)
  74.                     await prev.setDisabled(false);
  75.                     await console.log(17)
  76.                     await home.setDisabled(false);
  77.                     await console.log(18)
  78.                 }
  79.                 await console.log(19)
  80.                 if (index === pages.length - 1) {
  81.                     await console.log(20)
  82.                     await next.setDisabled(true)
  83.                     await console.log(21)
  84.                 } else {
  85.                     await console.log(22)
  86.                     await next.setDisabled(false);
  87.                     await console.log(23)
  88.                 }
  89.                 await console.log(24)
  90.                 await msg.edit({
  91.                     embeds: [pages[index]],
  92.                     components: [buttons],
  93.                 });
  94.                 await console.log(25)
  95.                 await mc.resetTimer();
  96.                 await console.log(26)
  97.             }
  98.             await console.log(27)
  99.             mc.on(`end`, async () => {
  100.                 await console.log(28)
  101.                 await msg.edit({
  102.                     embeds: [pages[index]],
  103.                     components: [],
  104.                 });
  105.                 await console.log(29)
  106.             })
  107.             await console.log(30)
  108.             return msg;
  109.         })
  110.         await console.log(31)
  111.     } catch (e) {
  112.         await console.log(32)
  113.         await console.log(e)
  114.         await console.log(33)
  115.     }
  116.     await console.log(34)
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement