Compare commits
No commits in common. "673b258fa999eea7aec55cb94ef83becafcd211c" and "690b63470a3d99cd98b0fd8824c390ef0b83d401" have entirely different histories.
673b258fa9
...
690b63470a
4 changed files with 8 additions and 57 deletions
|
@ -12,15 +12,7 @@ export default class Moons extends Command {
|
||||||
.addSubcommand(subcommand =>
|
.addSubcommand(subcommand =>
|
||||||
subcommand
|
subcommand
|
||||||
.setName('list')
|
.setName('list')
|
||||||
.setDescription('List moons you have obtained')
|
.setDescription('List moons you have obtained'));
|
||||||
.addUserOption(option =>
|
|
||||||
option
|
|
||||||
.setName("user")
|
|
||||||
.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) {
|
||||||
|
|
|
@ -1,44 +1,22 @@
|
||||||
import {ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder} from "discord.js";
|
import {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 moons = await Moon.FetchMoonsByUserId(interaction.user.id);
|
||||||
const page = interaction.options.get("page")?.value as number ?? 0;
|
|
||||||
|
|
||||||
const moons = await Moon.FetchPaginatedMoonsByUserId(user.id, page);
|
if (!moons || moons.length == 0) {
|
||||||
|
await interaction.reply("You do 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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalPages = Math.ceil(moons[1] / MoonValues.ListPageLength);
|
const description = moons.flatMap(x => `${x.MoonNumber}. ${x.Description.slice(0, 15)}`);
|
||||||
|
|
||||||
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(`${interaction.user.username}'s Moons`)
|
||||||
.setColor(EmbedColours.Ok)
|
.setColor(EmbedColours.Ok)
|
||||||
.setDescription(description.join("\n"))
|
.setDescription(description.join("\n"))
|
||||||
.setFooter({ text: `${moons.length} moons` });
|
.setFooter({ text: `${moons.length} moons` });
|
||||||
|
|
||||||
const row = new ActionRowBuilder<ButtonBuilder>()
|
await interaction.reply({ embeds: [ embed ] });
|
||||||
.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 ],
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
export default class MoonValues {
|
|
||||||
public static readonly ListPageLength = 10;
|
|
||||||
}
|
|
|
@ -1,7 +1,6 @@
|
||||||
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 {
|
||||||
|
@ -29,19 +28,4 @@ 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;
|
|
||||||
|
|
||||||
const repository = AppDataSource.getRepository(Moon);
|
|
||||||
|
|
||||||
const moons = await repository.findAndCount({
|
|
||||||
where: { UserId: userId },
|
|
||||||
order: { MoonNumber: "ASC" },
|
|
||||||
skip: rangeStart,
|
|
||||||
take: MoonValues.ListPageLength,
|
|
||||||
});
|
|
||||||
|
|
||||||
return moons;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue