From 673b258fa999eea7aec55cb94ef83becafcd211c Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 4 Jul 2024 19:21:52 +0100 Subject: [PATCH] Add pagination buttons --- src/commands/304276391837302787/moons.ts | 6 +++- src/commands/304276391837302787/moons/list.ts | 32 +++++++++++++++---- src/constants/MoonValues.ts | 3 ++ src/database/entities/Moon.ts | 16 ++++++++++ 4 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 src/constants/MoonValues.ts diff --git a/src/commands/304276391837302787/moons.ts b/src/commands/304276391837302787/moons.ts index 5c8fc85..13f0f98 100644 --- a/src/commands/304276391837302787/moons.ts +++ b/src/commands/304276391837302787/moons.ts @@ -16,7 +16,11 @@ export default class Moons extends Command { .addUserOption(option => option .setName("user") - .setDescription("The user to view (Defaults to yourself)"))); + .setDescription("The user to view (Defaults to yourself)")) + .addNumberOption(option => + option + .setName("page") + .setDescription("The page to start with"))); } public override async execute(interaction: CommandInteraction) { diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts index b359f2f..f568ff5 100644 --- a/src/commands/304276391837302787/moons/list.ts +++ b/src/commands/304276391837302787/moons/list.ts @@ -1,18 +1,22 @@ -import {CommandInteraction, EmbedBuilder} from "discord.js"; +import {ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder} from "discord.js"; import Moon from "../../../database/entities/Moon"; import EmbedColours from "../../../constants/EmbedColours"; +import MoonValues from "../../../constants/MoonValues"; export default async function ListMoons(interaction: CommandInteraction) { const user = interaction.options.get("user")?.user ?? interaction.user; + const page = interaction.options.get("page")?.value as number ?? 0; - const moons = await Moon.FetchMoonsByUserId(user.id); + const moons = await Moon.FetchPaginatedMoonsByUserId(user.id, page); - if (!moons || moons.length == 0) { - await interaction.reply(`${user.username} does not have any moons.`); + if (!moons || moons[0].length == 0) { + await interaction.reply(`${user.username} does not have any moons or page is invalid.`); return; } - const description = moons.flatMap(x => `${x.MoonNumber}. ${x.Description.slice(0, 15)}`); + const totalPages = Math.ceil(moons[1] / MoonValues.ListPageLength); + + const description = moons[0].flatMap(x => `${x.MoonNumber}. ${x.Description.slice(0, 15)}`); const embed = new EmbedBuilder() .setTitle(`${user.username}'s Moons`) @@ -20,5 +24,21 @@ export default async function ListMoons(interaction: CommandInteraction) { .setDescription(description.join("\n")) .setFooter({ text: `${moons.length} moons` }); - await interaction.reply({ embeds: [ embed ] }); + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(`moons ${user} ${page - 1}`) + .setLabel("Previous") + .setStyle(ButtonStyle.Primary) + .setDisabled(page == 0), + new ButtonBuilder() + .setCustomId(`moons ${user.id} ${page + 1}`) + .setLabel("Next") + .setStyle(ButtonStyle.Primary) + .setDisabled(page + 1 == totalPages)); + + await interaction.reply({ + embeds: [ embed ], + components: [ row ], + }); } diff --git a/src/constants/MoonValues.ts b/src/constants/MoonValues.ts new file mode 100644 index 0000000..52b49c8 --- /dev/null +++ b/src/constants/MoonValues.ts @@ -0,0 +1,3 @@ +export default class MoonValues { + public static readonly ListPageLength = 10; +} diff --git a/src/database/entities/Moon.ts b/src/database/entities/Moon.ts index feb2cbb..e246f8c 100644 --- a/src/database/entities/Moon.ts +++ b/src/database/entities/Moon.ts @@ -1,6 +1,7 @@ import { Column, Entity } from "typeorm"; import BaseEntity from "../../contracts/BaseEntity"; import AppDataSource from "../dataSources/appDataSource"; +import MoonValues from "../../constants/MoonValues"; @Entity() export default class Moon extends BaseEntity { @@ -28,4 +29,19 @@ export default class Moon extends BaseEntity { return all; } + + public static async FetchPaginatedMoonsByUserId(userId: string, page: number): Promise<[ Moon[], number ]> { + const rangeStart = page * MoonValues.ListPageLength; + + const repository = AppDataSource.getRepository(Moon); + + const moons = await repository.findAndCount({ + where: { UserId: userId }, + order: { MoonNumber: "ASC" }, + skip: rangeStart, + take: MoonValues.ListPageLength, + }); + + return moons; + } }