split autocompletion
This commit is contained in:
parent
a3178942af
commit
09e264a3fa
1 changed files with 41 additions and 33 deletions
|
@ -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<string>) {
|
||||
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<string>) {
|
||||
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<string>) {
|
|||
.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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue