- Remove no-op log stubs (logGmail, logAutomation, logSecurity, logSystem) and ~17 callsites; dead counters in tickets.js and gmail-poll.js go too - Dedup three near-identical Gmail send paths into sendThreadedEmail helper - Drop dead Mongoose fields: broccoliniTicketId, lastSyncedBroccoliniArticleId, renameCount, renameWindowStart, reminderSent, staffChannelId, unclaimedRemindersSent, lastMessageAuthorIsStaff - Drop dead config fields and their .env.example entries - Inline api/botClient.js (3-line wrapper, 2 callers) - Trim unused exports across utils.js, tickets.js, configSchema.js, debugLog.js - Fix handlers/messages.js to use isStaff() — old partial check ignored ADDITIONAL_STAFF_ROLES, so those members were treated as customers - Drop unused deps p-queue + dotenv-expand; move mongodb to devDependencies Net: -583 LOC source + -57 LOC lockfile. All 23 modules load clean. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
81 lines
2.3 KiB
JavaScript
81 lines
2.3 KiB
JavaScript
/**
|
||
* Structured logging service – posts embeds to dedicated Discord channels.
|
||
* Call setClient(client) from the main bot on ready so logs can be posted.
|
||
*/
|
||
const { EmbedBuilder } = require('discord.js');
|
||
const { CONFIG } = require('../config');
|
||
|
||
let client = null;
|
||
|
||
function setClient(c) {
|
||
client = c;
|
||
}
|
||
|
||
// --- Helpers ---
|
||
|
||
async function sendToChannel(channelId, embed, overrideClient) {
|
||
const c = overrideClient || client;
|
||
if (!c || !channelId) return;
|
||
try {
|
||
const channel = await c.channels.fetch(channelId);
|
||
if (channel) await channel.send({ embeds: [embed] });
|
||
} catch (_) {
|
||
// ignore send failures
|
||
}
|
||
}
|
||
|
||
// --- logError (backwards-compatible) ---
|
||
|
||
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
|
||
}
|
||
}
|
||
|
||
// --- logWarn ---
|
||
|
||
async function logWarn(context, message, overrideClient = null) {
|
||
const embed = new EmbedBuilder()
|
||
.setTitle(`Warning: ${context}`)
|
||
.setDescription(String(message).slice(0, 4000))
|
||
.setColor(0xFFFF00)
|
||
.setTimestamp();
|
||
await sendToChannel(CONFIG.DEBUGGING_CHANNEL_ID, embed, overrideClient);
|
||
}
|
||
|
||
// --- logTicketEvent ---
|
||
|
||
async function logTicketEvent(action, fields, interaction = null) {
|
||
const embed = new EmbedBuilder()
|
||
.setTitle(action)
|
||
.setColor(CONFIG.EMBED_COLOR_INFO || 0x1e2124)
|
||
.addFields(fields.map(f => ({ name: f.name, value: String(f.value).slice(0, 1024), inline: f.inline ?? true })))
|
||
.setTimestamp();
|
||
if (interaction?.user?.tag) {
|
||
embed.setFooter({ text: interaction.user.tag });
|
||
}
|
||
await sendToChannel(CONFIG.LOG_CHAN, embed, interaction?.client);
|
||
}
|
||
|
||
module.exports = {
|
||
setClient,
|
||
logError,
|
||
logWarn,
|
||
logTicketEvent
|
||
};
|