refactor organize command exports

This commit is contained in:
JOLIMAITRE Matthieu 2024-05-01 17:39:52 +02:00
parent b759f96dde
commit dbbc99dab7
2 changed files with 43 additions and 35 deletions

View file

@ -8,13 +8,35 @@ import {
import { Storage } from "../storage.ts"; import { Storage } from "../storage.ts";
import { _1d, _1min, Channel, collect, days_to_ms, log_from, trimmed } from "../utils.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); const log = log_from(import.meta);
// exported
export function declare_commands(subjects: Set<string>) { export function declare_commands(subjects: Set<string>) {
return [devoir_command(subjects), adm_command()]; 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() { function adm_command() {
return new SlashCommandBuilder().setName("adm") return new SlashCommandBuilder().setName("adm")
.setDescription("Commandes d'administration.") .setDescription("Commandes d'administration.")
@ -94,35 +116,6 @@ function devoir_command(subjects: Set<string>) {
// handling // 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( async function handle_command_devoir(
interaction: ChatInputCommandInteraction, interaction: ChatInputCommandInteraction,
storage: Storage, storage: Storage,
@ -188,14 +181,27 @@ async function handle_command_devoir_ajouter(
update_display.send(); update_display.send();
} }
// autocompletion async function handle_command_adm(interaction: ChatInputCommandInteraction, storage: Storage) {
const subcommand = interaction.options.getSubcommand(true);
export async function handle_autocomplete(interaction: AutocompleteInteraction, storage: Storage) { if (subcommand === "ajouter-feed") return await handle_command_adm_ajouter_feed(interaction, storage);
log("Auto completing."); return log("Unknown adm sub command", subcommand);
if (interaction.commandName === "devoir") return await handle_autocomplete_devoir(interaction, storage);
log("Unknown command", interaction.commandName);
} }
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) { async function handle_autocomplete_devoir(interaction: AutocompleteInteraction, storage: Storage) {
const subcommand = interaction.options.getSubcommand(true); const subcommand = interaction.options.getSubcommand(true);
if (subcommand === "retirer") return await autocomplete_with_devoirs(storage, interaction); if (subcommand === "retirer") return await autocomplete_with_devoirs(storage, interaction);

View file

@ -53,6 +53,8 @@ export function trimmed(text: string, width: number) {
else return text.slice(0, width - 4) + "..."; else return text.slice(0, width - 4) + "...";
} }
export function voids<T>(_: T) {}
export const _1min = 60 * 1000; export const _1min = 60 * 1000;
export const _1h = 60 * _1min; export const _1h = 60 * _1min;
export const _1d = 24 * _1h; export const _1d = 24 * _1h;