mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 03:01:25 +00:00
fix(discord): autoThread race condition when multiple agents mentioned
When multiple agents with autoThread:true are @mentioned in the same message, only the first agent successfully creates a thread. Subsequent agents fail because Discord only allows one thread per message. Previously, the failure was silently caught and the agent would fall back to replying in the parent channel. Now, when thread creation fails, the code re-fetches the message and checks for an existing thread (created by another agent). If found, the agent replies in that thread instead of falling back. Fixes #7508
This commit is contained in:
@@ -358,8 +358,24 @@ export async function maybeCreateDiscordAutoThread(params: {
|
||||
return createdId || undefined;
|
||||
} catch (err) {
|
||||
logVerbose(
|
||||
`discord: autoThread failed for ${params.message.channelId}/${params.message.id}: ${String(err)}`,
|
||||
`discord: autoThread creation failed for ${params.message.channelId}/${params.message.id}: ${String(err)}`,
|
||||
);
|
||||
// Race condition: another agent may have already created a thread on this
|
||||
// message. Re-fetch the message to check for an existing thread.
|
||||
try {
|
||||
const msg = (await params.client.rest.get(
|
||||
Routes.channelMessage(params.message.channelId, params.message.id),
|
||||
)) as { thread?: { id?: string } };
|
||||
const existingThreadId = msg?.thread?.id ? String(msg.thread.id) : "";
|
||||
if (existingThreadId) {
|
||||
logVerbose(
|
||||
`discord: autoThread reusing existing thread ${existingThreadId} on ${params.message.channelId}/${params.message.id}`,
|
||||
);
|
||||
return existingThreadId;
|
||||
}
|
||||
} catch {
|
||||
// If the refetch also fails, fall through to return undefined.
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user