From 3d32aad4cc5ed018936ea60f7b715c28b61bbcd7 Mon Sep 17 00:00:00 2001 From: saschabuehrle Date: Sat, 21 Mar 2026 15:53:09 +0100 Subject: [PATCH] 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; + } } /**