WIP: Split up moon counter from the database #489

Draft
Vylpes wants to merge 16 commits from feature/300-moon-set into develop
3 changed files with 21 additions and 3 deletions
Showing only changes of commit 476ff7cfc4 - Show all commits

View file

@ -1,6 +1,7 @@
import {ActionRowBuilder, ButtonBuilder, ButtonInteraction, ButtonStyle, EmbedBuilder} from "discord.js";
import Moon from "../../database/entities/304276391837302787/Moon";
import EmbedColours from "../../constants/EmbedColours";
import UserSetting from "../../database/entities/UserSetting";
export default async function List(interaction: ButtonInteraction) {
if (!interaction.guild) return;
@ -23,6 +24,9 @@ export default async function List(interaction: ButtonInteraction) {
return;
}
const moonSetting = await UserSetting.FetchOneByKey(userId, "moons");
const totalMoons = moonSetting && Number(moonSetting.Value) ? Number(moonSetting.Value) : 0;
const totalPages = Math.ceil(moons[1] / pageLength);
const description = moons[0].flatMap(x => `**${x.MoonNumber} -** ${x.Description.slice(0, 15)}`);
@ -31,7 +35,7 @@ export default async function List(interaction: ButtonInteraction) {
.setTitle(`${member?.user.username}'s Moons`)
.setColor(EmbedColours.Ok)
.setDescription(description.join("\n"))
.setFooter({ text: `Page ${page + 1} of ${totalPages} · ${moons[1]} moons` });
.setFooter({ text: `Page ${pageNumber + 1} of ${totalPages} · ${totalMoons} moons` });
const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(

View file

@ -1,6 +1,7 @@
import {CommandInteraction, EmbedBuilder} from "discord.js";
import Moon from "../../../database/entities/304276391837302787/Moon";
import EmbedColours from "../../../constants/EmbedColours";
import UserSetting from "../../../database/entities/UserSetting";
export default async function AddMoon(interaction: CommandInteraction) {
const description = interaction.options.get("description", true).value?.toString();
@ -10,7 +11,16 @@ export default async function AddMoon(interaction: CommandInteraction) {
return;
}
const moonCount = await Moon.FetchMoonCountByUserId(interaction.user.id);
let moonSetting = await UserSetting.FetchOneByKey(interaction.user.id, "moons");
const moonCount = moonSetting && Number(moonSetting.Value) ? Number(moonSetting.Value) : 0;
if (moonSetting) {
moonSetting.UpdateValue(`${moonCount + 1}`);
} else {
moonSetting = new UserSetting(interaction.user.id, "moons", `${moonCount + 1}`);
}
await moonSetting.Save(UserSetting, moonSetting);
const moon = new Moon(moonCount + 1, description, interaction.user.id);

View file

@ -1,6 +1,7 @@
import {ActionRowBuilder, ButtonBuilder, ButtonStyle, CommandInteraction, EmbedBuilder} from "discord.js";
import Moon from "../../../database/entities/304276391837302787/Moon";
import EmbedColours from "../../../constants/EmbedColours";
import UserSetting from "../../../database/entities/UserSetting";
export default async function ListMoons(interaction: CommandInteraction) {
const user = interaction.options.get("user")?.user ?? interaction.user;
@ -15,6 +16,9 @@ export default async function ListMoons(interaction: CommandInteraction) {
return;
}
const moonSetting = await UserSetting.FetchOneByKey(interaction.user.id, "moons");
const totalMoons = moonSetting && Number(moonSetting.Value) ? Number(moonSetting.Value) : 0;
const totalPages = Math.ceil(moons[1] / pageLength);
const description = moons[0].flatMap(x => `**${x.MoonNumber} -** ${x.Description.slice(0, 15)}`);
@ -23,7 +27,7 @@ export default async function ListMoons(interaction: CommandInteraction) {
.setTitle(`${user.username}'s Moons`)
.setColor(EmbedColours.Ok)
.setDescription(description.join("\n"))
.setFooter({ text: `Page ${page + 1} of ${totalPages} · ${moons[1]} moons` });
.setFooter({ text: `Page ${page + 1} of ${totalPages} · ${totalMoons} moons` });
const row = new ActionRowBuilder<ButtonBuilder>()
.addComponents(