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

@@ -1,10 +1,12 @@
// "通用设置" tab — fields that apply to the whole OneBotInstance rather
// than any specific adapter. Currently just the music-sign service URL,
// but the layout has room for additional cross-cutting toggles.
// than any specific adapter: the music-sign service URL and the built-in
// `#sl` status command. Edits here mark the config dirty and are persisted
// via the page's explicit top-right 保存 (a continuously-edited surface).
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import type { OneBotConfig } from '@/types';
import { ToggleSwitch } from '@/components/ui/toggle-switch';
import type { OneBotConfig, StatusCommandConfig } from '@/types';
interface GeneralSettingsTabProps {
config: OneBotConfig;
@@ -12,6 +14,10 @@ interface GeneralSettingsTabProps {
}
export function GeneralSettingsTab({ config, onChange }: GeneralSettingsTabProps) {
const sc = config.statusCommand;
const setStatusCommand = (patch: Partial<StatusCommandConfig>) =>
onChange({ ...config, statusCommand: { ...sc, ...patch } });
return (
<div className="flex flex-col gap-4">
<div className="flex flex-col gap-1.5 rounded-lg border bg-card/40 p-4">
@@ -26,6 +32,57 @@ export function GeneralSettingsTab({ config, onChange }: GeneralSettingsTabProps
用于音乐分享卡片签名。未配置时音乐相关消息段会回落为普通文本。
</p>
</div>
<div className="flex flex-col gap-4 rounded-lg border bg-card/40 p-4">
<div className="flex items-start justify-between gap-3">
<div className="min-w-0">
<Label>
内置状态命令 <code className="font-mono text-xs">#sl</code>
</Label>
<p className="mt-1 text-[11px] leading-relaxed text-muted-foreground">
收到纯文本 <code className="font-mono">#sl</code> 时回复 SnowLuma 版本 / 平台 / 运行时长。任何人可触发,关闭后完全不响应。
</p>
</div>
<ToggleSwitch
value={sc.enabled}
onChange={(v) => setStatusCommand({ enabled: v })}
ariaLabel="启用 #sl 状态命令"
/>
</div>
<div className="flex items-start justify-between gap-3 border-t pt-3">
<div className="min-w-0">
<Label className={sc.enabled ? undefined : 'text-muted-foreground'}>不转发给下游(swallow)</Label>
<p className="mt-1 text-[11px] leading-relaxed text-muted-foreground">
开启后,命中的 <code className="font-mono">#sl</code> 不再投递给已连接的 Bot(仍会回复并本地记录)。默认关闭即透传。
</p>
</div>
<ToggleSwitch
value={sc.swallow}
onChange={(v) => setStatusCommand({ swallow: v })}
ariaLabel="吞掉 #sl 不转发给下游"
disabled={!sc.enabled}
/>
</div>
<div className="flex flex-col gap-1.5 border-t pt-3">
<Label className={sc.enabled ? undefined : 'text-muted-foreground'}>回复冷却(秒)</Label>
<Input
type="number"
min={0}
className="w-32 tabular-nums"
value={sc.cooldownSeconds}
disabled={!sc.enabled}
onChange={(e) => {
const n = Math.trunc(Number(e.target.value));
setStatusCommand({ cooldownSeconds: Number.isFinite(n) && n >= 0 ? n : 0 });
}}
/>
<p className="text-[11px] leading-relaxed text-muted-foreground">
同一会话在该秒数内重复 <code className="font-mono">#sl</code> 不再回复,防刷屏。<code className="font-mono">0</code> 表示不限制。
</p>
</div>
</div>
</div>
);
}
@@ -7,6 +7,7 @@ import { motion } from 'motion/react';
import { Pencil, Trash2 } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { Badge } from '@/components/ui/badge';
import { ToggleSwitch } from '@/components/ui/toggle-switch';
import { cn } from '@/lib/utils';
import type { AdapterCommon } from './defaults';
import type { AdapterStatus } from '@/types';
@@ -126,30 +127,3 @@ export function NodeSummaryCard<T extends AdapterCommon>({
);
}
interface ToggleSwitchProps {
value: boolean;
onChange: (v: boolean) => void;
ariaLabel: string;
}
function ToggleSwitch({ value, onChange, ariaLabel }: ToggleSwitchProps) {
return (
<button
type="button"
role="switch"
aria-checked={value ? 'true' : 'false'}
aria-label={ariaLabel}
onClick={() => onChange(!value)}
className={cn(
'relative inline-flex h-6 w-11 shrink-0 cursor-pointer items-center rounded-full border transition-colors',
value ? 'border-primary bg-primary' : 'border-input bg-muted',
)}
>
<motion.span
className="inline-block size-4 rounded-full bg-background shadow-sm"
animate={{ x: value ? 22 : 4 }}
transition={{ type: 'spring', stiffness: 500, damping: 30 }}
/>
</button>
);
}
@@ -0,0 +1,35 @@
import { motion } from 'motion/react';
import { cn } from '@/lib/utils';
interface ToggleSwitchProps {
value: boolean;
onChange: (v: boolean) => void;
ariaLabel: string;
disabled?: boolean;
}
/** Small animated on/off switch (`role="switch"`). Shared across the config
* surfaces — node enable toggle, built-in command toggles, etc. */
export function ToggleSwitch({ value, onChange, ariaLabel, disabled }: ToggleSwitchProps) {
return (
<button
type="button"
role="switch"
aria-checked={value ? 'true' : 'false'}
aria-label={ariaLabel}
disabled={disabled}
onClick={() => onChange(!value)}
className={cn(
'relative inline-flex h-6 w-11 shrink-0 items-center rounded-full border transition-colors',
disabled ? 'cursor-not-allowed opacity-50' : 'cursor-pointer',
value ? 'border-primary bg-primary' : 'border-input bg-muted',
)}
>
<motion.span
className="inline-block size-4 rounded-full bg-background shadow-sm"
animate={{ x: value ? 22 : 4 }}
transition={{ type: 'spring', stiffness: 500, damping: 30 }}
/>
</button>
);
}
+18 -1
View File
@@ -1,4 +1,20 @@
import type { MessageFormat, OneBotConfig } from '@/types';
import type { MessageFormat, OneBotConfig, StatusCommandConfig } from '@/types';
const DEFAULT_STATUS_COMMAND: StatusCommandConfig = { enabled: true, swallow: false, cooldownSeconds: 5 };
/** Fill the `statusCommand` block with defaults when the backend omits or
* partially supplies it (older configs predate the feature). */
function normalizeStatusCommand(raw: unknown): StatusCommandConfig {
const src = (raw ?? {}) as Record<string, unknown>;
return {
enabled: typeof src.enabled === 'boolean' ? src.enabled : DEFAULT_STATUS_COMMAND.enabled,
swallow: typeof src.swallow === 'boolean' ? src.swallow : DEFAULT_STATUS_COMMAND.swallow,
cooldownSeconds:
typeof src.cooldownSeconds === 'number' && Number.isFinite(src.cooldownSeconds) && src.cooldownSeconds >= 0
? Math.trunc(src.cooldownSeconds)
: DEFAULT_STATUS_COMMAND.cooldownSeconds,
};
}
/**
* Anti-corruption layer for the per-UIN config payload. Older backends emit
@@ -30,5 +46,6 @@ export function normalizeOneBotConfig(raw: unknown): OneBotConfig {
wsClients: list(nets.wsClients) as unknown as OneBotConfig['networks']['wsClients'],
},
musicSignUrl: typeof cfg.musicSignUrl === 'string' ? cfg.musicSignUrl : undefined,
statusCommand: normalizeStatusCommand(cfg.statusCommand),
};
}
+8
View File
@@ -77,9 +77,17 @@ export interface OneBotNetworks {
wsClients: WsClientNetwork[];
}
/** Built-in `#sl` status command settings (trigger word is hardcoded). */
export interface StatusCommandConfig {
enabled: boolean;
swallow: boolean;
cooldownSeconds: number;
}
export interface OneBotConfig {
networks: OneBotNetworks;
musicSignUrl?: string;
statusCommand: StatusCommandConfig;
}
export type NetworkKind = keyof OneBotNetworks;