diff --git a/src/buttonEvents/Pagination.ts b/src/buttonEvents/Pagination.ts index d39371c..225e2a8 100644 --- a/src/buttonEvents/Pagination.ts +++ b/src/buttonEvents/Pagination.ts @@ -7,18 +7,11 @@ import EmbedPage from "../type/embedPage"; 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 page = Number(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; - } + await command.sendButtonEmbed(interaction, page); } } \ No newline at end of file diff --git a/src/commands/dev/paginationtest.ts b/src/commands/dev/paginationtest.ts index 1a3d6ac..0121128 100644 --- a/src/commands/dev/paginationtest.ts +++ b/src/commands/dev/paginationtest.ts @@ -1,5 +1,4 @@ -import { ButtonInteraction, CacheType, CommandInteraction, SlashCommandBuilder } from "discord.js"; -import PaginatedEmbed from "../../helpers/PaginatedEmbed"; +import { CacheType, CommandInteraction, SlashCommandBuilder } from "discord.js"; import TestPage from "../../helpers/Pagination/TestPage"; import { PaginatedCommand } from "../../type/paginatedCommand"; @@ -15,9 +14,7 @@ export default class Paginationtest extends PaginatedCommand { } public override async execute(interaction: CommandInteraction) { - const page = this.paginatedEmbed.GetCurrentPage(); - - await page!.sendCommandEmbed(interaction); + await this.sendCommandEmbed(interaction, 0); } private InitialisePages() { @@ -25,6 +22,6 @@ export default class Paginationtest extends PaginatedCommand { const page1 = new TestPage("paginationtest", 1, 2); const page2 = new TestPage("paginationtest", 2, 2); - this.paginatedEmbed.AddPages(page0, page1, page2); + this.pages = [ page0, page1, page2 ]; } } \ No newline at end of file diff --git a/src/helpers/PaginatedEmbed.ts b/src/helpers/PaginatedEmbed.ts deleted file mode 100644 index bc5f98e..0000000 --- a/src/helpers/PaginatedEmbed.ts +++ /dev/null @@ -1,51 +0,0 @@ -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/type/embedPage.ts b/src/type/embedPage.ts index 0099aa1..627c4ce 100644 --- a/src/type/embedPage.ts +++ b/src/type/embedPage.ts @@ -1,7 +1,7 @@ -import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, CommandInteraction, EmbedBuilder } from "discord.js"; +import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder } from "discord.js"; export default class EmbedPage extends EmbedBuilder { - private row: ActionRowBuilder; + public row: ActionRowBuilder; constructor(embedId: string, page: number, total: number) { super(); @@ -12,29 +12,14 @@ export default class EmbedPage extends EmbedBuilder { this.row.addComponents( new ButtonBuilder() - .setCustomId(`pagination ${embedId} previous`) + .setCustomId(`pagination ${embedId} ${page - 1}`) .setLabel("◀️") .setStyle(ButtonStyle.Primary) .setDisabled(page == 0), new ButtonBuilder() - .setCustomId(`pagination ${embedId} next`) + .setCustomId(`pagination ${embedId} ${page + 1}`) .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.update({ - embeds: [ this ], - components: [ this.row ], - }); - return; - } } \ No newline at end of file diff --git a/src/type/paginatedCommand.ts b/src/type/paginatedCommand.ts index ca5ace6..be059b7 100644 --- a/src/type/paginatedCommand.ts +++ b/src/type/paginatedCommand.ts @@ -1,27 +1,27 @@ -import { ButtonInteraction, CacheType } from "discord.js"; +import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, CacheType, CommandInteraction, InteractionUpdateOptions } from "discord.js"; import { Command } from "./command"; -import PaginatedEmbed from "../helpers/PaginatedEmbed"; import EmbedPage from "./embedPage"; export class PaginatedCommand extends Command { public CommandBuilder: any; - public paginatedEmbed: PaginatedEmbed; + public pages: TPage[]; constructor() { super(); - - this.paginatedEmbed = new PaginatedEmbed("paginationtest"); } - public async nextPage(interaction: ButtonInteraction) { - const page = this.paginatedEmbed.NextPage(); - - await page!.sendButtonEmbed(interaction); + public async sendCommandEmbed(interaction: CommandInteraction, index: number) { + await interaction.reply({ + embeds: [ this.pages[index] ], + components: [ this.pages[index].row ], + }); } - public async previousPage(interaction: ButtonInteraction) { - const page = this.paginatedEmbed.PreviousPage(); - - await page!.sendButtonEmbed(interaction); + public async sendButtonEmbed(interaction: ButtonInteraction, index: number) { + await interaction.update({ + embeds: [ this.pages[index] ], + components: [ this.pages[index].row ], + }); + return; } }