Skip to content
Open
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
44 changes: 30 additions & 14 deletions packages/adapter-discord/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,23 +959,39 @@ 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 &&
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