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,
|
ChatInputCommandInteraction,
|
||||||
EmbedBuilder,
|
EmbedBuilder,
|
||||||
SlashCommandBuilder,
|
SlashCommandBuilder,
|
||||||
|
SlashCommandSubcommandBuilder,
|
||||||
TextChannel,
|
TextChannel,
|
||||||
} from "npm:discord.js";
|
} 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);
|
const log = log_from(import.meta);
|
||||||
|
|
||||||
export function declare_commands(subjects: Set<string>) {
|
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 subjects_as_choices = [...subjects.values()].map((name) => ({ name, value: name }));
|
||||||
const devoir_command = new SlashCommandBuilder()
|
return new SlashCommandBuilder()
|
||||||
.setName("devoir")
|
.setName("devoir")
|
||||||
.setDescription("Manipuler les devoirs à effectuer.")
|
.setDescription("Manipuler les devoirs à effectuer.")
|
||||||
.addSubcommand((command) =>
|
.addSubcommand((command) =>
|
||||||
|
@ -72,20 +91,10 @@ export function declare_commands(subjects: Set<string>) {
|
||||||
.setRequired(false)
|
.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(
|
export async function handle_command(
|
||||||
interaction: ChatInputCommandInteraction,
|
interaction: ChatInputCommandInteraction,
|
||||||
storage: Storage,
|
storage: Storage,
|
||||||
|
@ -159,27 +168,26 @@ export async function handle_command(
|
||||||
log("Unknown command", interaction.commandName);
|
log("Unknown command", interaction.commandName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// autocompletion
|
||||||
|
|
||||||
export async function handle_autocomplete(interaction: AutocompleteInteraction, storage: Storage) {
|
export async function handle_autocomplete(interaction: AutocompleteInteraction, storage: Storage) {
|
||||||
log("Auto completing.");
|
log("Auto completing.");
|
||||||
if (interaction.commandName === "devoir") {
|
if (interaction.commandName === "devoir") return await handle_autocomplete_devoir(interaction, storage);
|
||||||
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);
|
|
||||||
}
|
|
||||||
log("Unknown command", interaction.commandName);
|
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