CustomButton.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //Imports.
  2. const { Client, EmbedBuilder, ButtonStyle, IntentsBitField } = require("discord.js");
  3. const { PaginationWrapper, ButtonWrapper } = require("djs-button-pages");
  4. //Pre-made buttons.
  5. const { NextPageButton, PreviousPageButton } = require('@djs-button-pages/presets');
  6. //Array of embeds for pagination.
  7. const embeds =
  8. [
  9. new EmbedBuilder().setColor("Aqua").setTitle("Test!").setDescription("Whoosh! Your first page!"),
  10. new EmbedBuilder().setColor("Blurple").setTitle("Test!").setDescription("Wow! It's a second one!"),
  11. new EmbedBuilder().setColor("DarkAqua").setTitle("Test!").setDescription("Unbelivable! Third page is available to be bought for 20$!"),
  12. new EmbedBuilder().setColor("DarkGold").setTitle("Test!").setDescription("Not possible! This is my fourth page!"),
  13. new EmbedBuilder().setColor("Gold").setTitle("Test!").setDescription("Not probable! Special fifth page!"),
  14. new EmbedBuilder().setColor("DarkButNotBlack").setTitle("Test!").setDescription("Wow! Another page..."),
  15. new EmbedBuilder().setColor("White").setTitle("Test!").setDescription("Don't tell me that it is page number seven!"),
  16. new EmbedBuilder().setColor("Red").setTitle("Oh, wow!").setDescription("Looks like it is the last page("),
  17. ];
  18. //Array of buttons for pagination.
  19. const buttons =
  20. [
  21. new PreviousPageButton({custom_id: "prev_page", emoji: "◀", style: ButtonStyle.Secondary}),
  22. new NextPageButton({custom_id: "next_page", emoji: "▶", style: ButtonStyle.Secondary}),
  23. new ButtonWrapper({custom_id: "custom_button", label: "x2", style: ButtonStyle.Primary})
  24. //Setting action that doubles page number.
  25. //+1 -1 needed because page number is zero-based.
  26. .setAction(async (pagination) => {
  27. return pagination.setPage((pagination.page + 1) * 2 - 1).update();
  28. })
  29. //Setting switch that disables button when pagination doubled page number will be bigger than pagination has.
  30. .setSwitch(async (pagination) => {
  31. return ((pagination.page + 1) * 2 - 1) > pagination.data.embeds.length;
  32. }),
  33. ];
  34. //These very intents are needed.
  35. //For DMs use IntentBitField.Flags.DirectMessages instead of Guilds and GuildMessages.
  36. const client = new Client({intents: [IntentsBitField.Flags.GuildMessages, IntentsBitField.Flags.Guilds, IntentsBitField.Flags.MessageContent]});
  37. //Ready!
  38. client.once("ready", async () => {
  39. console.log("Ready!");
  40. });
  41. //Catch command.
  42. client.on("messageCreate", async (message) => {
  43. if (message.content === "!pages")
  44. {
  45. //Setup pagination.
  46. const pagination = new PaginationWrapper()
  47. .setButtons(buttons)
  48. .setEmbeds(embeds)
  49. .setTime(60000);
  50. //Send it as a message to a certain channel.
  51. await pagination.send(message.channel);
  52. };
  53. });
  54. //Login.
  55. client.login("yourToken");