Skip to content

Commit 3932a60

Browse files
authored
refactor(discord): privatize internal plugin surfaces (#108003)
* refactor(discord): privatize internal plugin surfaces * chore(deadcode): update export baseline
1 parent decdaf1 commit 3932a60

39 files changed

Lines changed: 687 additions & 794 deletions
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import {
2+
addRoleDiscord,
3+
banMemberDiscord,
4+
canManageGuildMemberRoleDiscord,
5+
canManageGuildRoleDiscord,
6+
createChannelDiscord,
7+
createScheduledEventDiscord,
8+
deleteChannelDiscord,
9+
editChannelDiscord,
10+
fetchChannelInfoDiscord,
11+
fetchMemberInfoDiscord,
12+
fetchRoleInfoDiscord,
13+
fetchVoiceStatusDiscord,
14+
hasAnyChannelPermissionDiscord,
15+
hasAnyGuildPermissionDiscord,
16+
kickMemberDiscord,
17+
listGuildChannelsDiscord,
18+
listGuildEmojisDiscord,
19+
listScheduledEventsDiscord,
20+
moveChannelDiscord,
21+
removeChannelPermissionDiscord,
22+
removeRoleDiscord,
23+
resolveEventCoverImage,
24+
setChannelPermissionDiscord,
25+
timeoutMemberDiscord,
26+
uploadEmojiDiscord,
27+
uploadStickerDiscord,
28+
} from "../send.js";
29+
30+
export const discordGuildActionRuntime = {
31+
addRoleDiscord,
32+
canManageGuildRoleDiscord,
33+
canManageGuildMemberRoleDiscord,
34+
createChannelDiscord,
35+
createScheduledEventDiscord,
36+
resolveEventCoverImage,
37+
deleteChannelDiscord,
38+
editChannelDiscord,
39+
fetchChannelInfoDiscord,
40+
fetchMemberInfoDiscord,
41+
hasAnyChannelPermissionDiscord,
42+
hasAnyGuildPermissionDiscord,
43+
fetchRoleInfoDiscord,
44+
fetchVoiceStatusDiscord,
45+
listGuildChannelsDiscord,
46+
listGuildEmojisDiscord,
47+
listScheduledEventsDiscord,
48+
moveChannelDiscord,
49+
removeChannelPermissionDiscord,
50+
removeRoleDiscord,
51+
setChannelPermissionDiscord,
52+
uploadEmojiDiscord,
53+
uploadStickerDiscord,
54+
};
55+
56+
export const discordModerationActionRuntime = {
57+
banMemberDiscord,
58+
hasAnyGuildPermissionDiscord,
59+
kickMemberDiscord,
60+
timeoutMemberDiscord,
61+
};

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

Lines changed: 1 addition & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,7 @@ import {
1212
type DiscordActionConfig,
1313
type OpenClawConfig,
1414
} from "../runtime-api.js";
15-
import {
16-
addRoleDiscord,
17-
canManageGuildRoleDiscord,
18-
canManageGuildMemberRoleDiscord,
19-
createChannelDiscord,
20-
createScheduledEventDiscord,
21-
deleteChannelDiscord,
22-
editChannelDiscord,
23-
fetchChannelInfoDiscord,
24-
fetchMemberInfoDiscord,
25-
hasAnyChannelPermissionDiscord,
26-
hasAnyGuildPermissionDiscord,
27-
fetchRoleInfoDiscord,
28-
fetchVoiceStatusDiscord,
29-
listGuildChannelsDiscord,
30-
listGuildEmojisDiscord,
31-
listScheduledEventsDiscord,
32-
moveChannelDiscord,
33-
removeChannelPermissionDiscord,
34-
removeRoleDiscord,
35-
setChannelPermissionDiscord,
36-
uploadEmojiDiscord,
37-
uploadStickerDiscord,
38-
resolveEventCoverImage,
39-
} from "../send.js";
15+
import { discordGuildActionRuntime } from "./runtime-deps.js";
4016
import {
4117
createDiscordMessagingActionContext,
4218
type DiscordMessagingActionOptions,
@@ -48,32 +24,6 @@ import {
4824
readDiscordChannelMoveParams,
4925
} from "./runtime.shared.js";
5026

51-
export const discordGuildActionRuntime = {
52-
addRoleDiscord,
53-
canManageGuildRoleDiscord,
54-
canManageGuildMemberRoleDiscord,
55-
createChannelDiscord,
56-
createScheduledEventDiscord,
57-
resolveEventCoverImage,
58-
deleteChannelDiscord,
59-
editChannelDiscord,
60-
fetchChannelInfoDiscord,
61-
fetchMemberInfoDiscord,
62-
hasAnyChannelPermissionDiscord,
63-
hasAnyGuildPermissionDiscord,
64-
fetchRoleInfoDiscord,
65-
fetchVoiceStatusDiscord,
66-
listGuildChannelsDiscord,
67-
listGuildEmojisDiscord,
68-
listScheduledEventsDiscord,
69-
moveChannelDiscord,
70-
removeChannelPermissionDiscord,
71-
removeRoleDiscord,
72-
setChannelPermissionDiscord,
73-
uploadEmojiDiscord,
74-
uploadStickerDiscord,
75-
};
76-
7727
type DiscordRoleMutationOpts = { cfg: OpenClawConfig; accountId?: string };
7828
type DiscordRoleMutation = (
7929
params: {
@@ -735,4 +685,3 @@ export async function handleDiscordGuildAction(
735685
throw new Error(`Unknown action: ${action}`);
736686
}
737687
}
738-
/* oxlint-disable max-lines -- TODO: split this grandfathered oversized file. */

‎extensions/discord/src/actions/runtime.moderation.authz.test.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@ import { PermissionFlagsBits } from "discord-api-types/v10";
33
import type { DiscordActionConfig } from "openclaw/plugin-sdk/config-contracts";
44
import { beforeEach, describe, expect, it, vi } from "vitest";
55
import { EMPTY_DISCORD_TEST_CONFIG } from "../test-support/config.js";
6-
import {
7-
discordModerationActionRuntime,
8-
handleDiscordModerationAction,
9-
} from "./runtime.moderation.js";
6+
import { discordModerationActionRuntime } from "./runtime-deps.js";
7+
import { handleDiscordModerationAction } from "./runtime.moderation.js";
108

119
const originalDiscordModerationActionRuntime = { ...discordModerationActionRuntime };
1210
const banMemberDiscord = vi.fn(async () => ({ ok: true }));

‎extensions/discord/src/actions/runtime.moderation.ts‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,14 @@ import {
77
type DiscordActionConfig,
88
type OpenClawConfig,
99
} from "../runtime-api.js";
10-
import {
11-
banMemberDiscord,
12-
hasAnyGuildPermissionDiscord,
13-
kickMemberDiscord,
14-
timeoutMemberDiscord,
15-
} from "../send.js";
10+
import { discordModerationActionRuntime } from "./runtime-deps.js";
1611
import {
1712
isDiscordModerationAction,
1813
readDiscordModerationCommand,
1914
requiredGuildPermissionForModerationAction,
2015
} from "./runtime.moderation-shared.js";
2116
import { createDiscordActionOptions } from "./runtime.shared.js";
2217

23-
export const discordModerationActionRuntime = {
24-
banMemberDiscord,
25-
hasAnyGuildPermissionDiscord,
26-
kickMemberDiscord,
27-
timeoutMemberDiscord,
28-
};
29-
3018
async function verifySenderModerationPermission(params: {
3119
guildId: string;
3220
senderUserId?: string;

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import type { DiscordActionConfig } from "openclaw/plugin-sdk/config-contracts";
66
import { beforeEach, describe, expect, it, vi } from "vitest";
77
import { clearPresences, setPresence } from "../monitor/presence-cache.js";
88
import { DiscordThreadInitialMessageError } from "../send.js";
9-
import { discordGuildActionRuntime, handleDiscordGuildAction } from "./runtime.guild.js";
9+
import { discordGuildActionRuntime, discordModerationActionRuntime } from "./runtime-deps.js";
10+
import { handleDiscordGuildAction } from "./runtime.guild.js";
1011
import { handleDiscordAction } from "./runtime.js";
1112
import { handleDiscordMessagingAction } from "./runtime.messaging.js";
1213
import { discordMessagingActionRuntime } from "./runtime.messaging.runtime.js";
13-
import {
14-
discordModerationActionRuntime,
15-
handleDiscordModerationAction,
16-
} from "./runtime.moderation.js";
14+
import { handleDiscordModerationAction } from "./runtime.moderation.js";
1715

1816
const originalDiscordMessagingActionRuntime = { ...discordMessagingActionRuntime };
1917
const originalDiscordGuildActionRuntime = { ...discordGuildActionRuntime };
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { resolveGlobalMap } from "openclaw/plugin-sdk/global-singleton";
2+
import type { DiscordComponentEntry, DiscordModalEntry } from "./components.js";
3+
4+
type PersistedDiscordRegistryEntry<T extends { id: string }> = {
5+
version: 1;
6+
entry: T;
7+
};
8+
9+
type DiscordPersistentStore<T> = {
10+
register(key: string, value: T, opts?: { ttlMs?: number }): Promise<void>;
11+
lookup(key: string): Promise<T | undefined>;
12+
consume(key: string): Promise<T | undefined>;
13+
delete(key: string): Promise<boolean>;
14+
};
15+
16+
export type DiscordRegistryStore<T extends { id: string }> = DiscordPersistentStore<
17+
PersistedDiscordRegistryEntry<T>
18+
>;
19+
20+
export const discordComponentRegistryState = {
21+
componentEntries: resolveGlobalMap<string, DiscordComponentEntry>(
22+
Symbol.for("openclaw.discord.componentEntries"),
23+
),
24+
modalEntries: resolveGlobalMap<string, DiscordModalEntry>(
25+
Symbol.for("openclaw.discord.modalEntries"),
26+
),
27+
persistentComponentStore: undefined as DiscordRegistryStore<DiscordComponentEntry> | undefined,
28+
persistentModalStore: undefined as DiscordRegistryStore<DiscordModalEntry> | undefined,
29+
persistentRegistryDisabled: false,
30+
reset(): void {
31+
this.componentEntries.clear();
32+
this.modalEntries.clear();
33+
this.persistentComponentStore = undefined;
34+
this.persistentModalStore = undefined;
35+
this.persistentRegistryDisabled = false;
36+
},
37+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { discordComponentRegistryState } from "./components-registry-state.js";
2+
3+
export function clearDiscordComponentEntriesForTest(): void {
4+
discordComponentRegistryState.reset();
5+
}

‎extensions/discord/src/components-registry.ts‎

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
// Discord plugin module implements components registry behavior.
2-
import { resolveGlobalMap } from "openclaw/plugin-sdk/global-singleton";
32
import {
43
asDateTimestampMs,
54
isFutureDateTimestampMs,
65
resolveDateTimestampMs,
76
resolveExpiresAtMsFromDurationMs,
87
} from "openclaw/plugin-sdk/number-runtime";
98
import { uniqueStrings } from "openclaw/plugin-sdk/string-coerce-runtime";
9+
import {
10+
discordComponentRegistryState,
11+
type DiscordRegistryStore,
12+
} from "./components-registry-state.js";
1013
import type { DiscordComponentEntry, DiscordModalEntry } from "./components.js";
1114
import { getOptionalDiscordRuntime } from "./runtime.js";
1215

@@ -15,41 +18,17 @@ const PERSISTENT_COMPONENT_NAMESPACE = "discord.components";
1518
const PERSISTENT_MODAL_NAMESPACE = "discord.modals";
1619
const PERSISTENT_COMPONENT_MAX_ENTRIES = 500;
1720
const PERSISTENT_MODAL_MAX_ENTRIES = 500;
18-
const DISCORD_COMPONENT_ENTRIES_KEY = Symbol.for("openclaw.discord.componentEntries");
19-
const DISCORD_MODAL_ENTRIES_KEY = Symbol.for("openclaw.discord.modalEntries");
20-
2121
type PersistedDiscordRegistryEntry<T extends { id: string }> = {
2222
version: 1;
2323
entry: T;
2424
};
2525

26-
type DiscordPersistentStore<T> = {
27-
register(key: string, value: T, opts?: { ttlMs?: number }): Promise<void>;
28-
lookup(key: string): Promise<T | undefined>;
29-
consume(key: string): Promise<T | undefined>;
30-
delete(key: string): Promise<boolean>;
31-
};
32-
33-
type DiscordRegistryStore<T extends { id: string }> = DiscordPersistentStore<
34-
PersistedDiscordRegistryEntry<T>
35-
>;
36-
37-
let componentEntries: Map<string, DiscordComponentEntry> | undefined;
38-
let modalEntries: Map<string, DiscordModalEntry> | undefined;
39-
let persistentComponentStore: DiscordRegistryStore<DiscordComponentEntry> | undefined;
40-
let persistentModalStore: DiscordRegistryStore<DiscordModalEntry> | undefined;
41-
let persistentRegistryDisabled = false;
42-
4326
function getComponentEntries(): Map<string, DiscordComponentEntry> {
44-
componentEntries ??= resolveGlobalMap<string, DiscordComponentEntry>(
45-
DISCORD_COMPONENT_ENTRIES_KEY,
46-
);
47-
return componentEntries;
27+
return discordComponentRegistryState.componentEntries;
4828
}
4929

5030
function getModalEntries(): Map<string, DiscordModalEntry> {
51-
modalEntries ??= resolveGlobalMap<string, DiscordModalEntry>(DISCORD_MODAL_ENTRIES_KEY);
52-
return modalEntries;
31+
return discordComponentRegistryState.modalEntries;
5332
}
5433

5534
function reportPersistentComponentRegistryError(error: unknown): void {
@@ -111,58 +90,58 @@ function formatRegistryErrorValue(value: unknown): string {
11190
}
11291

11392
function disablePersistentComponentRegistry(error: unknown): void {
114-
persistentRegistryDisabled = true;
115-
persistentComponentStore = undefined;
116-
persistentModalStore = undefined;
93+
discordComponentRegistryState.persistentRegistryDisabled = true;
94+
discordComponentRegistryState.persistentComponentStore = undefined;
95+
discordComponentRegistryState.persistentModalStore = undefined;
11796
reportPersistentComponentRegistryError(error);
11897
}
11998

12099
function getPersistentComponentStore(): DiscordRegistryStore<DiscordComponentEntry> | undefined {
121-
if (persistentRegistryDisabled) {
100+
if (discordComponentRegistryState.persistentRegistryDisabled) {
122101
return undefined;
123102
}
124-
if (persistentComponentStore) {
125-
return persistentComponentStore;
103+
if (discordComponentRegistryState.persistentComponentStore) {
104+
return discordComponentRegistryState.persistentComponentStore;
126105
}
127106
const runtime = getOptionalDiscordRuntime();
128107
if (!runtime) {
129108
return undefined;
130109
}
131110
try {
132-
persistentComponentStore = runtime.state.openKeyedStore<
111+
discordComponentRegistryState.persistentComponentStore = runtime.state.openKeyedStore<
133112
PersistedDiscordRegistryEntry<DiscordComponentEntry>
134113
>({
135114
namespace: PERSISTENT_COMPONENT_NAMESPACE,
136115
maxEntries: PERSISTENT_COMPONENT_MAX_ENTRIES,
137116
defaultTtlMs: DEFAULT_COMPONENT_TTL_MS,
138117
});
139-
return persistentComponentStore;
118+
return discordComponentRegistryState.persistentComponentStore;
140119
} catch (error) {
141120
disablePersistentComponentRegistry(error);
142121
return undefined;
143122
}
144123
}
145124

146125
function getPersistentModalStore(): DiscordRegistryStore<DiscordModalEntry> | undefined {
147-
if (persistentRegistryDisabled) {
126+
if (discordComponentRegistryState.persistentRegistryDisabled) {
148127
return undefined;
149128
}
150-
if (persistentModalStore) {
151-
return persistentModalStore;
129+
if (discordComponentRegistryState.persistentModalStore) {
130+
return discordComponentRegistryState.persistentModalStore;
152131
}
153132
const runtime = getOptionalDiscordRuntime();
154133
if (!runtime) {
155134
return undefined;
156135
}
157136
try {
158-
persistentModalStore = runtime.state.openKeyedStore<
137+
discordComponentRegistryState.persistentModalStore = runtime.state.openKeyedStore<
159138
PersistedDiscordRegistryEntry<DiscordModalEntry>
160139
>({
161140
namespace: PERSISTENT_MODAL_NAMESPACE,
162141
maxEntries: PERSISTENT_MODAL_MAX_ENTRIES,
163142
defaultTtlMs: DEFAULT_COMPONENT_TTL_MS,
164143
});
165-
return persistentModalStore;
144+
return discordComponentRegistryState.persistentModalStore;
166145
} catch (error) {
167146
disablePersistentComponentRegistry(error);
168147
return undefined;
@@ -370,7 +349,7 @@ export function registerDiscordComponentEntries(params: {
370349
});
371350
}
372351

373-
export function resolveDiscordComponentEntry(params: {
352+
function resolveDiscordComponentEntry(params: {
374353
id: string;
375354
consume?: boolean;
376355
}): DiscordComponentEntry | null {
@@ -402,7 +381,7 @@ export async function resolveDiscordComponentEntryWithPersistence(params: {
402381
return persisted;
403382
}
404383

405-
export function resolveDiscordModalEntry(params: {
384+
function resolveDiscordModalEntry(params: {
406385
id: string;
407386
consume?: boolean;
408387
}): DiscordModalEntry | null {
@@ -425,11 +404,3 @@ export async function resolveDiscordModalEntryWithPersistence(params: {
425404
openStore: getPersistentModalStore,
426405
});
427406
}
428-
429-
export function clearDiscordComponentEntries(): void {
430-
getComponentEntries().clear();
431-
getModalEntries().clear();
432-
persistentComponentStore = undefined;
433-
persistentModalStore = undefined;
434-
persistentRegistryDisabled = false;
435-
}

0 commit comments

Comments
 (0)