diff --git a/src/lib/commands/commands.ts b/src/lib/commands/commands.ts index c8f45a5..6f1fa8b 100644 --- a/src/lib/commands/commands.ts +++ b/src/lib/commands/commands.ts @@ -3,7 +3,6 @@ import { ChatInputCommandInteraction, EmbedBuilder, SlashCommandBuilder, - SlashCommandSubcommandBuilder, TextChannel, } from "npm:discord.js"; @@ -101,51 +100,7 @@ export async function handle_command( update_display: Channel, ) { log("Received command", interaction.commandName); - if (interaction.commandName === "devoir") { - const subcommand = interaction.options.getSubcommand(true); - if (subcommand === "ajouter") { - const subject = interaction.options.getString("matière", true); - const days = interaction.options.getNumber("jours", true); - const description = interaction.options.getString("description", true); - const date = Date.now() + days_to_ms(days); - const { id } = await storage.devoirs.add({ date, description, subject }); - const embed = new EmbedBuilder() - .setTitle("`🌟` Ajouté") - .setDescription(`Nouveau devoir ajouté avec succès.`) - .setFooter({ text: `(devoir id:${id})` }); - await interaction.reply({ embeds: [embed], ephemeral: true }); - update_display.send(); - return; - } - if (subcommand === "retirer") { - const id = interaction.options.getString("devoir", true); - await storage.devoirs.delete({ id }); - const embed = new EmbedBuilder() - .setTitle("`🗑️` Retiré") - .setDescription(`Devoir supprimé avec succès.`) - .setFooter({ text: `(devoir id:${id})` }); - await interaction.reply({ embeds: [embed], ephemeral: true }); - update_display.send(); - return; - } - if (subcommand === "éditer") { - const id = interaction.options.getString("devoir", true); - const subject = interaction.options.getString("matière", false); - if (subject !== null) storage.devoirs.update({ id }, (d) => d.subject = subject); - const description = interaction.options.getString("description", false); - if (description !== null) storage.devoirs.update({ id }, (d) => d.description = description); - const days = interaction.options.getNumber("jours", false); - if (days !== null) storage.devoirs.update({ id }, (d) => d.date = Date.now() + days_to_ms(days)); - const embed = new EmbedBuilder() - .setTitle("`🔧` Édité") - .setDescription(`Devoir mis à jour avec succès.`) - .setFooter({ text: `(devoir id:${id})` }); - await interaction.reply({ embeds: [embed], ephemeral: true }); - update_display.send(); - return; - } - return log("Unknown devoir sub command", subcommand); - } + if (interaction.commandName === "devoir") return await handle_command_devoir(interaction, storage, update_display); if (interaction.commandName === "adm") { const subcommand = interaction.options.getSubcommand(true); if (subcommand === "ajouter-feed") { @@ -168,6 +123,71 @@ export async function handle_command( log("Unknown command", interaction.commandName); } +async function handle_command_devoir( + interaction: ChatInputCommandInteraction, + storage: Storage, + update_display: Channel, +) { + const subcommand = interaction.options.getSubcommand(true); + if (subcommand === "ajouter") return await handle_command_devoir_ajouter(interaction, storage, update_display); + if (subcommand === "retirer") return await handle_command_devoir_retirer(interaction, storage, update_display); + if (subcommand === "éditer") return await handle_command_devoir_editer(interaction, storage, update_display); + return log("Unknown devoir sub command", subcommand); +} + +async function handle_command_devoir_editer( + interaction: ChatInputCommandInteraction, + storage: Storage, + update_display: Channel, +) { + const id = interaction.options.getString("devoir", true); + const subject = interaction.options.getString("matière", false); + if (subject !== null) storage.devoirs.update({ id }, (d) => d.subject = subject); + const description = interaction.options.getString("description", false); + if (description !== null) storage.devoirs.update({ id }, (d) => d.description = description); + const days = interaction.options.getNumber("jours", false); + if (days !== null) storage.devoirs.update({ id }, (d) => d.date = Date.now() + days_to_ms(days)); + const embed = new EmbedBuilder() + .setTitle("`🔧` Édité") + .setDescription(`Devoir mis à jour avec succès.`) + .setFooter({ text: `(devoir id:${id})` }); + await interaction.reply({ embeds: [embed], ephemeral: true }); + update_display.send(); +} + +async function handle_command_devoir_retirer( + interaction: ChatInputCommandInteraction, + storage: Storage, + update_display: Channel, +) { + const id = interaction.options.getString("devoir", true); + await storage.devoirs.delete({ id }); + const embed = new EmbedBuilder() + .setTitle("`🗑️` Retiré") + .setDescription(`Devoir supprimé avec succès.`) + .setFooter({ text: `(devoir id:${id})` }); + await interaction.reply({ embeds: [embed], ephemeral: true }); + update_display.send(); +} + +async function handle_command_devoir_ajouter( + interaction: ChatInputCommandInteraction, + storage: Storage, + update_display: Channel, +) { + const subject = interaction.options.getString("matière", true); + const days = interaction.options.getNumber("jours", true); + const description = interaction.options.getString("description", true); + const date = Date.now() + days_to_ms(days); + const { id } = await storage.devoirs.add({ date, description, subject }); + const embed = new EmbedBuilder() + .setTitle("`🌟` Ajouté") + .setDescription(`Nouveau devoir ajouté avec succès.`) + .setFooter({ text: `(devoir id:${id})` }); + await interaction.reply({ embeds: [embed], ephemeral: true }); + update_display.send(); +} + // autocompletion export async function handle_autocomplete(interaction: AutocompleteInteraction, storage: Storage) {