From 3fc8ea3e281473a4003bc18cc18a329836ce825e Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 1 Dec 2023 18:51:08 +0000 Subject: [PATCH 1/2] Make pages update initial embed --- src/helpers/Pagination/TestPage.ts | 2 +- src/type/embedPage.ts | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/helpers/Pagination/TestPage.ts b/src/helpers/Pagination/TestPage.ts index 7eb1f3f..87cc4ec 100644 --- a/src/helpers/Pagination/TestPage.ts +++ b/src/helpers/Pagination/TestPage.ts @@ -5,6 +5,6 @@ export default class TestPage extends EmbedPage { super(embedId, page, total); this.setTitle("Test Embed"); - this.setDescription(`You are viewing page ${page}`); + this.setDescription(`You are viewing page ${page + 1}`); } } \ No newline at end of file diff --git a/src/type/embedPage.ts b/src/type/embedPage.ts index 82577f5..0099aa1 100644 --- a/src/type/embedPage.ts +++ b/src/type/embedPage.ts @@ -6,19 +6,19 @@ export default class EmbedPage extends EmbedBuilder { constructor(embedId: string, page: number, total: number) { super(); - this.setFooter({ text: `Page ${page} of ${total}`}); + this.setFooter({ text: `Page ${page + 1} of ${total + 1}`}); this.row = new ActionRowBuilder(); this.row.addComponents( new ButtonBuilder() .setCustomId(`pagination ${embedId} previous`) - .setLabel("<") + .setLabel("◀️") .setStyle(ButtonStyle.Primary) .setDisabled(page == 0), new ButtonBuilder() .setCustomId(`pagination ${embedId} next`) - .setLabel(">") + .setLabel("▶️") .setStyle(ButtonStyle.Primary) .setDisabled(page == total)); } @@ -31,9 +31,10 @@ export default class EmbedPage extends EmbedBuilder { } public async sendButtonEmbed(interaction: ButtonInteraction) { - await interaction.reply({ + await interaction.update({ embeds: [ this ], components: [ this.row ], }); + return; } } \ No newline at end of file From d29a81c786a777f791d2e67f17d326de45d8fb18 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 1 Dec 2023 18:57:33 +0000 Subject: [PATCH 2/2] Clean up some code --- src/buttonEvents/Pagination.ts | 5 +++-- src/commands/dev/paginationtest.ts | 18 +----------------- src/type/paginatedCommand.ts | 21 +++++++++++++++++---- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/buttonEvents/Pagination.ts b/src/buttonEvents/Pagination.ts index 036dd92..d39371c 100644 --- a/src/buttonEvents/Pagination.ts +++ b/src/buttonEvents/Pagination.ts @@ -2,14 +2,15 @@ import { ButtonInteraction, CacheType } from "discord.js"; import { ButtonEvent } from "../type/buttonEvent"; import { CoreClient } from "../client/client"; import { PaginatedCommand } from "../type/paginatedCommand"; +import EmbedPage from "../type/embedPage"; -export default class Pagination extends ButtonEvent { +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; + const command = commandItem?.Command as PaginatedCommand; switch (action) { case "next": diff --git a/src/commands/dev/paginationtest.ts b/src/commands/dev/paginationtest.ts index 76e4790..1a3d6ac 100644 --- a/src/commands/dev/paginationtest.ts +++ b/src/commands/dev/paginationtest.ts @@ -3,9 +3,7 @@ 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; - +export default class Paginationtest extends PaginatedCommand { constructor() { super(); @@ -13,8 +11,6 @@ export default class Paginationtest extends PaginatedCommand { .setName('paginationtest') .setDescription('Test the pagination functionality'); - this.paginatedEmbed = new PaginatedEmbed("paginationtest"); - this.InitialisePages(); } @@ -24,18 +20,6 @@ export default class Paginationtest extends PaginatedCommand { 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); diff --git a/src/type/paginatedCommand.ts b/src/type/paginatedCommand.ts index 6132175..ca5ace6 100644 --- a/src/type/paginatedCommand.ts +++ b/src/type/paginatedCommand.ts @@ -1,14 +1,27 @@ -import { ButtonInteraction, CommandInteraction } from "discord.js"; +import { ButtonInteraction, CacheType } from "discord.js"; import { Command } from "./command"; +import PaginatedEmbed from "../helpers/PaginatedEmbed"; +import EmbedPage from "./embedPage"; -export class PaginatedCommand extends Command { +export class PaginatedCommand extends Command { public CommandBuilder: any; + public paginatedEmbed: PaginatedEmbed; - public async nextPage(interaction: ButtonInteraction) { + constructor() { + super(); + this.paginatedEmbed = new PaginatedEmbed("paginationtest"); } - public async previousPage(interaction: ButtonInteraction) { + public async nextPage(interaction: ButtonInteraction) { + const page = this.paginatedEmbed.NextPage(); + await page!.sendButtonEmbed(interaction); + } + + public async previousPage(interaction: ButtonInteraction) { + const page = this.paginatedEmbed.PreviousPage(); + + await page!.sendButtonEmbed(interaction); } }