From a213e727c1920d7c810abb225064c126a96b47fe Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 29 Nov 2023 17:58:01 +0000 Subject: [PATCH] Create base pagination helper --- .dev.env | 4 +-- .prod.env | 2 +- .stage.env | 2 +- src/buttonEvents/Pagination.ts | 23 ++++++++++++++ src/commands/dev/paginationtest.ts | 46 +++++++++++++++++++++++++++ src/helpers/PaginatedEmbed.ts | 51 ++++++++++++++++++++++++++++++ src/helpers/Pagination/TestPage.ts | 10 ++++++ src/registry.ts | 8 ++++- src/type/embedPage.ts | 39 +++++++++++++++++++++++ src/type/paginatedCommand.ts | 14 ++++++++ 10 files changed, 194 insertions(+), 5 deletions(-) create mode 100644 src/buttonEvents/Pagination.ts create mode 100644 src/commands/dev/paginationtest.ts create mode 100644 src/helpers/PaginatedEmbed.ts create mode 100644 src/helpers/Pagination/TestPage.ts create mode 100644 src/type/embedPage.ts create mode 100644 src/type/paginatedCommand.ts diff --git a/.dev.env b/.dev.env index 26a975e..f23aef6 100644 --- a/.dev.env +++ b/.dev.env @@ -7,7 +7,7 @@ # any secret values. BOT_TOKEN= -BOT_VER=0.2.1 DEV +BOT_VER=0.3.0 DEV BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=682942374040961060 @@ -29,4 +29,4 @@ DB_CARD_FILE=:memory: EXPRESS_PORT=3303 GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690 -GDRIVESYNC_AUTO=true +GDRIVESYNC_AUTO=false diff --git a/.prod.env b/.prod.env index f2356de..c2d4d52 100644 --- a/.prod.env +++ b/.prod.env @@ -7,7 +7,7 @@ # any secret values. BOT_TOKEN= -BOT_VER=0.2.1 +BOT_VER=0.3.0 BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=1093810443589529631 diff --git a/.stage.env b/.stage.env index ab0babf..131fe37 100644 --- a/.stage.env +++ b/.stage.env @@ -7,7 +7,7 @@ # any secret values. BOT_TOKEN= -BOT_VER=0.2.1 BETA +BOT_VER=0.3.0 BETA BOT_AUTHOR=Vylpes BOT_OWNERID=147392775707426816 BOT_CLIENTID=1147976642942214235 diff --git a/src/buttonEvents/Pagination.ts b/src/buttonEvents/Pagination.ts new file mode 100644 index 0000000..036dd92 --- /dev/null +++ b/src/buttonEvents/Pagination.ts @@ -0,0 +1,23 @@ +import { ButtonInteraction, CacheType } from "discord.js"; +import { ButtonEvent } from "../type/buttonEvent"; +import { CoreClient } from "../client/client"; +import { PaginatedCommand } from "../type/paginatedCommand"; + +export default class Pagination extends ButtonEvent { + public override async execute(interaction: ButtonInteraction) { + const embedId = interaction.customId.split(' ')[1]; + const action = interaction.customId.split(' ')[2]; + + const commandItem = CoreClient.commandItems.find(x => x.Name == embedId); + const command = commandItem?.Command as PaginatedCommand; + + switch (action) { + case "next": + await command.nextPage(interaction); + break; + case "previous": + await command.previousPage(interaction); + break; + } + } +} \ No newline at end of file diff --git a/src/commands/dev/paginationtest.ts b/src/commands/dev/paginationtest.ts new file mode 100644 index 0000000..76e4790 --- /dev/null +++ b/src/commands/dev/paginationtest.ts @@ -0,0 +1,46 @@ +import { ButtonInteraction, CacheType, CommandInteraction, SlashCommandBuilder } from "discord.js"; +import PaginatedEmbed from "../../helpers/PaginatedEmbed"; +import TestPage from "../../helpers/Pagination/TestPage"; +import { PaginatedCommand } from "../../type/paginatedCommand"; + +export default class Paginationtest extends PaginatedCommand { + public paginatedEmbed: PaginatedEmbed; + + constructor() { + super(); + + super.CommandBuilder = new SlashCommandBuilder() + .setName('paginationtest') + .setDescription('Test the pagination functionality'); + + this.paginatedEmbed = new PaginatedEmbed("paginationtest"); + + this.InitialisePages(); + } + + public override async execute(interaction: CommandInteraction) { + const page = this.paginatedEmbed.GetCurrentPage(); + + await page!.sendCommandEmbed(interaction); + } + + public override async nextPage(interaction: ButtonInteraction) { + const page = this.paginatedEmbed.NextPage(); + + await page!.sendButtonEmbed(interaction); + } + + public override async previousPage(interaction: ButtonInteraction) { + const page = this.paginatedEmbed.PreviousPage(); + + await page!.sendButtonEmbed(interaction); + } + + private InitialisePages() { + const page0 = new TestPage("paginationtest", 0, 2); + const page1 = new TestPage("paginationtest", 1, 2); + const page2 = new TestPage("paginationtest", 2, 2); + + this.paginatedEmbed.AddPages(page0, page1, page2); + } +} \ No newline at end of file diff --git a/src/helpers/PaginatedEmbed.ts b/src/helpers/PaginatedEmbed.ts new file mode 100644 index 0000000..bc5f98e --- /dev/null +++ b/src/helpers/PaginatedEmbed.ts @@ -0,0 +1,51 @@ +import EmbedPage from "../type/embedPage"; + +export default class PaginatedEmbed { + public Id: string; + public CurrentPageIndex: number; + public Pages: TPage[]; + + constructor(id: string) { + this.Id = id; + this.CurrentPageIndex = 0; + this.Pages = []; + } + + public AddPages(...pages: TPage[]) { + this.Pages.push(...pages); + } + + public GetCurrentPage(): TPage | undefined { + if (this.Pages.length == 0) { + return; + } + + return this.Pages[this.CurrentPageIndex]; + } + + public NextPage(): TPage | undefined { + if (this.Pages.length == 0) { + return; + } + + if (this.CurrentPageIndex == this.Pages.length - 1) { + return; + } + + this.CurrentPageIndex++; + return this.GetCurrentPage(); + } + + public PreviousPage(): TPage | undefined { + if (this.Pages.length == 0) { + return; + } + + if (this.CurrentPageIndex == 0) { + return; + } + + this.CurrentPageIndex--; + return this.GetCurrentPage(); + } +} \ No newline at end of file diff --git a/src/helpers/Pagination/TestPage.ts b/src/helpers/Pagination/TestPage.ts new file mode 100644 index 0000000..7eb1f3f --- /dev/null +++ b/src/helpers/Pagination/TestPage.ts @@ -0,0 +1,10 @@ +import EmbedPage from "../../type/embedPage"; + +export default class TestPage extends EmbedPage { + constructor(embedId: string, page: number, total: number) { + super(embedId, page, total); + + this.setTitle("Test Embed"); + this.setDescription(`You are viewing page ${page}`); + } +} \ No newline at end of file diff --git a/src/registry.ts b/src/registry.ts index e602198..ee89bc8 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -1,4 +1,5 @@ import { CoreClient } from "./client/client"; +import { Environment } from "./constants/Environment"; // Global Command Imports import About from "./commands/about"; @@ -10,10 +11,12 @@ import Resync from "./commands/resync"; import Dropnumber from "./commands/stage/dropnumber"; import Droprarity from "./commands/stage/droprarity"; +import Paginationtest from "./commands/dev/paginationtest"; + // Button Event Imports import Claim from "./buttonEvents/Claim"; +import Pagination from "./buttonEvents/Pagination"; import Reroll from "./buttonEvents/Reroll"; -import { Environment } from "./constants/Environment"; export default class Registry { public static RegisterCommands() { @@ -26,6 +29,8 @@ export default class Registry { // Test Commands CoreClient.RegisterCommand('dropnumber', new Dropnumber(), Environment.Test); CoreClient.RegisterCommand('droprarity', new Droprarity(), Environment.Test); + + CoreClient.RegisterCommand('paginationtest', new Paginationtest(), Environment.Local); } public static RegisterEvents() { @@ -34,6 +39,7 @@ export default class Registry { public static RegisterButtonEvents() { CoreClient.RegisterButtonEvent('claim', new Claim()); + CoreClient.RegisterButtonEvent('pagination', new Pagination()); CoreClient.RegisterButtonEvent('reroll', new Reroll()); } } \ No newline at end of file diff --git a/src/type/embedPage.ts b/src/type/embedPage.ts new file mode 100644 index 0000000..82577f5 --- /dev/null +++ b/src/type/embedPage.ts @@ -0,0 +1,39 @@ +import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, CommandInteraction, EmbedBuilder } from "discord.js"; + +export default class EmbedPage extends EmbedBuilder { + private row: ActionRowBuilder; + + constructor(embedId: string, page: number, total: number) { + super(); + + this.setFooter({ text: `Page ${page} of ${total}`}); + + this.row = new ActionRowBuilder(); + + this.row.addComponents( + new ButtonBuilder() + .setCustomId(`pagination ${embedId} previous`) + .setLabel("<") + .setStyle(ButtonStyle.Primary) + .setDisabled(page == 0), + new ButtonBuilder() + .setCustomId(`pagination ${embedId} next`) + .setLabel(">") + .setStyle(ButtonStyle.Primary) + .setDisabled(page == total)); + } + + public async sendCommandEmbed(interaction: CommandInteraction) { + await interaction.reply({ + embeds: [ this ], + components: [ this.row ], + }); + } + + public async sendButtonEmbed(interaction: ButtonInteraction) { + await interaction.reply({ + embeds: [ this ], + components: [ this.row ], + }); + } +} \ No newline at end of file diff --git a/src/type/paginatedCommand.ts b/src/type/paginatedCommand.ts new file mode 100644 index 0000000..6132175 --- /dev/null +++ b/src/type/paginatedCommand.ts @@ -0,0 +1,14 @@ +import { ButtonInteraction, CommandInteraction } from "discord.js"; +import { Command } from "./command"; + +export class PaginatedCommand extends Command { + public CommandBuilder: any; + + public async nextPage(interaction: ButtonInteraction) { + + } + + public async previousPage(interaction: ButtonInteraction) { + + } +}