openclaw-matrix-multiaccounts/src/discord/monitor.slash.test.ts
Kentaro Kuribayashi 3e07bd8b48
feat(discord): add configurable privileged Gateway Intents (GuildPresences, GuildMembers) (#2266)
* feat(discord): add configurable privileged Gateway Intents (GuildPresences, GuildMembers)

Add support for optionally enabling Discord privileged Gateway Intents
via config, starting with GuildPresences and GuildMembers.

When `channels.discord.intents.presence` is set to true:
- GatewayIntents.GuildPresences is added to the gateway connection
- A PresenceUpdateListener caches user presence data in memory
- The member-info action includes user status and activities
  (e.g. Spotify listening activity) from the cache

This enables use cases like:
- Seeing what music a user is currently listening to
- Checking user online/offline/idle/dnd status
- Tracking user activities through the bot API

Both intents require Portal opt-in (Discord Developer Portal →
Privileged Gateway Intents) before they can be used.

Changes:
- config: add `channels.discord.intents.{presence,guildMembers}`
- provider: compute intents dynamically from config
- listeners: add DiscordPresenceListener (extends PresenceUpdateListener)
- presence-cache: simple in-memory Map<userId, GatewayPresenceUpdate>
- discord-actions-guild: include cached presence in member-info response
- schema: add labels and descriptions for new config fields

* fix(test): add PresenceUpdateListener to @buape/carbon mock

* Discord: scope presence cache by account

---------

Co-authored-by: kugutsushi <kugutsushi@clawd>
Co-authored-by: Shadow <hi@shadowing.dev>
2026-01-26 10:39:54 -06:00

102 lines
3.4 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { createReplyDispatcherWithTyping } from "../auto-reply/reply/reply-dispatcher.js";
const dispatchMock = vi.fn();
vi.mock("@buape/carbon", () => ({
ChannelType: { DM: "dm", GroupDM: "group" },
MessageType: {
ChatInputCommand: 1,
ContextMenuCommand: 2,
Default: 0,
},
Button: class {},
Command: class {},
Client: class {},
MessageCreateListener: class {},
MessageReactionAddListener: class {},
MessageReactionRemoveListener: class {},
PresenceUpdateListener: class {},
Row: class {
constructor(_components: unknown[]) {}
},
}));
vi.mock("../auto-reply/dispatch.js", async (importOriginal) => {
const actual = await importOriginal<typeof import("../auto-reply/dispatch.js")>();
return {
...actual,
dispatchInboundMessage: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithDispatcher: (...args: unknown[]) => dispatchMock(...args),
dispatchInboundMessageWithBufferedDispatcher: (...args: unknown[]) => dispatchMock(...args),
};
});
beforeEach(() => {
dispatchMock.mockReset().mockImplementation(async (params) => {
if ("dispatcher" in params && params.dispatcher) {
params.dispatcher.sendFinalReply({ text: "final reply" });
return { queuedFinal: true, counts: { tool: 0, block: 0, final: 1 } };
}
if ("dispatcherOptions" in params && params.dispatcherOptions) {
const { dispatcher, markDispatchIdle } = createReplyDispatcherWithTyping(
params.dispatcherOptions,
);
dispatcher.sendFinalReply({ text: "final reply" });
await dispatcher.waitForIdle();
markDispatchIdle();
return { queuedFinal: true, counts: dispatcher.getQueuedCounts() };
}
return { queuedFinal: false, counts: { tool: 0, block: 0, final: 0 } };
});
});
describe("discord native commands", () => {
it("skips tool results for native slash commands", { timeout: 60_000 }, async () => {
const { ChannelType } = await import("@buape/carbon");
const { createDiscordNativeCommand } = await import("./monitor.js");
const cfg = {
agents: {
defaults: {
model: "anthropic/claude-opus-4-5",
humanDelay: { mode: "off" },
workspace: "/tmp/clawd",
},
},
session: { store: "/tmp/clawdbot-sessions.json" },
discord: { dm: { enabled: true, policy: "open" } },
} as ReturnType<typeof import("../config/config.js").loadConfig>;
const command = createDiscordNativeCommand({
command: {
name: "verbose",
description: "Toggle verbose mode.",
acceptsArgs: true,
},
cfg,
discordConfig: cfg.discord,
accountId: "default",
sessionPrefix: "discord:slash",
ephemeralDefault: true,
});
const reply = vi.fn().mockResolvedValue(undefined);
const followUp = vi.fn().mockResolvedValue(undefined);
await command.run({
user: { id: "u1", username: "Ada", globalName: "Ada" },
channel: { type: ChannelType.DM },
guild: null,
rawData: { id: "i1" },
options: { getString: vi.fn().mockReturnValue("on") },
reply,
followUp,
});
expect(dispatchMock).toHaveBeenCalledTimes(1);
expect(reply).toHaveBeenCalledTimes(1);
expect(followUp).toHaveBeenCalledTimes(0);
expect(reply.mock.calls[0]?.[0]?.content).toContain("final");
});
});