Skip to content

Commit fb246b3

Browse files
committed
fix(discord): ping mention-bearing final replies under live preview (#88360)
1 parent 70c8abd commit fb246b3

4 files changed

Lines changed: 150 additions & 2 deletions

File tree

‎extensions/discord/src/mentions.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import {
33
resetDiscordDirectoryCacheForTest,
44
rememberDiscordDirectoryUser,
55
} from "./directory-cache.js";
6-
import { formatMention, rewriteDiscordKnownMentions } from "./mentions.js";
6+
import {
7+
discordTextHasBroadcastMention,
8+
discordTextHasTargetedMention,
9+
formatMention,
10+
rewriteDiscordKnownMentions,
11+
} from "./mentions.js";
712

813
describe("formatMention", () => {
914
it("formats user mentions from ids", () => {
@@ -109,3 +114,29 @@ describe("rewriteDiscordKnownMentions", () => {
109114
expect(opsRewrite).toBe("<@999888777>");
110115
});
111116
});
117+
118+
describe("discordTextHasTargetedMention", () => {
119+
it("detects user and role mentions", () => {
120+
expect(discordTextHasTargetedMention("ping <@123>")).toBe(true);
121+
expect(discordTextHasTargetedMention("ping <@!123>")).toBe(true);
122+
expect(discordTextHasTargetedMention("ping <@&456>")).toBe(true);
123+
});
124+
125+
it("ignores plain text, channels, and broadcasts", () => {
126+
expect(discordTextHasTargetedMention("ping @alice")).toBe(false);
127+
expect(discordTextHasTargetedMention("see <#789>")).toBe(false);
128+
expect(discordTextHasTargetedMention("heads up @everyone @here")).toBe(false);
129+
});
130+
});
131+
132+
describe("discordTextHasBroadcastMention", () => {
133+
it("detects @everyone and @here", () => {
134+
expect(discordTextHasBroadcastMention("heads up @everyone")).toBe(true);
135+
expect(discordTextHasBroadcastMention("@here please")).toBe(true);
136+
});
137+
138+
it("ignores targeted mentions and lookalikes", () => {
139+
expect(discordTextHasBroadcastMention("ping <@123>")).toBe(false);
140+
expect(discordTextHasBroadcastMention("mail me at a@everyones")).toBe(false);
141+
});
142+
});

‎extensions/discord/src/mentions.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const MARKDOWN_CODE_SEGMENT_PATTERN = /```[\s\S]*?```|`[^`\n]*`/g;
1111
const MENTION_CANDIDATE_PATTERN = /(^|[\s([{"'.,;:!?])@([a-z0-9_.-]{2,32}(?:#[0-9]{4})?)/gi;
1212
const DISCORD_RESERVED_MENTIONS = new Set(["everyone", "here"]);
1313
const DISCORD_DISCRIMINATOR_SUFFIX = /#\d{4}$/;
14+
const DISCORD_TARGETED_MENTION_PATTERN = /<@!?\d+>|<@&\d+>/;
15+
const DISCORD_BROADCAST_MENTION_PATTERN = /@(everyone|here)\b/;
1416

1517
function normalizeSnowflake(value: string | number | bigint): string | null {
1618
const text = normalizeOptionalStringifiedId(value) ?? "";
@@ -145,3 +147,13 @@ export function rewriteDiscordKnownMentions(
145147
rewritten += rewritePlainTextMentions(text.slice(offset), params);
146148
return rewritten;
147149
}
150+
151+
/** Whether text carries a Discord user/role mention (`<@id>`, `<@!id>`, `<@&id>`) that pings when sent fresh. */
152+
export function discordTextHasTargetedMention(text: string): boolean {
153+
return DISCORD_TARGETED_MENTION_PATTERN.test(text);
154+
}
155+
156+
/** Whether text carries an `@everyone`/`@here` broadcast mention. */
157+
export function discordTextHasBroadcastMention(text: string): boolean {
158+
return DISCORD_BROADCAST_MENTION_PATTERN.test(text);
159+
}

‎extensions/discord/src/monitor/message-handler.process.test.ts‎

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,95 @@ describe("processDiscordMessage draft streaming", () => {
19521952
expectSinglePreviewEdit();
19531953
});
19541954

1955+
it("delivers a fresh message instead of a preview edit when the final reply resolves a mention alias", async () => {
1956+
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
1957+
await params?.dispatcher.sendFinalReply({ text: "On it @Sentinel" });
1958+
return { queuedFinal: true, counts: { final: 1, tool: 0, block: 0 } };
1959+
});
1960+
1961+
const ctx = await createAutomaticSourceDeliveryContext({
1962+
discordConfig: { streamMode: "partial", maxLinesPerMessage: 5 },
1963+
cfg: {
1964+
channels: { discord: { mentionAliases: { Sentinel: "1485891428809707651" } } },
1965+
},
1966+
});
1967+
1968+
await runProcessDiscordMessage(ctx);
1969+
1970+
// Discord only fires mention notifications on create, never on edits, so the
1971+
// streamed preview must be abandoned and the mention delivered fresh.
1972+
expect(editMessageDiscord).not.toHaveBeenCalled();
1973+
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
1974+
});
1975+
1976+
it("delivers a fresh message instead of a preview edit for a literal user mention in the final reply", async () => {
1977+
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
1978+
await params?.dispatcher.sendFinalReply({ text: "On it <@1485891428809707651>" });
1979+
return { queuedFinal: true, counts: { final: 1, tool: 0, block: 0 } };
1980+
});
1981+
1982+
const ctx = await createAutomaticSourceDeliveryContext({
1983+
discordConfig: { streamMode: "partial", maxLinesPerMessage: 5 },
1984+
});
1985+
1986+
await runProcessDiscordMessage(ctx);
1987+
1988+
expect(editMessageDiscord).not.toHaveBeenCalled();
1989+
expect(deliverDiscordReply).toHaveBeenCalledTimes(1);
1990+
});
1991+
1992+
it("still finalizes via preview edit when an unaliased handle stays plain text", async () => {
1993+
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
1994+
await params?.dispatcher.sendFinalReply({ text: "On it @Sentinel" });
1995+
return { queuedFinal: true, counts: { final: 1, tool: 0, block: 0 } };
1996+
});
1997+
1998+
const ctx = await createAutomaticSourceDeliveryContext({
1999+
discordConfig: { streamMode: "partial", maxLinesPerMessage: 5 },
2000+
});
2001+
2002+
await runProcessDiscordMessage(ctx);
2003+
2004+
expect(editMessageDiscord).toHaveBeenCalledTimes(1);
2005+
expect(deliverDiscordReply).not.toHaveBeenCalled();
2006+
});
2007+
2008+
it("still finalizes via preview edit for broadcast mentions like @everyone", async () => {
2009+
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
2010+
await params?.dispatcher.sendFinalReply({ text: "heads up @everyone" });
2011+
return { queuedFinal: true, counts: { final: 1, tool: 0, block: 0 } };
2012+
});
2013+
2014+
const ctx = await createAutomaticSourceDeliveryContext({
2015+
discordConfig: { streamMode: "partial", maxLinesPerMessage: 5 },
2016+
});
2017+
2018+
await runProcessDiscordMessage(ctx);
2019+
2020+
expect(editMessageDiscord).toHaveBeenCalledTimes(1);
2021+
expect(deliverDiscordReply).not.toHaveBeenCalled();
2022+
});
2023+
2024+
it("still finalizes via preview edit when a targeted mention is mixed with @everyone", async () => {
2025+
dispatchInboundMessage.mockImplementationOnce(async (params?: DispatchInboundParams) => {
2026+
await params?.dispatcher.sendFinalReply({ text: "heads up @Sentinel @everyone" });
2027+
return { queuedFinal: true, counts: { final: 1, tool: 0, block: 0 } };
2028+
});
2029+
2030+
const ctx = await createAutomaticSourceDeliveryContext({
2031+
discordConfig: { streamMode: "partial", maxLinesPerMessage: 5 },
2032+
cfg: {
2033+
channels: { discord: { mentionAliases: { Sentinel: "1485891428809707651" } } },
2034+
},
2035+
});
2036+
2037+
await runProcessDiscordMessage(ctx);
2038+
2039+
// Mixed targeted + broadcast must not escalate into a create that pings @everyone.
2040+
expect(editMessageDiscord).toHaveBeenCalledTimes(1);
2041+
expect(deliverDiscordReply).not.toHaveBeenCalled();
2042+
});
2043+
19552044
it("accepts streaming=true alias for partial preview mode", async () => {
19562045
await runSingleChunkFinalScenario({ streaming: true, maxLinesPerMessage: 5 });
19572046
expectSinglePreviewEdit();

‎extensions/discord/src/monitor/message-handler.process.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ import {
4848
resolveSessionStoreEntry,
4949
resolveStorePath,
5050
} from "openclaw/plugin-sdk/session-store-runtime";
51-
import { resolveDiscordMaxLinesPerMessage } from "../accounts.js";
51+
import { resolveDiscordAccount, resolveDiscordMaxLinesPerMessage } from "../accounts.js";
5252
import { createDiscordRestClient } from "../client.js";
5353
import { beginDiscordInboundEventDeliveryCorrelation } from "../inbound-event-delivery.js";
54+
import {
55+
discordTextHasBroadcastMention,
56+
discordTextHasTargetedMention,
57+
rewriteDiscordKnownMentions,
58+
} from "../mentions.js";
5459
import { removeReactionDiscord } from "../send.js";
5560
import { editMessageDiscord } from "../send.messages.js";
5661
import { resolveDiscordTargetChannelId } from "../send.shared.js";
@@ -713,6 +718,17 @@ async function processDiscordMessageInner(
713718
) {
714719
return undefined;
715720
}
721+
// Discord pings only on create, not edits: send a targeted mention fresh, but keep mixed @everyone/@here in place so the create cannot escalate a broadcast.
722+
const rewrittenFinal = rewriteDiscordKnownMentions(previewFinalText, {
723+
accountId,
724+
mentionAliases: resolveDiscordAccount({ cfg, accountId }).config.mentionAliases,
725+
});
726+
if (
727+
discordTextHasTargetedMention(rewrittenFinal) &&
728+
!discordTextHasBroadcastMention(rewrittenFinal)
729+
) {
730+
return undefined;
731+
}
716732
return {
717733
content: previewFinalText,
718734
...(finalPreviewFlags ? { flags: finalPreviewFlags } : {}),

0 commit comments

Comments
 (0)