Fix command not allowing multiple instances of the embeds

This commit is contained in:
Ethan Lane 2023-12-02 15:28:52 +00:00
parent d29a81c786
commit 0b6bb9a17d
5 changed files with 22 additions and 98 deletions

View file

@ -7,18 +7,11 @@ import EmbedPage from "../type/embedPage";
export default class Pagination<TPage extends EmbedPage> extends ButtonEvent { export default class Pagination<TPage extends EmbedPage> extends ButtonEvent {
public override async execute(interaction: ButtonInteraction<CacheType>) { public override async execute(interaction: ButtonInteraction<CacheType>) {
const embedId = interaction.customId.split(' ')[1]; 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 commandItem = CoreClient.commandItems.find(x => x.Name == embedId);
const command = commandItem?.Command as PaginatedCommand<TPage>; const command = commandItem?.Command as PaginatedCommand<TPage>;
switch (action) { await command.sendButtonEmbed(interaction, page);
case "next":
await command.nextPage(interaction);
break;
case "previous":
await command.previousPage(interaction);
break;
}
} }
} }

View file

@ -1,5 +1,4 @@
import { ButtonInteraction, CacheType, CommandInteraction, SlashCommandBuilder } from "discord.js"; import { CacheType, CommandInteraction, SlashCommandBuilder } from "discord.js";
import PaginatedEmbed from "../../helpers/PaginatedEmbed";
import TestPage from "../../helpers/Pagination/TestPage"; import TestPage from "../../helpers/Pagination/TestPage";
import { PaginatedCommand } from "../../type/paginatedCommand"; import { PaginatedCommand } from "../../type/paginatedCommand";
@ -15,9 +14,7 @@ export default class Paginationtest extends PaginatedCommand<TestPage> {
} }
public override async execute(interaction: CommandInteraction<CacheType>) { public override async execute(interaction: CommandInteraction<CacheType>) {
const page = this.paginatedEmbed.GetCurrentPage(); await this.sendCommandEmbed(interaction, 0);
await page!.sendCommandEmbed(interaction);
} }
private InitialisePages() { private InitialisePages() {
@ -25,6 +22,6 @@ export default class Paginationtest extends PaginatedCommand<TestPage> {
const page1 = new TestPage("paginationtest", 1, 2); const page1 = new TestPage("paginationtest", 1, 2);
const page2 = new TestPage("paginationtest", 2, 2); const page2 = new TestPage("paginationtest", 2, 2);
this.paginatedEmbed.AddPages(page0, page1, page2); this.pages = [ page0, page1, page2 ];
} }
} }

View file

@ -1,51 +0,0 @@
import EmbedPage from "../type/embedPage";
export default class PaginatedEmbed<TPage extends EmbedPage> {
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();
}
}

View file

@ -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 { export default class EmbedPage extends EmbedBuilder {
private row: ActionRowBuilder<ButtonBuilder>; public row: ActionRowBuilder<ButtonBuilder>;
constructor(embedId: string, page: number, total: number) { constructor(embedId: string, page: number, total: number) {
super(); super();
@ -12,29 +12,14 @@ export default class EmbedPage extends EmbedBuilder {
this.row.addComponents( this.row.addComponents(
new ButtonBuilder() new ButtonBuilder()
.setCustomId(`pagination ${embedId} previous`) .setCustomId(`pagination ${embedId} ${page - 1}`)
.setLabel("◀️") .setLabel("◀️")
.setStyle(ButtonStyle.Primary) .setStyle(ButtonStyle.Primary)
.setDisabled(page == 0), .setDisabled(page == 0),
new ButtonBuilder() new ButtonBuilder()
.setCustomId(`pagination ${embedId} next`) .setCustomId(`pagination ${embedId} ${page + 1}`)
.setLabel("▶️") .setLabel("▶️")
.setStyle(ButtonStyle.Primary) .setStyle(ButtonStyle.Primary)
.setDisabled(page == total)); .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;
}
} }

View file

@ -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 { Command } from "./command";
import PaginatedEmbed from "../helpers/PaginatedEmbed";
import EmbedPage from "./embedPage"; import EmbedPage from "./embedPage";
export class PaginatedCommand<TPage extends EmbedPage> extends Command { export class PaginatedCommand<TPage extends EmbedPage> extends Command {
public CommandBuilder: any; public CommandBuilder: any;
public paginatedEmbed: PaginatedEmbed<TPage>; public pages: TPage[];
constructor() { constructor() {
super(); super();
this.paginatedEmbed = new PaginatedEmbed<TPage>("paginationtest");
} }
public async nextPage(interaction: ButtonInteraction<CacheType>) { public async sendCommandEmbed(interaction: CommandInteraction, index: number) {
const page = this.paginatedEmbed.NextPage(); await interaction.reply({
embeds: [ this.pages[index] ],
await page!.sendButtonEmbed(interaction); components: [ this.pages[index].row ],
});
} }
public async previousPage(interaction: ButtonInteraction<CacheType>) { public async sendButtonEmbed(interaction: ButtonInteraction, index: number) {
const page = this.paginatedEmbed.PreviousPage(); await interaction.update({
embeds: [ this.pages[index] ],
await page!.sendButtonEmbed(interaction); components: [ this.pages[index].row ],
});
return;
} }
} }