Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions bots/events/handlers/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
7 changes: 7 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
20 changes: 20 additions & 0 deletions test/harness.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading