diff --git a/src/channels/discord.test.ts b/src/channels/discord.test.ts index cecce26..b1a75be 100644 --- a/src/channels/discord.test.ts +++ b/src/channels/discord.test.ts @@ -1389,6 +1389,51 @@ describe('message edit handling', () => { expect(opts.onMessage).not.toHaveBeenCalled(); }); + it('ignores an embed being generated for a link (edit time unchanged)', async () => { + const opts = createTestOpts(); + const channel = new DiscordChannel('test-token', opts); + await channel.connect(); + + // Unfurl: Discord adds the embed but leaves edited_timestamp null. + const oldMessage = { + ...createMessage({ content: '이거 봐 https://example.com' }), + editedAt: null, + embeds: [], + }; + const newMessage = { + ...createMessage({ content: '이거 봐 https://example.com' }), + editedAt: null, + embeds: [{ url: 'https://example.com', title: 'Example' }], + }; + await triggerMessageUpdate(oldMessage, newMessage); + + expect(opts.onMessage).not.toHaveBeenCalled(); + }); + + it('ignores "Remove Embeds" even on a message that was edited before', async () => { + const opts = createTestOpts(); + const channel = new DiscordChannel('test-token', opts); + await channel.connect(); + + // SUPPRESS_EMBEDS keeps the earlier edited_timestamp. The cached old text + // is deliberately different so only the edit-time check can reject it. + const editedAt = new Date('2024-01-01T00:05:00.000Z'); + const oldMessage = { + ...createMessage({ content: '수정 전 https://example.com' }), + editedAt: new Date(editedAt), + embeds: [{ url: 'https://example.com' }], + }; + const newMessage = { + ...createMessage({ content: '수정 후 https://example.com' }), + editedAt: new Date(editedAt), + embeds: [], + flags: { has: (flag: number) => flag === 1 << 2 }, + }; + await triggerMessageUpdate(oldMessage, newMessage); + + expect(opts.onMessage).not.toHaveBeenCalled(); + }); + it('ignores edits by bots, including its own messages', async () => { const opts = createTestOpts(); const channel = new DiscordChannel('test-token', opts); diff --git a/src/channels/discord.ts b/src/channels/discord.ts index 4ac9a62..d92fcab 100644 --- a/src/channels/discord.ts +++ b/src/channels/discord.ts @@ -415,9 +415,16 @@ export class DiscordChannel implements Channel { // Human edits only: own/other bot edits (streaming progress, reviewer bot) // are not user input. if (newMessage.author.bot) return; + // Discord sets a new edited_timestamp only when the author edits the + // message. Embed/link-preview generation and "Remove Embeds" (the + // SUPPRESS_EMBEDS flag) also fire MessageUpdate but leave it unchanged. + const editedAt = newMessage.editedAt; + if (!editedAt || editedAt.getTime() === oldMessage.editedAt?.getTime()) { + return; + } const before = oldMessage.content; const after = newMessage.content; - // Same text = embed/link-preview update, not a user edit. + // Same text = not a text edit. if (before === after) return; const chatJid = `dc:${newMessage.channelId}`; @@ -430,7 +437,6 @@ export class DiscordChannel implements Channel { return; } - const editedAt = newMessage.editedAt ?? new Date(); const senderName = newMessage.member?.displayName || newMessage.author.displayName ||