Create base pagination helper
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Ethan Lane 2023-11-29 17:58:01 +00:00
parent e96f173533
commit a213e727c1
10 changed files with 194 additions and 5 deletions

View file

@ -7,7 +7,7 @@
# any secret values. # any secret values.
BOT_TOKEN= BOT_TOKEN=
BOT_VER=0.2.1 DEV BOT_VER=0.3.0 DEV
BOT_AUTHOR=Vylpes BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816 BOT_OWNERID=147392775707426816
BOT_CLIENTID=682942374040961060 BOT_CLIENTID=682942374040961060
@ -29,4 +29,4 @@ DB_CARD_FILE=:memory:
EXPRESS_PORT=3303 EXPRESS_PORT=3303
GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690 GDRIVESYNC_WHITELIST=147392775707426816,887272961504071690
GDRIVESYNC_AUTO=true GDRIVESYNC_AUTO=false

View file

@ -7,7 +7,7 @@
# any secret values. # any secret values.
BOT_TOKEN= BOT_TOKEN=
BOT_VER=0.2.1 BOT_VER=0.3.0
BOT_AUTHOR=Vylpes BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816 BOT_OWNERID=147392775707426816
BOT_CLIENTID=1093810443589529631 BOT_CLIENTID=1093810443589529631

View file

@ -7,7 +7,7 @@
# any secret values. # any secret values.
BOT_TOKEN= BOT_TOKEN=
BOT_VER=0.2.1 BETA BOT_VER=0.3.0 BETA
BOT_AUTHOR=Vylpes BOT_AUTHOR=Vylpes
BOT_OWNERID=147392775707426816 BOT_OWNERID=147392775707426816
BOT_CLIENTID=1147976642942214235 BOT_CLIENTID=1147976642942214235

View file

@ -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<CacheType>) {
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;
}
}
}

View file

@ -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<TestPage>;
constructor() {
super();
super.CommandBuilder = new SlashCommandBuilder()
.setName('paginationtest')
.setDescription('Test the pagination functionality');
this.paginatedEmbed = new PaginatedEmbed<TestPage>("paginationtest");
this.InitialisePages();
}
public override async execute(interaction: CommandInteraction<CacheType>) {
const page = this.paginatedEmbed.GetCurrentPage();
await page!.sendCommandEmbed(interaction);
}
public override async nextPage(interaction: ButtonInteraction<CacheType>) {
const page = this.paginatedEmbed.NextPage();
await page!.sendButtonEmbed(interaction);
}
public override async previousPage(interaction: ButtonInteraction<CacheType>) {
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);
}
}

View file

@ -0,0 +1,51 @@
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

@ -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}`);
}
}

View file

@ -1,4 +1,5 @@
import { CoreClient } from "./client/client"; import { CoreClient } from "./client/client";
import { Environment } from "./constants/Environment";
// Global Command Imports // Global Command Imports
import About from "./commands/about"; import About from "./commands/about";
@ -10,10 +11,12 @@ import Resync from "./commands/resync";
import Dropnumber from "./commands/stage/dropnumber"; import Dropnumber from "./commands/stage/dropnumber";
import Droprarity from "./commands/stage/droprarity"; import Droprarity from "./commands/stage/droprarity";
import Paginationtest from "./commands/dev/paginationtest";
// Button Event Imports // Button Event Imports
import Claim from "./buttonEvents/Claim"; import Claim from "./buttonEvents/Claim";
import Pagination from "./buttonEvents/Pagination";
import Reroll from "./buttonEvents/Reroll"; import Reroll from "./buttonEvents/Reroll";
import { Environment } from "./constants/Environment";
export default class Registry { export default class Registry {
public static RegisterCommands() { public static RegisterCommands() {
@ -26,6 +29,8 @@ export default class Registry {
// Test Commands // Test Commands
CoreClient.RegisterCommand('dropnumber', new Dropnumber(), Environment.Test); CoreClient.RegisterCommand('dropnumber', new Dropnumber(), Environment.Test);
CoreClient.RegisterCommand('droprarity', new Droprarity(), Environment.Test); CoreClient.RegisterCommand('droprarity', new Droprarity(), Environment.Test);
CoreClient.RegisterCommand('paginationtest', new Paginationtest(), Environment.Local);
} }
public static RegisterEvents() { public static RegisterEvents() {
@ -34,6 +39,7 @@ export default class Registry {
public static RegisterButtonEvents() { public static RegisterButtonEvents() {
CoreClient.RegisterButtonEvent('claim', new Claim()); CoreClient.RegisterButtonEvent('claim', new Claim());
CoreClient.RegisterButtonEvent('pagination', new Pagination());
CoreClient.RegisterButtonEvent('reroll', new Reroll()); CoreClient.RegisterButtonEvent('reroll', new Reroll());
} }
} }

39
src/type/embedPage.ts Normal file
View file

@ -0,0 +1,39 @@
import { ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, CommandInteraction, EmbedBuilder } from "discord.js";
export default class EmbedPage extends EmbedBuilder {
private row: ActionRowBuilder<ButtonBuilder>;
constructor(embedId: string, page: number, total: number) {
super();
this.setFooter({ text: `Page ${page} of ${total}`});
this.row = new ActionRowBuilder<ButtonBuilder>();
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 ],
});
}
}

View file

@ -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) {
}
}