From 5371f5c037740a51395e933146a38d2e495166f9 Mon Sep 17 00:00:00 2001 From: Musiker15 Date: Thu, 5 Nov 2026 12:09:36 +0100 Subject: [PATCH] Stop an edit of an uncached message from taking the bot down An edit or delete of a message that is not in the cache arrives as a partial with author null. The logging handlers read author.id and threw. discord.js turns that into an error event on the client, and with no listener Node exits the process: twelve restarts in the last seven days. Both handlers now skip a message without an author, and every client gets an error listener so a failing handler is logged instead of ending the process. --- bots/events/handlers/logging.js | 6 ++++-- main.js | 7 +++++++ test/harness.js | 20 ++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/bots/events/handlers/logging.js b/bots/events/handlers/logging.js index 907490b..6b531dd 100644 --- a/bots/events/handlers/logging.js +++ b/bots/events/handlers/logging.js @@ -235,8 +235,10 @@ async function onMemberUnban(guild, user) { // ─── Message events ─────────────────────────────────────────────────────────── +// An uncached message arrives as a partial without an author (Partials.Message). +// There is nothing to log for it, and reading author.id would throw. async function onMessageDelete(message) { - if (!message.guild || message.author?.bot) return; + if (!message.guild || !message.author || message.author.bot) return; if (ignoredChannel(message.channel.id) || ignoredUser(message.author.id)) return; let deleter = null; @@ -286,7 +288,7 @@ async function onBulkMessageDelete(messages) { } async function onMessageEdit(before, after) { - if (!before.guild || before.author?.bot) return; + if (!before.guild || !before.author || before.author.bot) return; if (before.content === after.content) return; if (ignoredChannel(before.channel.id) || ignoredUser(before.author.id)) return; diff --git a/main.js b/main.js index b186fa1..c8f419b 100644 --- a/main.js +++ b/main.js @@ -34,6 +34,13 @@ async function runGroup(group, token) { const client = new Client({ intents: [...intentSet], partials: [...partialSet] }); const registry = createRegistry(); + // discord.js turns a rejected event handler into an 'error' event on the + // client. With no listener, Node makes that an uncaught exception and the + // whole bot process exits, so one bad event took every bot down. + client.on(Events.Error, err => { + console.error(`[${label}] event handler failed: ${err?.stack || err}`); + }); + for (const b of group) { try { b.module.attach(client, registry, { botName: b.name }); diff --git a/test/harness.js b/test/harness.js index 3ada83d..a313393 100644 --- a/test/harness.js +++ b/test/harness.js @@ -1785,6 +1785,26 @@ const nameOf = key => config.command(key).name; }); } + section('L) message logging survives partial messages'); + { + // An edit or delete of an uncached message arrives as a partial with + // author null. Reading author.id threw, and with no 'error' listener on + // the client that exception ended the whole process. + const logging = req('bots/events/handlers/logging'); + const partial = { partial: true, guild: { id: '1' }, author: null, content: null, channel: { id: '2' } }; + + await acheck('an edit of an uncached message is skipped, not thrown', async () => { + await logging.onMessageEdit(partial, { ...partial, partial: false, content: 'new' }); + }); + await acheck('a delete of an uncached message is skipped, not thrown', async () => { + await logging.onMessageDelete(partial); + }); + check('every client gets an error listener, so one failing handler cannot end the process', () => { + const src = fs.readFileSync(path.join(REPO, 'main.js'), 'utf8'); + assert.match(src, /client\.on\(Events\.Error,/); + }); + } + // ------------------------------------------------------------ client section('H) real discord.js Client and a live REST round trip'); let realClient = null;