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:
2 files changed
+68
-3
No files matched your search
+41
-1
@@ -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',
|
||||
|
||||
Reference in new issue
Block a user