From 0806edb7d9417abd0849801d987b052aede58a6f Mon Sep 17 00:00:00 2001 From: orion Date: Sun, 8 Mar 2026 06:15:19 +0000 Subject: [PATCH] feat(cache): include source and guildId metadata in channel member cache --- plugin/core/turn-bootstrap.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/plugin/core/turn-bootstrap.ts b/plugin/core/turn-bootstrap.ts index a73128e..d561cf6 100644 --- a/plugin/core/turn-bootstrap.ts +++ b/plugin/core/turn-bootstrap.ts @@ -19,7 +19,7 @@ function loadCache(api: OpenClawPluginApi): void { try { if (!fs.existsSync(p)) return; const raw = fs.readFileSync(p, "utf8"); - const parsed = JSON.parse(raw) as Record; + const parsed = JSON.parse(raw) as Record; for (const [channelId, rec] of Object.entries(parsed || {})) { const ids = Array.isArray(rec?.botAccountIds) ? rec.botAccountIds.filter(Boolean) : []; if (ids.length > 0) channelSeenAccounts.set(channelId, new Set(ids)); @@ -29,11 +29,32 @@ function loadCache(api: OpenClawPluginApi): void { } } +function inferGuildIdFromChannelId(api: OpenClawPluginApi, channelId: string): string | undefined { + const root = (api.config as Record) || {}; + const channels = (root.channels as Record) || {}; + const discord = (channels.discord as Record) || {}; + const accounts = (discord.accounts as Record>) || {}; + for (const rec of Object.values(accounts)) { + const chMap = (rec?.channels as Record> | undefined) || undefined; + if (!chMap) continue; + const direct = chMap[channelId]; + const prefixed = chMap[`channel:${channelId}`]; + const found = (direct || prefixed) as Record | undefined; + if (found && typeof found.guildId === "string" && found.guildId.trim()) return found.guildId.trim(); + } + return undefined; +} + function persistCache(api: OpenClawPluginApi): void { const p = cachePath(api); - const out: Record = {}; + const out: Record = {}; for (const [channelId, set] of channelSeenAccounts.entries()) { - out[channelId] = { botAccountIds: [...set], updatedAt: new Date().toISOString() }; + out[channelId] = { + botAccountIds: [...set], + updatedAt: new Date().toISOString(), + source: "dirigent/turn-bootstrap", + guildId: inferGuildIdFromChannelId(api, channelId), + }; } try { fs.mkdirSync(path.dirname(p), { recursive: true });