diff --git a/extensions/discord/src/send.outbound.ts b/extensions/discord/src/send.outbound.ts index 08fcac3a630..6d17d5d7212 100644 --- a/extensions/discord/src/send.outbound.ts +++ b/extensions/discord/src/send.outbound.ts @@ -365,15 +365,15 @@ export async function sendWebhookMessageDiscord( throw new Error("Discord webhook id/token are required"); } - const rewrittenText = rewriteDiscordKnownMentions(text, { - accountId: opts.accountId, - }); const replyTo = typeof opts.replyTo === "string" ? opts.replyTo.trim() : ""; const messageReference = replyTo ? { message_id: replyTo, fail_if_not_exists: false } : undefined; const { account, proxyFetch } = resolveDiscordClientAccountContext({ cfg: opts.cfg, accountId: opts.accountId, }); + const rewrittenText = rewriteDiscordKnownMentions(text, { + accountId: account.accountId, + }); const response = await (proxyFetch ?? fetch)( resolveWebhookExecutionUrl({ @@ -430,11 +430,16 @@ export async function sendStickerDiscord( stickerIds: string[], opts: DiscordSendOpts & { content?: string } = {}, ): Promise { + const cfg = opts.cfg ?? loadConfig(); + const accountInfo = resolveDiscordAccount({ + cfg, + accountId: opts.accountId, + }); const { rest, request, channelId } = await resolveDiscordSendTarget(to, opts); const content = opts.content?.trim(); const rewrittenContent = content ? rewriteDiscordKnownMentions(content, { - accountId: opts.accountId, + accountId: accountInfo.accountId, }) : undefined; const stickers = normalizeStickerIds(stickerIds); @@ -456,11 +461,16 @@ export async function sendPollDiscord( poll: PollInput, opts: DiscordSendOpts & { content?: string } = {}, ): Promise { + const cfg = opts.cfg ?? loadConfig(); + const accountInfo = resolveDiscordAccount({ + cfg, + accountId: opts.accountId, + }); const { rest, request, channelId } = await resolveDiscordSendTarget(to, opts); const content = opts.content?.trim(); const rewrittenContent = content ? rewriteDiscordKnownMentions(content, { - accountId: opts.accountId, + accountId: accountInfo.accountId, }) : undefined; if (poll.durationSeconds !== undefined) { diff --git a/extensions/discord/src/send.sends-basic-channel-messages.test.ts b/extensions/discord/src/send.sends-basic-channel-messages.test.ts index 74e0a17cc7f..a60b98d023a 100644 --- a/extensions/discord/src/send.sends-basic-channel-messages.test.ts +++ b/extensions/discord/src/send.sends-basic-channel-messages.test.ts @@ -126,6 +126,40 @@ describe("sendMessageDiscord", () => { ); }); + it("uses configured defaultAccount for cached mention rewriting when accountId is omitted", async () => { + rememberDiscordDirectoryUser({ + accountId: "work", + userId: "222333444555666777", + handles: ["Alice"], + }); + const { rest, postMock, getMock } = makeDiscordRest(); + getMock.mockResolvedValueOnce({ type: ChannelType.GuildText }); + postMock.mockResolvedValue({ + id: "msg1", + channel_id: "789", + }); + await sendMessageDiscord("channel:789", "ping @Alice", { + rest, + token: "t", + cfg: { + channels: { + discord: { + defaultAccount: "work", + accounts: { + work: { + token: "Bot work-token", // pragma: allowlist secret + }, + }, + }, + }, + } as never, + }); + expect(postMock).toHaveBeenCalledWith( + Routes.channelMessages("789"), + expect.objectContaining({ body: { content: "ping <@222333444555666777>" } }), + ); + }); + it("auto-creates a forum thread when target is a Forum channel", async () => { const { rest, postMock, getMock } = makeDiscordRest(); // Channel type lookup returns a Forum channel.