fix(discord): reuse nonces after lost responses

A successful create may lose its response. Reuse a stable nonce
with enforce_nonce so retries recover that message instead of
posting duplicates or accepting an unreadable success.
This commit is contained in:
翅膀 committed 2026-10-23 23:22:29 +08:00
1 parent 966403bdc1
commit 7cf507f654
3 files changed
+28 -11

No files matched your search

+7 -1
View File
@@ -12,7 +12,13 @@ export class DiscordDriver implements PlatformDriver {
return { ok: false, error: "target.channelId is required", errorCode: "NO_TARGET" };
}
const token = env.DISCORD_TOKEN ?? "";
return sendMessage(token, channelId, renderNeutralMessage(message), target.threadId);
return sendMessage(
token,
channelId,
renderNeutralMessage(message),
target.threadId,
message.sendKey,
);
}
async edit(
+14 -10
View File
@@ -81,15 +81,11 @@ async function request(
};
}
let messageId: string | undefined;
try {
const data = (await res.json()) as DiscordMessage;
messageId = data.id;
} catch {
// ignore malformed success body
}
return { ok: true, status: res.status, messageId, attempts: attempt + 1 };
// A success whose body cannot be read (cut off, timed out) is not known
// to have happened: tried again — a create carries its nonce, so it is
// never doubled.
const data = (await res.json()) as DiscordMessage | null;
return { ok: true, status: res.status, messageId: data?.id, attempts: attempt + 1 };
} catch (err) {
log.error({ err, attempt, channelId }, "Failed to send message");
if (attempt === 2)
@@ -106,11 +102,19 @@ export async function sendMessage(
channelId: string,
message: unknown,
threadId?: string,
sendKey?: string,
): Promise<SendResult> {
// Ids are path segments: encoded, so no stored value can address another
// resource (`x/../123` is never the channel `123`).
const url = `${DISCORD_API}/channels/${encodeURIComponent(threadId || channelId)}/messages`;
return request(url, "POST", token, message, channelId);
// One nonce for every attempt of this send — and, given a `sendKey`, for
// every attempt of the delivery: when a create went through but its
// response was lost, Discord answers the retry with that same message
// instead of posting a second one (`enforce_nonce`, a few minutes' window).
const nonce = (sendKey ?? crypto.randomUUID().replace(/-/g, "")).slice(0, 25);
const body =
message && typeof message === "object" ? { ...message, nonce, enforce_nonce: true } : message;
return request(url, "POST", token, body, channelId);
}
export async function editMessage(
+7
View File
@@ -326,6 +326,13 @@ export interface NeutralMessage {
* only to find a message tracked under the old key (then re-keyed).
*/
legacyUpdateKey?: string;
/**
* Stable identity of this message for one target, the same in every
* attempt of a delivery (set by dispatch from delivery × route × target).
* Platforms with request idempotency (Discord `nonce`, Feishu `uuid`) use
* it, so a retry after a lost response never posts the message twice.
*/
sendKey?: string;
/**
* Discord role ids to mention in the message content (set by dispatch from
* the route's `discordRoleIds`). Only used by the Discord driver.