fix(doctor): warn on missing channel env tokens

This commit is contained in:
Peter Steinberger
2026-05-02 04:28:57 +01:00
parent d2f623d560
commit ac58dc2e92
8 changed files with 173 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
import { describe, expect, it } from "vitest";
import {
collectDiscordMissingEnvTokenWarnings,
collectDiscordNumericIdWarnings,
discordDoctor,
maybeRepairDiscordNumericIds,
@@ -361,4 +362,44 @@ describe("discord doctor", () => {
expect(warnings[0]).toContain("cannot be auto-repaired");
expect(warnings[1]).toContain("openclaw doctor --fix");
});
it("warns when default env fallback token is missing after migration", async () => {
const cfg = {
channels: {
discord: {
allowFrom: ["123"],
},
},
} as unknown as OpenClawConfig;
expect(collectDiscordMissingEnvTokenWarnings({ cfg, env: {} })).toEqual([
expect.stringContaining("DISCORD_BOT_TOKEN is absent"),
]);
expect(
collectDiscordMissingEnvTokenWarnings({ cfg, env: { DISCORD_BOT_TOKEN: "Bot tok" } }),
).toEqual([]);
expect(
await discordDoctor.collectPreviewWarnings?.({
cfg,
doctorFixCommand: "openclaw doctor --fix",
env: {},
}),
).toEqual([expect.stringContaining("DISCORD_BOT_TOKEN is absent")]);
});
it("does not warn about DISCORD_BOT_TOKEN when a non-default account is selected", () => {
const cfg = {
channels: {
discord: {
accounts: {
work: {
token: "Bot work-token",
},
},
},
},
} as unknown as OpenClawConfig;
expect(collectDiscordMissingEnvTokenWarnings({ cfg, env: {} })).toEqual([]);
});
});

View File

@@ -2,6 +2,8 @@ import { type ChannelDoctorAdapter } from "openclaw/plugin-sdk/channel-contract"
import type { OpenClawConfig } from "openclaw/plugin-sdk/config-types";
import { collectProviderDangerousNameMatchingScopes } from "openclaw/plugin-sdk/runtime-doctor";
import { normalizeOptionalString } from "openclaw/plugin-sdk/text-runtime";
import { inspectDiscordAccount } from "./account-inspect.js";
import { resolveDefaultDiscordAccountId } from "./accounts.js";
import { normalizeCompatibilityConfig as normalizeDiscordCompatibilityConfig } from "./doctor-contract.js";
import { DISCORD_LEGACY_CONFIG_RULES } from "./doctor-shared.js";
import { isDiscordMutableAllowEntry } from "./security-doctor.js";
@@ -235,6 +237,26 @@ export function maybeRepairDiscordNumericIds(
};
}
export function collectDiscordMissingEnvTokenWarnings(params: {
cfg: OpenClawConfig;
env?: NodeJS.ProcessEnv;
}): string[] {
if (resolveDefaultDiscordAccountId(params.cfg) !== "default") {
return [];
}
const account = inspectDiscordAccount({
cfg: params.cfg,
accountId: "default",
envToken: params.env?.DISCORD_BOT_TOKEN ?? "",
});
if (!account.enabled || account.tokenStatus !== "missing" || account.tokenSource !== "none") {
return [];
}
return [
"- channels.discord: default account has no available bot token, and DISCORD_BOT_TOKEN is absent in this doctor environment. After migration, verify DISCORD_BOT_TOKEN is present in the state-dir .env or configure channels.discord.token / channels.discord.accounts.default.token as a SecretRef.",
];
}
function collectDiscordMutableAllowlistWarnings(cfg: OpenClawConfig): string[] {
const hits: Array<{ path: string; entry: string }> = [];
const addHits = (pathLabel: string, list: unknown) => {
@@ -306,11 +328,13 @@ export const discordDoctor: ChannelDoctorAdapter = {
warnOnEmptyGroupSenderAllowlist: false,
legacyConfigRules: DISCORD_LEGACY_CONFIG_RULES,
normalizeCompatibilityConfig: normalizeDiscordCompatibilityConfig,
collectPreviewWarnings: ({ cfg, doctorFixCommand }) =>
collectDiscordNumericIdWarnings({
collectPreviewWarnings: ({ cfg, doctorFixCommand, env }) => [
...collectDiscordMissingEnvTokenWarnings({ cfg, env }),
...collectDiscordNumericIdWarnings({
hits: scanDiscordNumericIdEntries(cfg),
doctorFixCommand,
}),
],
collectMutableAllowlistWarnings: ({ cfg }) => collectDiscordMutableAllowlistWarnings(cfg),
repairConfig: ({ cfg, doctorFixCommand }) => maybeRepairDiscordNumericIds(cfg, doctorFixCommand),
};