Add list moons command #449

Merged
Vylpes merged 15 commits from feature/195-list-moons into develop 2024-08-17 16:47:14 +01:00
4 changed files with 72 additions and 2 deletions
Showing only changes of commit dc3cb93653 - Show all commits

14
src/buttonEvents/moons.ts Normal file
View file

@ -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<void> {
const action = interaction.customId.split(" ")[0];
switch (action) {
case "list":
await List(interaction);
}
}
}

View file

@ -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<ButtonBuilder>()
.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 ],
});
}

View file

@ -28,12 +28,12 @@ export default async function ListMoons(interaction: CommandInteraction) {
const row = new ActionRowBuilder<ButtonBuilder>() const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents( .addComponents(
new ButtonBuilder() new ButtonBuilder()
.setCustomId(`moons ${user} ${page - 1}`) .setCustomId(`moons list ${user.id} ${page - 1}`)
.setLabel("Previous") .setLabel("Previous")
.setStyle(ButtonStyle.Primary) .setStyle(ButtonStyle.Primary)
.setDisabled(page == 0), .setDisabled(page == 0),
new ButtonBuilder() new ButtonBuilder()
.setCustomId(`moons ${user.id} ${page + 1}`) .setCustomId(`moons list ${user.id} ${page + 1}`)
.setLabel("Next") .setLabel("Next")
.setStyle(ButtonStyle.Primary) .setStyle(ButtonStyle.Primary)
.setDisabled(page + 1 == totalPages)); .setDisabled(page + 1 == totalPages));

View file

@ -41,6 +41,7 @@ import MessageCreate from "./events/MessageEvents/MessageCreate";
// Button Event Imports // Button Event Imports
import Verify from "./buttonEvents/verify"; import Verify from "./buttonEvents/verify";
import MoonsButtonEvent from "./buttonEvents/moons";
export default class Registry { export default class Registry {
public static RegisterCommands() { public static RegisterCommands() {
@ -95,5 +96,6 @@ export default class Registry {
public static RegisterButtonEvents() { public static RegisterButtonEvents() {
CoreClient.RegisterButtonEvent("verify", new Verify()); CoreClient.RegisterButtonEvent("verify", new Verify());
CoreClient.RegisterButtonEvent("moons", new MoonsButtonEvent());
} }
} }