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 {
public override async execute(interaction: ButtonInteraction<CacheType>) {
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<TPage>;
switch (action) {
case "next":
await command.nextPage(interaction);
break;
case "previous":
await command.previousPage(interaction);
break;
}
await command.sendButtonEmbed(interaction, page);
}
}

View file

@ -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<TestPage> {
}
public override async execute(interaction: CommandInteraction<CacheType>) {
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<TestPage> {
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 ];
}
}

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 {
private row: ActionRowBuilder<ButtonBuilder>;
public row: ActionRowBuilder<ButtonBuilder>;
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;
}
}

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 PaginatedEmbed from "../helpers/PaginatedEmbed";
import EmbedPage from "./embedPage";
export class PaginatedCommand<TPage extends EmbedPage> extends Command {
public CommandBuilder: any;
public paginatedEmbed: PaginatedEmbed<TPage>;
public pages: TPage[];
constructor() {
super();
this.paginatedEmbed = new PaginatedEmbed<TPage>("paginationtest");
}
public async nextPage(interaction: ButtonInteraction<CacheType>) {
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<CacheType>) {
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;
}
}