Add /moons add command

This commit is contained in:
Ethan Lane 2024-09-19 19:23:01 +01:00
parent 2b6223fa67
commit 4e734a9245
4 changed files with 49 additions and 1 deletions

View file

@ -1,6 +1,7 @@
import { Command } from "../../type/command"; import { Command } from "../../type/command";
import { CommandInteraction, SlashCommandBuilder } from "discord.js"; import { CommandInteraction, SlashCommandBuilder } from "discord.js";
import ListMoons from "./moons/list"; import ListMoons from "./moons/list";
import AddMoon from "./moons/add";
export default class Moons extends Command { export default class Moons extends Command {
constructor() { constructor() {
@ -20,7 +21,16 @@ export default class Moons extends Command {
.addNumberOption(option => .addNumberOption(option =>
option option
.setName("page") .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) { public override async execute(interaction: CommandInteraction) {
@ -30,6 +40,9 @@ export default class Moons extends Command {
case "list": case "list":
await ListMoons(interaction); await ListMoons(interaction);
break; break;
case "add":
await AddMoon(interaction);
break;
} }
} }
} }

View file

@ -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 ] });
}

View file

@ -1,3 +1,4 @@
export default class EmbedColours { export default class EmbedColours {
public static readonly Ok = 0x3050ba; public static readonly Ok = 0x3050ba;
public static readonly Moon = 0x50C878;
} }

View file

@ -46,4 +46,12 @@ export default class Moon extends BaseEntity {
return moons; return moons;
} }
public static async FetchMoonCountByUserId(userId: string): Promise<number> {
const repository = AppDataSource.getRepository(Moon);
const count = await repository.count({ where: { UserId: userId } });
return count;
}
} }