const botConfig = require('../../../model/botConfig/botConfig.model') const slashCommands = require('../../../utils/slashCommands') const log = require('../../../utils/logger')('internal') // GET /internal/bot-config — called by the bot process on its own boot so a // restart self-reconnects without any admin-panel interaction. Returns the // DECRYPTED token — this route must never be reachable outside the private // compose network (see requireInternalKey + deployment notes). async function getBotConfig(req, res) { try { const config = await botConfig.getWithToken() if (!config) return res.json({ enabled: false, token: null, guildId: null }) return res.json({ enabled: config.enabled, token: config.token, guildId: config.guildId }) } catch (err) { log.error('internal.getBotConfig', err) return res.status(500).json({ message: 'Internal Server Error' }) } } // GET /internal/commands — the registered slash-command definitions, pulled by // the bot on `ready` and again whenever it is nudged (TEAMS.md §7.1). // // `version` is `modules.version()`, the counter every module state change bumps. // The bot holds the value it registered with and re-PUTs only when it differs, // which is what makes DEREGISTRATION free: the bot's single whole-set // `REST.put(applicationGuildCommands)` means a module that is gone is simply // absent from the next pull, with nobody having to remember to unregister it. function listCommands(req, res) { return res.json(slashCommands.definitions()) } // POST /internal/commands/dispatch — run one command and answer with the // envelope. Never 500s on a handler's behalf: `dispatch` catches per handler and // reports `{ ok: false, reason }`, so the bot always has something to render and // a module's failure is its own. async function dispatchCommand(req, res) { const { command, options, platform, platformUserId, guildId } = req.body || {} if (!command) return res.status(400).json({ ok: false, reason: 'unknown' }) try { const result = await slashCommands.dispatch({ command, options: options && typeof options === 'object' ? options : {}, platform: platform || 'discord', platformUserId, guildId, }) return res.json(result) } catch (err) { // dispatch() is documented never to throw; if it ever does, that is core's // bug and not the module's, and it is logged as one. log.error('internal.dispatchCommand', err) return res.status(500).json({ ok: false, reason: 'error' }) } } module.exports = { getBotConfig, listCommands, dispatchCommand }