/** * Send error details to DEBUGGING_CHANNEL_ID when set. * Call setClient(client) from the main bot on ready so errors can be posted. */ const { CONFIG } = require('../config'); let client = null; function setClient(c) { client = c; } /** * Post an error to the debugging channel (if DEBUGGING_CHANNEL_ID and client are set). * @param {string} context - e.g. 'escalate', 'deescalate', 'email-routing', 'Gmail poll' * @param {Error} error * @param {import('discord.js').Interaction} [interaction] * @param {import('discord.js').Client} [overrideClient] - use this client instead of stored (e.g. from gmail-poll) */ async function logError(context, error, interaction = null, overrideClient = null) { const c = overrideClient || client; if (!c || !CONFIG.DEBUGGING_CHANNEL_ID) return; try { const channel = await c.channels.fetch(CONFIG.DEBUGGING_CHANNEL_ID); const userLine = interaction?.user?.tag ? `User: ${interaction.user.tag}\n` : ''; const commandLine = (interaction?.commandName || interaction?.customId) ? `Command/Button: ${interaction.commandName || interaction.customId}\n` : ''; const stack = (error.stack || error.message || String(error)).slice(0, 1500); await channel.send({ content: `\`[${context}]\` ${error.message || String(error)}\n${userLine}${commandLine}\n\`\`\`${stack}\`\`\`` }); } catch (_) { // ignore send failures } } module.exports = { setClient, logError };