From 09e264a3fafa4d54d69d059cc15c6d8668c443ce Mon Sep 17 00:00:00 2001 From: JOLIMAITRE Matthieu Date: Wed, 1 May 2024 17:27:40 +0200 Subject: [PATCH] split autocompletion --- src/lib/commands/commands.ts | 74 ++++++++++++++++++++---------------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/src/lib/commands/commands.ts b/src/lib/commands/commands.ts index 23e00c9..c8f45a5 100644 --- a/src/lib/commands/commands.ts +++ b/src/lib/commands/commands.ts @@ -3,6 +3,7 @@ import { ChatInputCommandInteraction, EmbedBuilder, SlashCommandBuilder, + SlashCommandSubcommandBuilder, TextChannel, } from "npm:discord.js"; @@ -12,8 +13,26 @@ import { _1d, _1min, Channel, collect, days_to_ms, log_from, trimmed } from "../ const log = log_from(import.meta); export function declare_commands(subjects: Set) { + return [devoir_command(subjects), adm_command()]; +} + +function adm_command() { + return new SlashCommandBuilder().setName("adm") + .setDescription("Commandes d'administration.") + .addSubcommand((command) => + command.setName("ajouter-feed") + .setDescription("Ajoute un salon comme feed de notifications.") + .addChannelOption((option) => + option.setName("salon") + .setDescription("Salon du nouveau feed.") + .setRequired(true) + ) + ); +} + +function devoir_command(subjects: Set) { const subjects_as_choices = [...subjects.values()].map((name) => ({ name, value: name })); - const devoir_command = new SlashCommandBuilder() + return new SlashCommandBuilder() .setName("devoir") .setDescription("Manipuler les devoirs à effectuer.") .addSubcommand((command) => @@ -72,20 +91,10 @@ export function declare_commands(subjects: Set) { .setRequired(false) ) ); - const adm_command = new SlashCommandBuilder().setName("adm") - .setDescription("Commandes d'administration.") - .addSubcommand((command) => - command.setName("ajouter-feed") - .setDescription("Ajoute un salon comme feed de notifications.") - .addChannelOption((option) => - option.setName("salon") - .setDescription("Salon du nouveau feed.") - .setRequired(true) - ) - ); - return [devoir_command, adm_command]; } +// handling + export async function handle_command( interaction: ChatInputCommandInteraction, storage: Storage, @@ -159,27 +168,26 @@ export async function handle_command( log("Unknown command", interaction.commandName); } +// autocompletion + export async function handle_autocomplete(interaction: AutocompleteInteraction, storage: Storage) { log("Auto completing."); - if (interaction.commandName === "devoir") { - const subcommand = interaction.options.getSubcommand(true); - if (subcommand === "retirer") { - const devoirs = await collect(storage.devoirs.list()); - const mapped = devoirs.map(([{ id }, value]) => ({ - name: trimmed(`[${value.subject}] ${value.description}`, 100), - value: id, - })); - return await interaction.respond(mapped); - } - if (subcommand === "éditer") { - const devoirs = await collect(storage.devoirs.list()); - const mapped = devoirs.map(([{ id }, value]) => ({ - name: trimmed(`[${value.subject}] ${value.description}`, 100), - value: id, - })); - return await interaction.respond(mapped); - } - return log("Unknown devoir sub command", subcommand); - } + if (interaction.commandName === "devoir") return await handle_autocomplete_devoir(interaction, storage); log("Unknown command", interaction.commandName); } + +async function handle_autocomplete_devoir(interaction: AutocompleteInteraction, storage: Storage) { + const subcommand = interaction.options.getSubcommand(true); + if (subcommand === "retirer") return await autocomplete_with_devoirs(storage, interaction); + if (subcommand === "éditer") return await autocomplete_with_devoirs(storage, interaction); + log("Unknown devoir sub command", subcommand); +} + +async function autocomplete_with_devoirs(storage: Storage, interaction: AutocompleteInteraction) { + const devoirs = await collect(storage.devoirs.list()); + const mapped = devoirs.map(([{ id }, { subject, description }]) => ({ + name: trimmed(`[${subject}] ${description}`, 100), + value: id, + })); + await interaction.respond(mapped); +}