From 3d32aad4cc5ed018936ea60f7b715c28b61bbcd7 Mon Sep 17 00:00:00 2001 From: saschabuehrle Date: Sat, 21 Mar 2026 15:53:09 +0100 Subject: [PATCH 1/2] fix(discord): recover existing thread on 160004 error (fixes #279) --- packages/adapter-discord/src/index.ts | 44 ++++++++++++++++++--------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/packages/adapter-discord/src/index.ts b/packages/adapter-discord/src/index.ts index 1b6f84a4..ec1c3873 100644 --- a/packages/adapter-discord/src/index.ts +++ b/packages/adapter-discord/src/index.ts @@ -959,23 +959,39 @@ export class DiscordAdapter implements Adapter { threadName, }); - const response = await this.discordFetch( - `/channels/${channelId}/messages/${messageId}/threads`, - "POST", - { - name: threadName, - auto_archive_duration: 1440, // 24 hours - } - ); + try { + const response = await this.discordFetch( + `/channels/${channelId}/messages/${messageId}/threads`, + "POST", + { + name: threadName, + auto_archive_duration: 1440, // 24 hours + } + ); - const result = (await response.json()) as { id: string; name: string }; + const result = (await response.json()) as { id: string; name: string }; - this.logger.debug("Discord API: POST thread response", { - threadId: result.id, - threadName: result.name, - }); + this.logger.debug("Discord API: POST thread response", { + threadId: result.id, + threadName: result.name, + }); - return result; + return result; + } catch (error) { + // Discord error 160004: "A thread has already been created for this message" + // Recover by using the existing thread (its ID equals the parent message ID). + if ( + error instanceof NetworkError && + error.message.includes("160004") + ) { + this.logger.debug( + "Thread already exists for message, reusing existing thread", + { channelId, messageId } + ); + return { id: messageId, name: threadName }; + } + throw error; + } } /** From cd9cc11892bf479303742ec79a70b2b31c9c4c22 Mon Sep 17 00:00:00 2001 From: Ben Sabic Date: Tue, 31 Mar 2026 13:22:24 +1100 Subject: [PATCH 2/2] Harden 160004 error matching, add tests, and add changeset --- .changeset/discord-thread-160004-recovery.md | 5 ++ packages/adapter-discord/src/index.test.ts | 67 ++++++++++++++++++++ packages/adapter-discord/src/index.ts | 2 + 3 files changed, 74 insertions(+) create mode 100644 .changeset/discord-thread-160004-recovery.md diff --git a/.changeset/discord-thread-160004-recovery.md b/.changeset/discord-thread-160004-recovery.md new file mode 100644 index 00000000..d32c3ace --- /dev/null +++ b/.changeset/discord-thread-160004-recovery.md @@ -0,0 +1,5 @@ +--- +"@chat-adapter/discord": patch +--- + +Fix silent thread creation failure when Discord returns error code 160004 ("A thread has already been created for this message"). The adapter now recovers by reusing the existing thread instead of falling back to a standalone channel message. diff --git a/packages/adapter-discord/src/index.test.ts b/packages/adapter-discord/src/index.test.ts index c20e6118..41e5096e 100644 --- a/packages/adapter-discord/src/index.test.ts +++ b/packages/adapter-discord/src/index.test.ts @@ -3968,3 +3968,70 @@ describe("mentionRoleIds handling", () => { fetchSpy.mockRestore(); }); }); + +// ============================================================================ +// createDiscordThread 160004 Recovery Tests +// ============================================================================ + +describe("createDiscordThread 160004 recovery", () => { + const adapter = createDiscordAdapter({ + botToken: "test-token", + publicKey: testPublicKey, + applicationId: "test-app-id", + logger: mockLogger, + }); + + it("should recover when Discord returns 160004 (thread already exists)", async () => { + const { NetworkError } = await import("@chat-adapter/shared"); + + const spy = vi + .spyOn(adapter as any, "discordFetch") + .mockRejectedValue( + new NetworkError( + "discord", + 'Discord API error: 400 {"code": 160004, "message": "A thread has already been created for this message"}' + ) + ); + + const result = await (adapter as any).createDiscordThread( + "channel123", + "msg456" + ); + + expect(result.id).toBe("msg456"); + expect(result.name).toContain("Thread "); + + spy.mockRestore(); + }); + + it("should propagate non-160004 NetworkErrors", async () => { + const { NetworkError } = await import("@chat-adapter/shared"); + + const spy = vi + .spyOn(adapter as any, "discordFetch") + .mockRejectedValue( + new NetworkError( + "discord", + 'Discord API error: 403 {"code": 50001, "message": "Missing Access"}' + ) + ); + + await expect( + (adapter as any).createDiscordThread("channel123", "msg456") + ).rejects.toThrow(NetworkError); + + spy.mockRestore(); + }); + + it("should propagate non-NetworkError errors", async () => { + const spy = vi + .spyOn(adapter as any, "discordFetch") + .mockRejectedValue(new Error("Connection failed")); + + await expect( + (adapter as any).createDiscordThread("channel123", "msg456") + ).rejects.toThrow("Connection failed"); + + spy.mockRestore(); + }); +}); diff --git a/packages/adapter-discord/src/index.ts b/packages/adapter-discord/src/index.ts index ec1c3873..ea302485 100644 --- a/packages/adapter-discord/src/index.ts +++ b/packages/adapter-discord/src/index.ts @@ -982,6 +982,8 @@ export class DiscordAdapter implements Adapter { // Recover by using the existing thread (its ID equals the parent message ID). if ( error instanceof NetworkError && + typeof error.message === "string" && + error.message.includes('"code"') && error.message.includes("160004") ) { this.logger.debug(