From 756597e6ad5827b352a2b95de18a558779442de2 Mon Sep 17 00:00:00 2001 From: Tak Hoffman <781889+Takhoffman@users.noreply.github.com> Date: Fri, 3 Apr 2026 15:09:14 -0500 Subject: [PATCH] fix: honor discord default directory cache account --- extensions/discord/src/directory-live.ts | 10 ++++---- extensions/discord/src/targets.test.ts | 32 ++++++++++++++++++++++++ extensions/discord/src/targets.ts | 7 +++++- 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/extensions/discord/src/directory-live.ts b/extensions/discord/src/directory-live.ts index 67a8e908f7c..f6c5bb2bb3d 100644 --- a/extensions/discord/src/directory-live.ts +++ b/extensions/discord/src/directory-live.ts @@ -12,7 +12,7 @@ type DiscordGuild = { id: string; name: string }; type DiscordUser = { id: string; username: string; global_name?: string; bot?: boolean }; type DiscordMember = { user: DiscordUser; nick?: string | null }; type DiscordChannel = { id: string; name?: string | null }; -type DiscordDirectoryAccess = { token: string; query: string }; +type DiscordDirectoryAccess = { token: string; query: string; accountId: string }; function normalizeQuery(value?: string | null): string { return value?.trim().toLowerCase() ?? ""; @@ -30,7 +30,7 @@ function resolveDiscordDirectoryAccess( if (!token) { return null; } - return { token, query: normalizeQuery(params.query) }; + return { token, query: normalizeQuery(params.query), accountId: account.accountId }; } async function listDiscordGuilds(token: string): Promise { @@ -45,7 +45,7 @@ export async function listDiscordDirectoryGroupsLive( if (!access) { return []; } - const { token, query } = access; + const { token, query, accountId } = access; const guilds = await listDiscordGuilds(token); const rows: ChannelDirectoryEntry[] = []; @@ -82,7 +82,7 @@ export async function listDiscordDirectoryPeersLive( if (!access) { return []; } - const { token, query } = access; + const { token, query, accountId } = access; if (!query) { return []; } @@ -106,7 +106,7 @@ export async function listDiscordDirectoryPeersLive( continue; } rememberDiscordDirectoryUser({ - accountId: params.accountId, + accountId, userId: user.id, handles: [ user.username, diff --git a/extensions/discord/src/targets.test.ts b/extensions/discord/src/targets.test.ts index 3175bd4c443..d5388970b46 100644 --- a/extensions/discord/src/targets.test.ts +++ b/extensions/discord/src/targets.test.ts @@ -1,5 +1,9 @@ import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime"; import { beforeEach, describe, expect, it, vi } from "vitest"; +import { + __resetDiscordDirectoryCacheForTest, + resolveDiscordDirectoryUserId, +} from "./directory-cache.js"; import * as directoryLive from "./directory-live.js"; import { resolveDiscordGroupRequireMention, @@ -76,6 +80,7 @@ describe("resolveDiscordTarget", () => { beforeEach(() => { vi.restoreAllMocks(); + __resetDiscordDirectoryCacheForTest(); }); it("returns a resolved user for usernames", async () => { @@ -102,6 +107,33 @@ describe("resolveDiscordTarget", () => { ).resolves.toMatchObject({ kind: "user", id: "123" }); expect(listPeers).not.toHaveBeenCalled(); }); + + it("caches username lookups under the configured default account when accountId is omitted", async () => { + const cfg = { + channels: { + discord: { + defaultAccount: "work", + accounts: { + work: { + token: "discord-work", + }, + }, + }, + }, + } as OpenClawConfig; + + vi.spyOn(directoryLive, "listDiscordDirectoryPeersLive").mockResolvedValueOnce([ + { kind: "user", id: "user:999", name: "Jane" } as const, + ]); + + await expect(resolveDiscordTarget("jane", { cfg })).resolves.toMatchObject({ + kind: "user", + id: "999", + normalized: "user:999", + }); + expect(resolveDiscordDirectoryUserId({ accountId: "work", handle: "jane" })).toBe("999"); + expect(resolveDiscordDirectoryUserId({ accountId: "default", handle: "jane" })).toBeUndefined(); + }); }); describe("normalizeDiscordMessagingTarget", () => { diff --git a/extensions/discord/src/targets.ts b/extensions/discord/src/targets.ts index cb04a96d914..acaa7b75da6 100644 --- a/extensions/discord/src/targets.ts +++ b/extensions/discord/src/targets.ts @@ -7,6 +7,7 @@ import { type MessagingTargetParseOptions, } from "openclaw/plugin-sdk/channel-targets"; import type { DirectoryConfigParams } from "openclaw/plugin-sdk/directory-runtime"; +import { resolveDiscordAccount } from "./accounts.js"; import { rememberDiscordDirectoryUser } from "./directory-cache.js"; import { listDiscordDirectoryPeersLive } from "./directory-live.js"; @@ -100,8 +101,12 @@ export async function resolveDiscordTarget( if (match && match.kind === "user") { // Extract user ID from the directory entry (format: "user:") const userId = match.id.replace(/^user:/, ""); - rememberDiscordDirectoryUser({ + const resolvedAccountId = resolveDiscordAccount({ + cfg: options.cfg, accountId: options.accountId, + }).accountId; + rememberDiscordDirectoryUser({ + accountId: resolvedAccountId, userId, handles: [trimmed, match.name, match.handle], });