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; + } } /**