Add list moons command (#449)
- Add concept of moons to the database - Add the list moons command to view a user's moons - This command is paginated using buttons #195 Reviewed-on: #449 Reviewed-by: VylpesTester <tester@vylpes.com> Co-authored-by: Ethan Lane <ethan@vylpes.com> Co-committed-by: Ethan Lane <ethan@vylpes.com>
This commit is contained in:
parent
8e87fd6618
commit
e2ec042fcd
10 changed files with 235 additions and 3 deletions
14
src/buttonEvents/moons.ts
Normal file
14
src/buttonEvents/moons.ts
Normal 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(" ")[1];
|
||||
|
||||
switch (action) {
|
||||
case "list":
|
||||
await List(interaction);
|
||||
}
|
||||
}
|
||||
}
|
53
src/buttonEvents/moons/list.ts
Normal file
53
src/buttonEvents/moons/list.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
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(" ")[2];
|
||||
const page = interaction.customId.split(" ")[3];
|
||||
|
||||
if (!userId || !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: `Page ${page + 1} of ${totalPages} · ${moons[1]} 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 ],
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue