Skip to content

Commit 6286ef5

Browse files
committed
fix: honor discord default guild action account
1 parent 9224afc commit 6286ef5

3 files changed

Lines changed: 56 additions & 4 deletions

File tree

extensions/discord/src/actions/runtime.guild.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
2+
import { resolveDefaultDiscordAccountId } from "../accounts.js";
23
import { getPresence } from "../monitor/presence-cache.js";
34
import {
45
type ActionGate,
@@ -8,6 +9,7 @@ import {
89
readStringArrayParam,
910
readStringParam,
1011
type DiscordActionConfig,
12+
type OpenClawConfig,
1113
} from "../runtime-api.js";
1214
import {
1315
addRoleDiscord,
@@ -92,6 +94,7 @@ export async function handleDiscordGuildAction(
9294
action: string,
9395
params: Record<string, unknown>,
9496
isActionEnabled: ActionGate<DiscordActionConfig>,
97+
cfg?: OpenClawConfig,
9598
): Promise<AgentToolResult<unknown>> {
9699
const accountId = readStringParam(params, "accountId");
97100
switch (action) {
@@ -105,10 +108,13 @@ export async function handleDiscordGuildAction(
105108
const userId = readStringParam(params, "userId", {
106109
required: true,
107110
});
108-
const member = accountId
109-
? await discordGuildActionRuntime.fetchMemberInfoDiscord(guildId, userId, { accountId })
111+
const effectiveAccountId = accountId ?? (cfg ? resolveDefaultDiscordAccountId(cfg) : undefined);
112+
const member = effectiveAccountId
113+
? await discordGuildActionRuntime.fetchMemberInfoDiscord(guildId, userId, {
114+
accountId: effectiveAccountId,
115+
})
110116
: await discordGuildActionRuntime.fetchMemberInfoDiscord(guildId, userId);
111-
const presence = getPresence(accountId, userId);
117+
const presence = getPresence(effectiveAccountId, userId);
112118
const activities = presence?.activities ?? undefined;
113119
const status = presence?.status ?? undefined;
114120
return jsonResult({ ok: true, member, ...(presence ? { status, activities } : {}) });

extensions/discord/src/actions/runtime.test.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-runtime";
22
import type { DiscordActionConfig } from "openclaw/plugin-sdk/config-runtime";
33
import { beforeEach, describe, expect, it, vi } from "vitest";
4+
import { clearPresences, setPresence } from "../monitor/presence-cache.js";
45
import { discordGuildActionRuntime, handleDiscordGuildAction } from "./runtime.guild.js";
56
import { handleDiscordAction } from "./runtime.js";
67
import {
@@ -88,6 +89,7 @@ const moderationEnabled = (key: keyof DiscordActionConfig) => key === "moderatio
8889

8990
beforeEach(() => {
9091
vi.clearAllMocks();
92+
clearPresences();
9193
Object.assign(
9294
discordMessagingActionRuntime,
9395
originalDiscordMessagingActionRuntime,
@@ -487,6 +489,50 @@ describe("handleDiscordMessagingAction", () => {
487489
});
488490
});
489491

492+
describe("handleDiscordGuildAction", () => {
493+
it("uses configured defaultAccount for omitted memberInfo presence lookup", async () => {
494+
setPresence("work", "U1", {
495+
user: { id: "U1" },
496+
guild_id: "G1",
497+
status: "online",
498+
activities: [],
499+
client_status: {},
500+
} as never);
501+
502+
discordGuildActionRuntime.fetchMemberInfoDiscord = vi.fn(async () => ({ user: { id: "U1" } })) as never;
503+
504+
const result = await handleDiscordGuildAction(
505+
"memberInfo",
506+
{
507+
guildId: "G1",
508+
userId: "U1",
509+
},
510+
enableAllActions,
511+
{
512+
channels: {
513+
discord: {
514+
defaultAccount: "work",
515+
accounts: {
516+
work: { token: "token-work" },
517+
},
518+
},
519+
},
520+
} as OpenClawConfig,
521+
);
522+
523+
expect(discordGuildActionRuntime.fetchMemberInfoDiscord).toHaveBeenCalledWith("G1", "U1", {
524+
accountId: "work",
525+
});
526+
expect(result.details).toEqual(
527+
expect.objectContaining({
528+
ok: true,
529+
status: "online",
530+
activities: [],
531+
}),
532+
);
533+
});
534+
});
535+
490536
const channelsEnabled = (key: keyof DiscordActionConfig) => key === "channels";
491537
const channelsDisabled = () => false;
492538

extensions/discord/src/actions/runtime.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ export async function handleDiscordAction(
6969
return await handleDiscordMessagingAction(action, params, isActionEnabled, options, cfg);
7070
}
7171
if (guildActions.has(action)) {
72-
return await handleDiscordGuildAction(action, params, isActionEnabled);
72+
return await handleDiscordGuildAction(action, params, isActionEnabled, cfg);
7373
}
7474
if (moderationActions.has(action)) {
7575
return await handleDiscordModerationAction(action, params, isActionEnabled);

0 commit comments

Comments
 (0)