HardCustomButton.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //Imports.
  2. const { Client, EmbedBuilder, ButtonStyle, IntentsBitField } = require("discord.js");
  3. const { ChannelPagination, NextPageButton, PreviousPageButton, StopButton, FirstPageButton, LastPageButton, CustomButton } = require("djs-button-pages");
  4. //Array of embeds for pagination.
  5. const embeds =
  6. [
  7. new EmbedBuilder().setDescription("First page!"),
  8. new EmbedBuilder().setDescription("Wow! It's second page!"),
  9. new EmbedBuilder().setDescription("Unbelivable! Third class page!"),
  10. new EmbedBuilder().setDescription("Not possible! Fourth page!"),
  11. new EmbedBuilder().setDescription("Not probable! Special fifth page!"),
  12. new EmbedBuilder().setDescription("Progress! It's page with number six that is stored with number five!"),
  13. new EmbedBuilder().setDescription("You're feeling with determination because of the seven page!"),
  14. new EmbedBuilder().setDescription("You shall not pass! It's the last and the latest page!"),
  15. ];
  16. //Array of buttons for pagination.
  17. const buttons =
  18. [
  19. new FirstPageButton({custom_id: "first_page", label: "First Page", style: ButtonStyle.Success}),
  20. new PreviousPageButton({custom_id: "prev_page", label: "Previous", style: ButtonStyle.Success}), //Style can be passed either in constructor.
  21. new StopButton({custom_id: "stop", label: "Stop", style: ButtonStyle.Danger}),
  22. new NextPageButton().setStyle({custom_id: "next_page", label: "Next", style: ButtonStyle.Success}), //Or in special method.
  23. new LastPageButton({custom_id: "last_page", label: "Last Page", style: ButtonStyle.Success}),
  24. new CustomButton({custom_id: "custom_button", label: "5", style: ButtonStyle.Secondary})
  25. //Doubles current page number. Because of zero-based numbers we are increasing by one before multiplying and after we should decrease by one.
  26. .setAction((pagination) => (pagination.currentPage + 1) * 2 - 1)
  27. //If page number is greater than half of the pages length then disable button.
  28. .setDisableWhen((pagination, nextPage) => nextPage > pagination.embeds.length / 2 - 1 ? nextPage : -1)
  29. ];
  30. //These very bitfields are needed for this example. For DM use IntentsBitField.Flags.DirectMessages instead of Guilds and GuildMessages.
  31. const client = new Client({intents: [IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.MessageContent]});
  32. //Ready signal!
  33. client.once("ready", () => {
  34. console.log("Ready!");
  35. });
  36. //Action on message.
  37. client.on("messageCreate", async (message) => {
  38. //If user wrote `!pages`.
  39. if (message.content === "!pages")
  40. {
  41. const pagination = new ChannelPagination() //Create pagination.
  42. .setButtons(buttons) //Insert buttons.
  43. .setEmbeds(embeds) //Add embeds.
  44. .setTime(60000); //Set time.
  45. await pagination.send(message.channel); //Send!
  46. };
  47. });
  48. //Login. Replace YOUR TOKEN with token from Discord Developer Portal!
  49. client.login("YOUR TOKEN");