From 4e734a9245258e578dfeeef15ff8c1b5ea3c5b72 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Thu, 19 Sep 2024 19:23:01 +0100 Subject: [PATCH] Add /moons add command --- src/commands/304276391837302787/moons.ts | 15 ++++++++++- src/commands/304276391837302787/moons/add.ts | 26 ++++++++++++++++++++ src/constants/EmbedColours.ts | 1 + src/database/entities/Moon.ts | 8 ++++++ 4 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 src/commands/304276391837302787/moons/add.ts diff --git a/src/commands/304276391837302787/moons.ts b/src/commands/304276391837302787/moons.ts index 13f0f98..dc884a0 100644 --- a/src/commands/304276391837302787/moons.ts +++ b/src/commands/304276391837302787/moons.ts @@ -1,6 +1,7 @@ import { Command } from "../../type/command"; import { CommandInteraction, SlashCommandBuilder } from "discord.js"; import ListMoons from "./moons/list"; +import AddMoon from "./moons/add"; export default class Moons extends Command { constructor() { @@ -20,7 +21,16 @@ export default class Moons extends Command { .addNumberOption(option => option .setName("page") - .setDescription("The page to start with"))); + .setDescription("The page to start with"))) + .addSubcommand(subcommand => + subcommand + .setName('add') + .setDescription('Add a moon to your count!') + .addStringOption(option => + option + .setName("description") + .setDescription("What deserved a moon?") + .setRequired(true))); } public override async execute(interaction: CommandInteraction) { @@ -30,6 +40,9 @@ export default class Moons extends Command { case "list": await ListMoons(interaction); break; + case "add": + await AddMoon(interaction); + break; } } } diff --git a/src/commands/304276391837302787/moons/add.ts b/src/commands/304276391837302787/moons/add.ts new file mode 100644 index 0000000..3a68a14 --- /dev/null +++ b/src/commands/304276391837302787/moons/add.ts @@ -0,0 +1,26 @@ +import {CommandInteraction, EmbedBuilder} from "discord.js"; +import Moon from "../../../database/entities/Moon"; +import EmbedColours from "../../../constants/EmbedColours"; + +export default async function AddMoon(interaction: CommandInteraction) { + const description = interaction.options.get("description", true).value?.toString(); + + if (!description || description.length > 255) { + await interaction.reply("Name must be less than 255 characters!"); + return; + } + + const moonCount = await Moon.FetchMoonCountByUserId(interaction.user.id); + + const moon = new Moon(moonCount + 1, description, interaction.user.id); + + await moon.Save(Moon, moon); + + const embed = new EmbedBuilder() + .setTitle(`${interaction.user.globalName} Got A Moon!`) + .setColor(EmbedColours.Moon) + .setDescription(`${moon.MoonNumber}. ${moon.Description}`) + .setThumbnail("https://cdn.discordapp.com/emojis/374131312182689793.webp?size=96&quality=lossless"); + + await interaction.reply({ embeds: [ embed ] }); +} diff --git a/src/constants/EmbedColours.ts b/src/constants/EmbedColours.ts index 023c77a..b15429c 100644 --- a/src/constants/EmbedColours.ts +++ b/src/constants/EmbedColours.ts @@ -1,3 +1,4 @@ export default class EmbedColours { public static readonly Ok = 0x3050ba; + public static readonly Moon = 0x50C878; } \ No newline at end of file diff --git a/src/database/entities/Moon.ts b/src/database/entities/Moon.ts index 566d1d4..24fdbf1 100644 --- a/src/database/entities/Moon.ts +++ b/src/database/entities/Moon.ts @@ -46,4 +46,12 @@ export default class Moon extends BaseEntity { return moons; } + + public static async FetchMoonCountByUserId(userId: string): Promise { + const repository = AppDataSource.getRepository(Moon); + + const count = await repository.count({ where: { UserId: userId } }); + + return count; + } }