DisableButtonByArrayOfPages.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. const {MessageEmbed, Client, MessageButton, Intents} = require('discord.js');
  2. const {ChannelPagination, NextPageButton, PreviousPageButton, StopButton, FirstPageButton, LastPageButton, CustomButton} = require("djs-button-pages");
  3. //Array of embeds for pagination.
  4. const embeds =
  5. [
  6. new MessageEmbed().setColor("RANDOM").setDescription("First page!"),
  7. new MessageEmbed().setColor("RANDOM").setDescription("Wow! It's second page!"),
  8. new MessageEmbed().setColor("RANDOM").setDescription("Unbelivable! Third class page!"),
  9. new MessageEmbed().setColor("RANDOM").setDescription("Not possible! Fourth page!"),
  10. new MessageEmbed().setColor("RANDOM").setDescription("Not probable! Special fifth page!"),
  11. new MessageEmbed().setColor("RANDOM").setDescription("Progress! It's page with number six that is stored with number five!"),
  12. new MessageEmbed().setColor("RANDOM").setDescription("You're feeling with determination because of the seven page!"),
  13. new MessageEmbed().setColor("RANDOM").setDescription("You shall not pass! It's the last and the latest page!"),
  14. ]
  15. //Array of buttons for pagination.
  16. const buttons =
  17. [
  18. new FirstPageButton(new MessageButton().setCustomId("first").setLabel("First").setStyle("SUCCESS")),
  19. new PreviousPageButton(new MessageButton().setCustomId("prev").setLabel("Previous").setStyle("PRIMARY")),
  20. new StopButton(new MessageButton().setCustomId("stop").setLabel("Stop").setStyle("DANGER")),
  21. new NextPageButton(new MessageButton().setCustomId("next").setLabel("Next").setStyle("PRIMARY")),
  22. //ALSO CAN BE: new NextPageButton().setStyle(new MessageButton().setCustomId("next").setLabel("Next").setStyle("PRIMARY"))
  23. new LastPageButton(new MessageButton().setCustomId("last").setLabel("Last").setStyle("SUCCESS")),
  24. new CustomButton(new MessageButton().setCustomId("x2").setLabel("Double Page").setStyle("SECONDARY"))
  25. //Doubles current page number. Because of zero-based number we are increasing by one before multiplying and decreasing by one.
  26. .setAction((pagination) => (pagination.currentPage + 1) * 2 - 1)
  27. //If array of number includes next page we should disable it.
  28. .setDisableWhen((pagination, nextPage) => [4, 5, 6, 7].includes(nextPage) ? nextPage : -1),
  29. ]
  30. //Client flags. These are for guilds. For DMs needs `Intents.FLAGS.DIRECT_MESSAGES`.
  31. const client = new Client({intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]});
  32. //Replace YOUR TOKEN with the token from Discord Developer Portal.
  33. client.login("YOUR TOKEN");
  34. //Ready!
  35. client.once("ready", async () => {
  36. console.log("ready");
  37. });
  38. //On message.
  39. client.on("messageCreate", async (message) => {
  40. if (message.content === "!pages")
  41. {
  42. const pagination = await new ChannelPagination() //Create pagination.
  43. .setButtons(buttons) //Pass buttons.
  44. .setEmbeds(embeds) //Pass embeds.
  45. .setTime(60000) //Set life-time to 60000.
  46. .send(message.channel); //Send.
  47. };
  48. });