From dc3cb936537326b868d76235e2917d05b4828954 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Wed, 14 Aug 2024 18:21:12 +0100 Subject: [PATCH] Add moon button event --- src/buttonEvents/moons.ts | 14 +++++ src/buttonEvents/moons/list.ts | 54 +++++++++++++++++++ src/commands/304276391837302787/moons/list.ts | 4 +- src/registry.ts | 2 + 4 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 src/buttonEvents/moons.ts create mode 100644 src/buttonEvents/moons/list.ts diff --git a/src/buttonEvents/moons.ts b/src/buttonEvents/moons.ts new file mode 100644 index 0000000..373e30c --- /dev/null +++ b/src/buttonEvents/moons.ts @@ -0,0 +1,14 @@ +import {ButtonInteraction} from "discord.js"; +import {ButtonEvent} from "../type/buttonEvent"; +import List from "./moons/list"; + +export default class Moons extends ButtonEvent { + public override async execute(interaction: ButtonInteraction): Promise { + const action = interaction.customId.split(" ")[0]; + + switch (action) { + case "list": + await List(interaction); + } + } +} diff --git a/src/buttonEvents/moons/list.ts b/src/buttonEvents/moons/list.ts new file mode 100644 index 0000000..fe5ae21 --- /dev/null +++ b/src/buttonEvents/moons/list.ts @@ -0,0 +1,54 @@ +import {ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, EmbedBuilder} from "discord.js"; +import Moon from "../../database/entities/Moon"; +import EmbedColours from "../../constants/EmbedColours"; + +export default async function List(interaction: ButtonInteraction) { + if (!interaction.guild) return; + + const userId = interaction.customId.split(" ")[1]; + const page = interaction.customId.split(" ")[2]; + + if (!userId || !page) return; + if (!Number(page)) return; + + const pageNumber = Number(page); + + const member = interaction.guild.members.cache.find(x => x.user.id == userId); + + const pageLength = 10; + + const moons = await Moon.FetchPaginatedMoonsByUserId(userId, pageLength, pageNumber); + + if (!moons || moons[0].length == 0) { + await interaction.reply(`${member?.user.username ?? "This user"} does not have any moons or page is invalid.`); + return; + } + + const totalPages = Math.ceil(moons[1] / pageLength); + + const description = moons[0].flatMap(x => `${x.MoonNumber}. ${x.Description.slice(0, 15)}`); + + const embed = new EmbedBuilder() + .setTitle(`${member?.user.username}'s Moons`) + .setColor(EmbedColours.Ok) + .setDescription(description.join("\n")) + .setFooter({ text: `${moons.length} moons` }); + + const row = new ActionRowBuilder() + .addComponents( + new ButtonBuilder() + .setCustomId(`moons list ${userId} ${pageNumber - 1}`) + .setLabel("Previous") + .setStyle(ButtonStyle.Primary) + .setDisabled(pageNumber == 0), + new ButtonBuilder() + .setCustomId(`moons list ${userId} ${pageNumber + 1}`) + .setLabel("Next") + .setStyle(ButtonStyle.Primary) + .setDisabled(pageNumber + 1 == totalPages)); + + await interaction.update({ + embeds: [ embed ], + components: [ row ], + }); +} diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts index 35a14d6..5cddffb 100644 --- a/src/commands/304276391837302787/moons/list.ts +++ b/src/commands/304276391837302787/moons/list.ts @@ -28,12 +28,12 @@ export default async function ListMoons(interaction: CommandInteraction) { const row = new ActionRowBuilder() .addComponents( new ButtonBuilder() - .setCustomId(`moons ${user} ${page - 1}`) + .setCustomId(`moons list ${user.id} ${page - 1}`) .setLabel("Previous") .setStyle(ButtonStyle.Primary) .setDisabled(page == 0), new ButtonBuilder() - .setCustomId(`moons ${user.id} ${page + 1}`) + .setCustomId(`moons list ${user.id} ${page + 1}`) .setLabel("Next") .setStyle(ButtonStyle.Primary) .setDisabled(page + 1 == totalPages)); diff --git a/src/registry.ts b/src/registry.ts index bfdd0ba..6c80edb 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -41,6 +41,7 @@ import MessageCreate from "./events/MessageEvents/MessageCreate"; // Button Event Imports import Verify from "./buttonEvents/verify"; +import MoonsButtonEvent from "./buttonEvents/moons"; export default class Registry { public static RegisterCommands() { @@ -95,5 +96,6 @@ export default class Registry { public static RegisterButtonEvents() { CoreClient.RegisterButtonEvent("verify", new Verify()); + CoreClient.RegisterButtonEvent("moons", new MoonsButtonEvent()); } }