diff --git a/src/lib/commands/commands.ts b/src/lib/commands/commands.ts index 6f1fa8b..8a591e2 100644 --- a/src/lib/commands/commands.ts +++ b/src/lib/commands/commands.ts @@ -8,13 +8,35 @@ import { import { Storage } from "../storage.ts"; import { _1d, _1min, Channel, collect, days_to_ms, log_from, trimmed } from "../utils.ts"; +import { voids } from "../utils.ts"; const log = log_from(import.meta); +// exported + export function declare_commands(subjects: Set) { return [devoir_command(subjects), adm_command()]; } +export async function handle_command( + interaction: ChatInputCommandInteraction, + storage: Storage, + update_display: Channel, +) { + log("Received command", interaction.commandName); + if (interaction.commandName === "devoir") return await handle_command_devoir(interaction, storage, update_display); + if (interaction.commandName === "adm") return await handle_command_adm(interaction, storage); + log("Unknown command", interaction.commandName); +} + +export async function handle_autocomplete(interaction: AutocompleteInteraction, storage: Storage) { + log("Auto completing."); + if (interaction.commandName === "devoir") return await handle_autocomplete_devoir(interaction, storage); + log("Unknown command", interaction.commandName); +} + +// declare + function adm_command() { return new SlashCommandBuilder().setName("adm") .setDescription("Commandes d'administration.") @@ -94,35 +116,6 @@ function devoir_command(subjects: Set) { // handling -export async function handle_command( - interaction: ChatInputCommandInteraction, - storage: Storage, - update_display: Channel, -) { - log("Received command", interaction.commandName); - 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") { - const channel = interaction.options.getChannel("salon", true); - const is_text_channel = channel instanceof TextChannel; - if (!is_text_channel) { - await interaction.reply("Channel must be text."); - return; - } - const board_message = await channel.send("[board]"); - const feed_id = await storage.feeds.add({ - channel_id: channel.id, - board_message_id: board_message.id, - notification_ids: new Set(), - }); - await interaction.reply("Added feed " + feed_id.id); - } - return log("Unknown adm sub command", subcommand); - } - log("Unknown command", interaction.commandName); -} - async function handle_command_devoir( interaction: ChatInputCommandInteraction, storage: Storage, @@ -188,14 +181,27 @@ async function handle_command_devoir_ajouter( update_display.send(); } -// autocompletion - -export async function handle_autocomplete(interaction: AutocompleteInteraction, storage: Storage) { - log("Auto completing."); - if (interaction.commandName === "devoir") return await handle_autocomplete_devoir(interaction, storage); - log("Unknown command", interaction.commandName); +async function handle_command_adm(interaction: ChatInputCommandInteraction, storage: Storage) { + const subcommand = interaction.options.getSubcommand(true); + if (subcommand === "ajouter-feed") return await handle_command_adm_ajouter_feed(interaction, storage); + return log("Unknown adm sub command", subcommand); } +async function handle_command_adm_ajouter_feed(interaction: ChatInputCommandInteraction, storage: Storage) { + const channel = interaction.options.getChannel("salon", true); + const is_text_channel = channel instanceof TextChannel; + if (!is_text_channel) return voids(await interaction.reply("Channel must be text.")); + const board_message = await channel.send("[board]"); + const feed_id = await storage.feeds.add({ + channel_id: channel.id, + board_message_id: board_message.id, + notification_ids: new Set(), + }); + await interaction.reply("Added feed " + feed_id.id); +} + +// autocompletion + 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); diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 4790637..78c65c7 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -53,6 +53,8 @@ export function trimmed(text: string, width: number) { else return text.slice(0, width - 4) + "..."; } +export function voids(_: T) {} + export const _1min = 60 * 1000; export const _1h = 60 * _1min; export const _1d = 24 * _1h;