feat(web): publish the slash commands from the panel

Registering the commands was the one routine step that still needed shell
access on the host, which is awkward when the bot runs behind a hosting
panel. Guild commands take effect immediately, so the button says what it
did as soon as it returns.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
nocodeandClaude Opus 5 committed 2026-09-23 22:31:42 +02:00
1 parent a61f446e44
commit d518f9fd56
2 files changed
+68 -3

No files matched your search

+41 -1
View File
@@ -1,6 +1,7 @@
import { ChannelType } from 'discord.js';
import { ChannelType, REST, Routes } from 'discord.js';
import { ACCESS_LABELS, ACCESS_LEVELS, MODERATION_ACTIONS } from '../../config/constants.js';
import { env } from '../../config/env.js';
import { ExternalApiError } from '../../core/errors.js';
import { createLogger } from '../../core/logger.js';
import { canActOn, getAccessLevel } from '../../core/permissions.js';
import { logEvent } from '../../database/repositories/event-log-repository.js';
@@ -336,6 +337,45 @@ export function registerDiscordRoutes(router) {
{ access: ACCESS_LEVELS.STAFF },
);
/**
* Publishes the slash commands on the guild, what `npm run deploy:commands`
* does. Guild commands take effect immediately, so the result is visible in
* Discord as soon as this returns.
*/
router.post(
'/api/discord/commands/deploy',
async ({ context, actor }) => {
const commands = [...(context.client?.commands?.values() ?? [])];
if (commands.length === 0) throw badRequest('Aucune commande chargée : le bot n’est pas prêt.');
if (!env.discord.token || !env.discord.clientId || !env.discord.guildId) {
throw badRequest('`DISCORD_TOKEN`, `DISCORD_CLIENT_ID` et `DISCORD_GUILD_ID` sont requis.');
}
const body = commands.map((command) => command.data.toJSON());
const rest = new REST().setToken(env.discord.token);
try {
await rest.put(Routes.applicationGuildCommands(env.discord.clientId, env.discord.guildId), {
body,
});
} catch (error) {
log.error('Slash command deployment failed', error);
throw new ExternalApiError('discord', error.message ?? 'Publication refusée par Discord');
}
logEvent({
guildId: guildId(),
type: 'web.commands.deploy',
actorId: actor.userId,
payload: { count: body.length },
});
log.info('Slash commands deployed from the dashboard', { count: body.length, actor: actor.userId });
return { ok: true, count: body.length, names: body.map((command) => command.name).sort() };
},
{ access: ACCESS_LEVELS.OWNER },
);
/** Read-only inventory of the loaded slash commands and their required level. */
router.get(
'/api/discord/commands',
+27 -2
View File
@@ -4,6 +4,7 @@ import {
badge,
button,
card,
confirmAction,
field,
grid,
input,
@@ -397,8 +398,32 @@ export async function render(host, { state }) {
notificationsCard(guild, level),
card(
'Commandes',
{ advanced: true, subtitle: 'Commandes slash chargées et niveau requis.' },
'Commandes slash',
{
subtitle: 'Commandes chargées par le bot et niveau requis pour chacune.',
actions:
level >= LEVELS.OWNER &&
button(
'Publier sur Discord',
async () => {
if (
!(await confirmAction(
'Publier les commandes',
'Les commandes chargées remplacent celles enregistrées sur le serveur. L’effet est immédiat.',
))
) {
return;
}
const result = await api.post('/api/discord/commands/deploy', {});
toast(`${result.count} commande(s) publiée(s) sur le serveur.`);
},
{ tone: 'primary' },
),
},
h('p', {
class: 'muted',
text: 'À relancer après chaque ajout ou modification de commande — l’équivalent de `npm run deploy:commands`.',
}),
await renderCommands(),
),
),