From 476ff7cfc4257f5682d09c61da50378bf8b83dec Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Fri, 27 Sep 2024 18:15:23 +0100 Subject: [PATCH] Make moon count come from user setting table --- src/buttonEvents/moons/list.ts | 6 +++++- src/commands/304276391837302787/moons/add.ts | 12 +++++++++++- src/commands/304276391837302787/moons/list.ts | 6 +++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/buttonEvents/moons/list.ts b/src/buttonEvents/moons/list.ts index 36aa356..cf2c463 100644 --- a/src/buttonEvents/moons/list.ts +++ b/src/buttonEvents/moons/list.ts @@ -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() .addComponents( diff --git a/src/commands/304276391837302787/moons/add.ts b/src/commands/304276391837302787/moons/add.ts index 130aee3..5eb2cdd 100644 --- a/src/commands/304276391837302787/moons/add.ts +++ b/src/commands/304276391837302787/moons/add.ts @@ -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); diff --git a/src/commands/304276391837302787/moons/list.ts b/src/commands/304276391837302787/moons/list.ts index 838cd14..6d4c369 100644 --- a/src/commands/304276391837302787/moons/list.ts +++ b/src/commands/304276391837302787/moons/list.ts @@ -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() .addComponents(