diff --git a/src/web/api/discord.js b/src/web/api/discord.js index 2153c41..07e3add 100644 --- a/src/web/api/discord.js +++ b/src/web/api/discord.js @@ -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', diff --git a/src/web/public/views/discord.js b/src/web/public/views/discord.js index 97356b1..c386cde 100644 --- a/src/web/public/views/discord.js +++ b/src/web/public/views/discord.js @@ -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(), ), ),