Add paired delivery diagnostic logging
This commit is contained in:
@@ -113,6 +113,7 @@ vi.mock('discord.js', () => {
|
||||
});
|
||||
|
||||
import { DiscordChannel, DiscordChannelOpts } from './discord.js';
|
||||
import { logger } from '../logger.js';
|
||||
|
||||
// --- Test helpers ---
|
||||
|
||||
@@ -815,6 +816,32 @@ describe('DiscordChannel', () => {
|
||||
flags: 1 << 2,
|
||||
});
|
||||
});
|
||||
|
||||
it('logs channel name and Discord message ids after sending', async () => {
|
||||
const opts = createTestOpts();
|
||||
const channel = new DiscordChannel('test-token', opts);
|
||||
await channel.connect();
|
||||
|
||||
const mockChannel = {
|
||||
send: vi.fn().mockResolvedValue({ id: 'discord-message-1' }),
|
||||
sendTyping: vi.fn(),
|
||||
};
|
||||
currentClient().channels.fetch.mockResolvedValue(mockChannel);
|
||||
|
||||
await channel.sendMessage('dc:1234567890123456', 'Hello');
|
||||
|
||||
expect(logger.info).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
jid: 'dc:1234567890123456',
|
||||
channelName: 'discord',
|
||||
deliveryMode: 'send',
|
||||
chunkCount: 1,
|
||||
messageId: 'discord-message-1',
|
||||
messageIds: ['discord-message-1'],
|
||||
}),
|
||||
'Discord message sent',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
// --- ownsJid ---
|
||||
|
||||
@@ -533,9 +533,19 @@ export class DiscordChannel implements Channel {
|
||||
attachment: f,
|
||||
name: path.basename(f),
|
||||
}));
|
||||
const sentMessageIds: string[] = [];
|
||||
let chunkCount = 0;
|
||||
|
||||
const recordSentMessage = (message: Message | null | undefined) => {
|
||||
chunkCount += 1;
|
||||
if (message?.id) sentMessageIds.push(message.id);
|
||||
};
|
||||
|
||||
if (!cleaned && files.length === 0) {
|
||||
logger.debug({ jid }, 'Skipping empty Discord outbound message');
|
||||
logger.debug(
|
||||
{ jid, channelName: this.name },
|
||||
'Skipping empty Discord outbound message',
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -547,17 +557,21 @@ export class DiscordChannel implements Channel {
|
||||
|
||||
if (cleaned.length <= MAX_LENGTH) {
|
||||
// Send text with first batch of files
|
||||
await textChannel.send({
|
||||
content: cleaned || undefined,
|
||||
files: fileBatches[0]?.length ? fileBatches[0] : undefined,
|
||||
flags: MessageFlags.SuppressEmbeds,
|
||||
});
|
||||
recordSentMessage(
|
||||
await textChannel.send({
|
||||
content: cleaned || undefined,
|
||||
files: fileBatches[0]?.length ? fileBatches[0] : undefined,
|
||||
flags: MessageFlags.SuppressEmbeds,
|
||||
}),
|
||||
);
|
||||
// Send remaining file batches as follow-up messages
|
||||
for (let b = 1; b < fileBatches.length; b++) {
|
||||
await textChannel.send({
|
||||
files: fileBatches[b],
|
||||
flags: MessageFlags.SuppressEmbeds,
|
||||
});
|
||||
recordSentMessage(
|
||||
await textChannel.send({
|
||||
files: fileBatches[b],
|
||||
flags: MessageFlags.SuppressEmbeds,
|
||||
}),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
// Send text in chunks, attach first batch to the first chunk
|
||||
@@ -565,24 +579,43 @@ export class DiscordChannel implements Channel {
|
||||
for (let i = 0; i < cleaned.length; i += MAX_LENGTH) {
|
||||
const chunk = cleaned.slice(i, i + MAX_LENGTH);
|
||||
const batch = fileBatches[fileBatchIndex];
|
||||
await textChannel.send({
|
||||
content: chunk,
|
||||
files: batch?.length ? batch : undefined,
|
||||
flags: MessageFlags.SuppressEmbeds,
|
||||
});
|
||||
recordSentMessage(
|
||||
await textChannel.send({
|
||||
content: chunk,
|
||||
files: batch?.length ? batch : undefined,
|
||||
flags: MessageFlags.SuppressEmbeds,
|
||||
}),
|
||||
);
|
||||
if (batch?.length) fileBatchIndex++;
|
||||
}
|
||||
// Send any remaining file batches
|
||||
for (let b = fileBatchIndex; b < fileBatches.length; b++) {
|
||||
await textChannel.send({
|
||||
files: fileBatches[b],
|
||||
flags: MessageFlags.SuppressEmbeds,
|
||||
});
|
||||
recordSentMessage(
|
||||
await textChannel.send({
|
||||
files: fileBatches[b],
|
||||
flags: MessageFlags.SuppressEmbeds,
|
||||
}),
|
||||
);
|
||||
}
|
||||
}
|
||||
logger.info({ jid, length: text.length }, 'Discord message sent');
|
||||
logger.info(
|
||||
{
|
||||
jid,
|
||||
channelName: this.name,
|
||||
length: text.length,
|
||||
deliveryMode: 'send',
|
||||
chunkCount,
|
||||
attachmentCount: files.length,
|
||||
messageId: sentMessageIds[0] ?? null,
|
||||
messageIds: sentMessageIds,
|
||||
},
|
||||
'Discord message sent',
|
||||
);
|
||||
} catch (err) {
|
||||
logger.error({ jid, err }, 'Failed to send Discord message');
|
||||
logger.error(
|
||||
{ jid, channelName: this.name, err },
|
||||
'Failed to send Discord message',
|
||||
);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
@@ -780,8 +813,21 @@ export class DiscordChannel implements Channel {
|
||||
if (!channel || !('messages' in channel)) return;
|
||||
const msg = await (channel as TextChannel).messages.fetch(messageId);
|
||||
await msg.edit(text);
|
||||
logger.info(
|
||||
{
|
||||
jid,
|
||||
channelName: this.name,
|
||||
deliveryMode: 'edit',
|
||||
messageId,
|
||||
length: text.length,
|
||||
},
|
||||
'Discord message edited',
|
||||
);
|
||||
} catch (err) {
|
||||
logger.debug({ jid, messageId, err }, 'Failed to edit Discord message');
|
||||
logger.debug(
|
||||
{ jid, channelName: this.name, messageId, err },
|
||||
'Failed to edit Discord message',
|
||||
);
|
||||
throw err; // Re-throw so callers (e.g. dashboard) can reset message ID
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user