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 50 additions and 7 deletions
Showing only changes of commit 673b258fa9 - Show all commits

View file

@ -16,7 +16,11 @@ export default class Moons extends Command {
.addUserOption(option => .addUserOption(option =>
option option
.setName("user") .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) { public override async execute(interaction: CommandInteraction) {

View file

@ -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 Moon from "../../../database/entities/Moon";
import EmbedColours from "../../../constants/EmbedColours"; import EmbedColours from "../../../constants/EmbedColours";
import MoonValues from "../../../constants/MoonValues";
export default async function ListMoons(interaction: CommandInteraction) { export default async function ListMoons(interaction: CommandInteraction) {
const user = interaction.options.get("user")?.user ?? interaction.user; 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) { if (!moons || moons[0].length == 0) {
await interaction.reply(`${user.username} does not have any moons.`); await interaction.reply(`${user.username} does not have any moons or page is invalid.`);
return; 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() const embed = new EmbedBuilder()
.setTitle(`${user.username}'s Moons`) .setTitle(`${user.username}'s Moons`)
@ -20,5 +24,21 @@ export default async function ListMoons(interaction: CommandInteraction) {
.setDescription(description.join("\n")) .setDescription(description.join("\n"))
.setFooter({ text: `${moons.length} moons` }); .setFooter({ text: `${moons.length} moons` });
await interaction.reply({ embeds: [ embed ] }); const row = new ActionRowBuilder<ButtonBuilder>()
.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 ],
});
} }

View file

@ -0,0 +1,3 @@
export default class MoonValues {
Vylpes marked this conversation as resolved Outdated

Maybe call this MoonConstants? I think that would make more sense

Maybe call this `MoonConstants`? I think that would make more sense
public static readonly ListPageLength = 10;
}

View file

@ -1,6 +1,7 @@
import { Column, Entity } from "typeorm"; import { Column, Entity } from "typeorm";
import BaseEntity from "../../contracts/BaseEntity"; import BaseEntity from "../../contracts/BaseEntity";
import AppDataSource from "../dataSources/appDataSource"; import AppDataSource from "../dataSources/appDataSource";
import MoonValues from "../../constants/MoonValues";
@Entity() @Entity()
export default class Moon extends BaseEntity { export default class Moon extends BaseEntity {
@ -28,4 +29,19 @@ export default class Moon extends BaseEntity {
return all; return all;
} }
public static async FetchPaginatedMoonsByUserId(userId: string, page: number): Promise<[ Moon[], number ]> {
const rangeStart = page * MoonValues.ListPageLength;
Vylpes marked this conversation as resolved Outdated

ListPageLength could just be a parameter tbh

`ListPageLength` could just be a parameter tbh

Or we make it a global page length, affecting all future pages?

Or we make it a global page length, affecting all future pages?

I think just make it a parameter, the issue with that would be adjusting it for 1 and having unintended changes on other pages

I think just make it a parameter, the issue with that would be adjusting it for 1 and having unintended changes on other pages
const repository = AppDataSource.getRepository(Moon);
const moons = await repository.findAndCount({
where: { UserId: userId },
order: { MoonNumber: "ASC" },
skip: rangeStart,
take: MoonValues.ListPageLength,
});
return moons;
}
} }