feat(onebot): built-in #sl status command (toggleable, non-plugin)

A hardcoded `#sl` status command — deliberately NOT a plugin system.
Matched at the instance dispatch choke point: a pure-text message equal
to `#sl` (exact, trimmed, case-insensitive — no startsWith) replies with
SnowLuma version / platform / uptime in the same conversation.

- Anyone can trigger; the per-UIN `statusCommand.enabled` toggle is the
  only gate (default on). Per-conversation cooldown (default 5s,
  configurable) prevents flood/风控 from repeated triggers.
- `swallow` (default false / pass-through): when set, a matched `#sl` is
  not forwarded to downstream adapters — still cached, logged, replied.
- Config on OneBotConfig.statusCommand (per-UIN): defaulted + clamped in
  config.ts, mirrored/normalized in the WebUI; edits hot-reload via the
  shared instance context.
- WebUI: a "内置命令" section in the 通用设置 tab (enable / swallow /
  cooldown). Node enable toggle extracted into a shared ui/ToggleSwitch.

Pure matcher / cooldown / uptime / text helpers live in
modules/status-command with unit tests; config defaults + clamp covered.
This commit is contained in:
DrSmoothl committed 2026-06-01 14:58:13 +08:00
1 parent 0d63bf7415
commit 003e8c2deb
11 files changed
+434 -32

No files matched your search

+35 -1
View File
@@ -9,6 +9,7 @@ import type {
MessageFormat,
OneBotConfig,
OneBotNetworks,
StatusCommandConfig,
WsClientNetwork,
WsRole,
WsServerNetwork,
@@ -20,6 +21,14 @@ const CONFIG_DIR = 'config';
const DEFAULT_CONFIG_PATH = path.join(CONFIG_DIR, 'onebot.json');
const DEFAULT_ACCESS_TOKEN_BYTES = 32;
const DEFAULT_STATUS_COMMAND: StatusCommandConfig = { enabled: true, swallow: false, cooldownSeconds: 5 };
/** Upper bound on the `#sl` reply cooldown — a year is effectively "off but sane". */
const STATUS_COMMAND_COOLDOWN_MAX = 31_536_000;
function makeDefaultStatusCommand(): StatusCommandConfig {
return { ...DEFAULT_STATUS_COMMAND };
}
export function makeDefaultOneBotConfig(): OneBotConfig {
return {
networks: {
@@ -46,6 +55,7 @@ export function makeDefaultOneBotConfig(): OneBotConfig {
wsClients: [],
},
musicSignUrl: '',
statusCommand: makeDefaultStatusCommand(),
};
}
@@ -98,6 +108,11 @@ function toJsonObject(config: OneBotConfig): JsonObject {
wsClients: nets.wsClients.map(wsClientToJson),
},
musicSignUrl: config.musicSignUrl ?? '',
statusCommand: {
enabled: config.statusCommand.enabled,
swallow: config.statusCommand.swallow,
cooldownSeconds: config.statusCommand.cooldownSeconds,
},
};
}
@@ -181,7 +196,26 @@ function fromJson(sources: JsonObject[], freshInstall: boolean): OneBotConfig {
}
const networks: OneBotNetworks = { httpServers, httpClients, wsServers, wsClients };
return { networks, musicSignUrl };
return { networks, musicSignUrl, statusCommand: parseStatusCommand(sources) };
}
/** Last-write-wins merge of `statusCommand` across config sources, with
* defaults filled and the cooldown clamped to a sane non-negative range. */
function parseStatusCommand(sources: JsonObject[]): StatusCommandConfig {
const out = makeDefaultStatusCommand();
for (const src of sources) {
const raw = src.statusCommand;
if (!isObject(raw)) continue;
if (typeof raw.enabled === 'boolean') out.enabled = raw.enabled;
if (typeof raw.swallow === 'boolean') out.swallow = raw.swallow;
if (raw.cooldownSeconds !== undefined) {
out.cooldownSeconds = Math.min(
STATUS_COMMAND_COOLDOWN_MAX,
asNumber(raw.cooldownSeconds, DEFAULT_STATUS_COMMAND.cooldownSeconds),
);
}
}
return out;
}
function collectByName<T extends { name: string }>(