Skip to content

Commit 2314059

Browse files
authored
fix(discord): record activity after successful poll and sticker sends (#126791)
* fix(discord): record activity for structured outbound messages * chore(discord): shrink structured-send assertion baseline
1 parent 0d55526 commit 2314059

3 files changed

Lines changed: 101 additions & 26 deletions

File tree

‎config/assertion-safety-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ extensions/discord/src/resolve-users.ts 1
431431
extensions/discord/src/security-audit.ts 12
432432
extensions/discord/src/send.components.ts 3
433433
extensions/discord/src/send.messages.ts 2
434-
extensions/discord/src/send.outbound.ts 3
434+
extensions/discord/src/send.outbound.ts 2
435435
extensions/discord/src/send.shared.ts 4
436436
extensions/discord/src/send.webhook.ts 1
437437
extensions/discord/src/setup-account-state.ts 1

‎extensions/discord/src/send.outbound.ts‎

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -447,8 +447,8 @@ export async function sendStickerDiscord(
447447
stickerIds: string[],
448448
opts: DiscordSendOpts & { content?: string },
449449
): Promise<DiscordSendResult> {
450-
const { rest, request, channelId, rewrittenContent, suppressEmbeds } =
451-
await resolveDiscordStructuredSendContext(to, opts);
450+
const context = await resolveDiscordStructuredSendContext(to, opts);
451+
const { rewrittenContent, suppressEmbeds } = context;
452452
const stickers = normalizeStickerIds(stickerIds);
453453
const flags = resolveDiscordMessageFlags({ suppressEmbeds });
454454
const body = {
@@ -458,22 +458,16 @@ export async function sendStickerDiscord(
458458
enforce_nonce: true,
459459
...(flags ? { flags } : {}),
460460
};
461-
await opts.onPlatformSendDispatch?.();
462-
const res = (await request(
463-
() => createChannelMessage<{ id: string; channel_id: string }>(rest, channelId, { body }),
464-
"sticker",
465-
{ safety: "nonce-protected-create" },
466-
)) as { id: string; channel_id: string };
467-
return toDiscordSendResult(res, channelId, { kind: "card" });
461+
return context.send("sticker", body);
468462
}
469463

470464
export async function sendPollDiscord(
471465
to: string,
472466
poll: PollInput,
473467
opts: DiscordSendOpts & { content?: string },
474468
): Promise<DiscordSendResult> {
475-
const { rest, request, channelId, rewrittenContent, suppressEmbeds } =
476-
await resolveDiscordStructuredSendContext(to, opts);
469+
const context = await resolveDiscordStructuredSendContext(to, opts);
470+
const { rewrittenContent, suppressEmbeds } = context;
477471
if (poll.durationSeconds !== undefined) {
478472
throw new Error("Discord polls do not support durationSeconds; use durationHours");
479473
}
@@ -486,22 +480,14 @@ export async function sendPollDiscord(
486480
enforce_nonce: true,
487481
...(flags ? { flags } : {}),
488482
};
489-
await opts.onPlatformSendDispatch?.();
490-
const res = (await request(
491-
() => createChannelMessage<{ id: string; channel_id: string }>(rest, channelId, { body }),
492-
"poll",
493-
{ safety: "nonce-protected-create" },
494-
)) as { id: string; channel_id: string };
495-
return toDiscordSendResult(res, channelId, { kind: "poll", threadId: opts.threadId });
483+
return context.send("poll", body);
496484
}
497485

498486
async function resolveDiscordStructuredSendContext(
499487
to: string,
500488
opts: DiscordSendOpts & { content?: string },
501489
): Promise<{
502-
rest: RequestClient;
503-
request: DiscordClientRequest;
504-
channelId: string;
490+
send: (kind: "poll" | "sticker", body: Record<string, unknown>) => Promise<DiscordSendResult>;
505491
rewrittenContent?: string;
506492
suppressEmbeds: boolean;
507493
}> {
@@ -519,9 +505,23 @@ async function resolveDiscordStructuredSendContext(
519505
})
520506
: undefined;
521507
return {
522-
rest,
523-
request,
524-
channelId,
508+
send: async (kind, body) => {
509+
await opts.onPlatformSendDispatch?.();
510+
const result = (await request(
511+
() => createChannelMessage<{ id: string; channel_id: string }>(rest, channelId, { body }),
512+
kind,
513+
{ safety: "nonce-protected-create" },
514+
)) as { id: string; channel_id: string };
515+
recordChannelActivity({
516+
channel: "discord",
517+
accountId: accountInfo.accountId,
518+
direction: "outbound",
519+
});
520+
return toDiscordSendResult(result, channelId, {
521+
kind: kind === "poll" ? "poll" : "card",
522+
threadId: kind === "poll" ? opts.threadId : undefined,
523+
});
524+
},
525525
rewrittenContent,
526526
suppressEmbeds: resolveDiscordSuppressEmbeds({
527527
configured: accountInfo.config.suppressEmbeds,

‎extensions/discord/src/send.webhook-activity.test.ts‎

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import { MessageFlags } from "discord-api-types/v10";
33
import { isRecentOutboundMessageIdentity } from "openclaw/plugin-sdk/channel-outbound";
44
import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
5+
import { makeDiscordRest } from "./send.test-harness.js";
56

67
const recordChannelActivityMock = vi.hoisted(() => vi.fn());
78
const loadConfigMock = vi.hoisted(() => vi.fn(() => ({ channels: { discord: {} } })));
@@ -28,6 +29,8 @@ vi.mock("openclaw/plugin-sdk/channel-activity-runtime", async () => {
2829
});
2930

3031
let sendWebhookMessageDiscord: typeof import("./send.webhook.js").sendWebhookMessageDiscord;
32+
let sendPollDiscord: typeof import("./send.outbound.js").sendPollDiscord;
33+
let sendStickerDiscord: typeof import("./send.outbound.js").sendStickerDiscord;
3134

3235
type MockWithCalls = { mock: { calls: unknown[][] } };
3336

@@ -39,9 +42,19 @@ function firstMockCall(mock: MockWithCalls, label: string): unknown[] {
3942
return call;
4043
}
4144

42-
describe("sendWebhookMessageDiscord activity", () => {
45+
async function sendStructuredMessage(
46+
kind: "poll" | "sticker",
47+
opts: Parameters<typeof sendStickerDiscord>[2],
48+
) {
49+
return kind === "poll"
50+
? sendPollDiscord("channel:789", { question: "Lunch?", options: ["Pizza", "Sushi"] }, opts)
51+
: sendStickerDiscord("channel:789", ["123"], opts);
52+
}
53+
54+
describe("Discord outbound channel activity", () => {
4355
beforeAll(async () => {
4456
({ sendWebhookMessageDiscord } = await import("./send.webhook.js"));
57+
({ sendPollDiscord, sendStickerDiscord } = await import("./send.outbound.js"));
4558
});
4659

4760
beforeEach(() => {
@@ -126,6 +139,68 @@ describe("sendWebhookMessageDiscord activity", () => {
126139
expect(loadConfigMock).not.toHaveBeenCalled();
127140
});
128141

142+
it.each([
143+
{ kind: "poll", accountId: undefined, defaultAccount: undefined, expectedAccountId: "default" },
144+
{ kind: "poll", accountId: " Work ", defaultAccount: undefined, expectedAccountId: "work" },
145+
{ kind: "poll", accountId: undefined, defaultAccount: "work", expectedAccountId: "work" },
146+
{
147+
kind: "sticker",
148+
accountId: undefined,
149+
defaultAccount: undefined,
150+
expectedAccountId: "default",
151+
},
152+
{ kind: "sticker", accountId: " Work ", defaultAccount: undefined, expectedAccountId: "work" },
153+
{ kind: "sticker", accountId: undefined, defaultAccount: "work", expectedAccountId: "work" },
154+
] as const)(
155+
"records successful $kind sends for the resolved $expectedAccountId account",
156+
async ({ kind, accountId, defaultAccount, expectedAccountId }) => {
157+
const { rest, postMock } = makeDiscordRest();
158+
postMock.mockResolvedValue({ id: "msg-1", channel_id: "789" });
159+
const cfg = {
160+
channels: {
161+
discord: {
162+
token: "resolved-token",
163+
accounts: {
164+
default: { token: "default-token" },
165+
work: { token: "work-token" },
166+
},
167+
...(defaultAccount ? { defaultAccount } : {}),
168+
},
169+
},
170+
};
171+
172+
await sendStructuredMessage(kind, {
173+
cfg,
174+
rest,
175+
token: "test-token",
176+
...(accountId ? { accountId } : {}),
177+
});
178+
179+
expect(recordChannelActivityMock).toHaveBeenCalledExactlyOnceWith({
180+
channel: "discord",
181+
accountId: expectedAccountId,
182+
direction: "outbound",
183+
});
184+
},
185+
);
186+
187+
it.each(["poll", "sticker"] as const)(
188+
"does not record outbound activity when a %s send fails",
189+
async (kind) => {
190+
const { rest, postMock } = makeDiscordRest();
191+
postMock.mockRejectedValue(new Error("provider rejected"));
192+
193+
await expect(
194+
sendStructuredMessage(kind, {
195+
cfg: { channels: { discord: { token: "resolved-token" } } },
196+
rest,
197+
token: "test-token",
198+
}),
199+
).rejects.toThrow("provider rejected");
200+
expect(recordChannelActivityMock).not.toHaveBeenCalled();
201+
},
202+
);
203+
129204
it("rewrites configured mention aliases for webhook sends", async () => {
130205
const cfg = {
131206
channels: {

0 commit comments

Comments
 (0)