mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-01 15:00:24 +00:00
95 lines
3.0 KiB
TypeScript
95 lines
3.0 KiB
TypeScript
import {
|
|
createAccountActionGate,
|
|
createAccountListHelpers,
|
|
normalizeAccountId,
|
|
resolveAccountEntry,
|
|
} from "../../../src/plugin-sdk-internal/accounts.js";
|
|
import type {
|
|
OpenClawConfig,
|
|
DiscordAccountConfig,
|
|
DiscordActionConfig,
|
|
} from "../../../src/plugin-sdk-internal/discord.js";
|
|
import { resolveDiscordToken } from "./token.js";
|
|
|
|
export type ResolvedDiscordAccount = {
|
|
accountId: string;
|
|
enabled: boolean;
|
|
name?: string;
|
|
token: string;
|
|
tokenSource: "env" | "config" | "none";
|
|
config: DiscordAccountConfig;
|
|
};
|
|
|
|
const { listAccountIds, resolveDefaultAccountId } = createAccountListHelpers("discord");
|
|
export const listDiscordAccountIds = listAccountIds;
|
|
export const resolveDefaultDiscordAccountId = resolveDefaultAccountId;
|
|
|
|
export function resolveDiscordAccountConfig(
|
|
cfg: OpenClawConfig,
|
|
accountId: string,
|
|
): DiscordAccountConfig | undefined {
|
|
return resolveAccountEntry(cfg.channels?.discord?.accounts, accountId);
|
|
}
|
|
|
|
export function mergeDiscordAccountConfig(
|
|
cfg: OpenClawConfig,
|
|
accountId: string,
|
|
): DiscordAccountConfig {
|
|
const { accounts: _ignored, ...base } = (cfg.channels?.discord ?? {}) as DiscordAccountConfig & {
|
|
accounts?: unknown;
|
|
};
|
|
const account = resolveDiscordAccountConfig(cfg, accountId) ?? {};
|
|
return { ...base, ...account };
|
|
}
|
|
|
|
export function createDiscordActionGate(params: {
|
|
cfg: OpenClawConfig;
|
|
accountId?: string | null;
|
|
}): (key: keyof DiscordActionConfig, defaultValue?: boolean) => boolean {
|
|
const accountId = normalizeAccountId(params.accountId);
|
|
return createAccountActionGate({
|
|
baseActions: params.cfg.channels?.discord?.actions,
|
|
accountActions: resolveDiscordAccountConfig(params.cfg, accountId)?.actions,
|
|
});
|
|
}
|
|
|
|
export function resolveDiscordAccount(params: {
|
|
cfg: OpenClawConfig;
|
|
accountId?: string | null;
|
|
}): ResolvedDiscordAccount {
|
|
const accountId = normalizeAccountId(params.accountId);
|
|
const baseEnabled = params.cfg.channels?.discord?.enabled !== false;
|
|
const merged = mergeDiscordAccountConfig(params.cfg, accountId);
|
|
const accountEnabled = merged.enabled !== false;
|
|
const enabled = baseEnabled && accountEnabled;
|
|
const tokenResolution = resolveDiscordToken(params.cfg, { accountId });
|
|
return {
|
|
accountId,
|
|
enabled,
|
|
name: merged.name?.trim() || undefined,
|
|
token: tokenResolution.token,
|
|
tokenSource: tokenResolution.source,
|
|
config: merged,
|
|
};
|
|
}
|
|
|
|
export function resolveDiscordMaxLinesPerMessage(params: {
|
|
cfg: OpenClawConfig;
|
|
discordConfig?: DiscordAccountConfig | null;
|
|
accountId?: string | null;
|
|
}): number | undefined {
|
|
if (typeof params.discordConfig?.maxLinesPerMessage === "number") {
|
|
return params.discordConfig.maxLinesPerMessage;
|
|
}
|
|
return resolveDiscordAccount({
|
|
cfg: params.cfg,
|
|
accountId: params.accountId,
|
|
}).config.maxLinesPerMessage;
|
|
}
|
|
|
|
export function listEnabledDiscordAccounts(cfg: OpenClawConfig): ResolvedDiscordAccount[] {
|
|
return listDiscordAccountIds(cfg)
|
|
.map((accountId) => resolveDiscordAccount({ cfg, accountId }))
|
|
.filter((account) => account.enabled);
|
|
}
|