Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/discord-thread-160004-recovery.md
Original file line number Diff line number Diff line change
@@ -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.
67 changes: 67 additions & 0 deletions packages/adapter-discord/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
46 changes: 32 additions & 14 deletions packages/adapter-discord/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,23 +959,41 @@ export class DiscordAdapter implements Adapter<DiscordThreadId, unknown> {
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 &&
typeof error.message === "string" &&
error.message.includes('"code"') &&
error.message.includes("160004")
) {
this.logger.debug(
"Thread already exists for message, reusing existing thread",
{ channelId, messageId }
);
return { id: messageId, name: threadName };
}
throw error;
}
}

/**
Expand Down
Loading