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:
11 files changed
+434
-32
No files matched your search
@@ -0,0 +1,103 @@
|
||||
import { describe, it, expect } from 'vitest';
|
||||
import {
|
||||
STATUS_COMMAND_TRIGGER,
|
||||
buildStatusText,
|
||||
formatUptime,
|
||||
matchesStatusCommand,
|
||||
statusCooldownElapsed,
|
||||
} from '../src/modules/status-command';
|
||||
import type { JsonValue } from '../src/types';
|
||||
|
||||
function textSeg(text: string): JsonValue {
|
||||
return [{ type: 'text', data: { text } }] as unknown as JsonValue;
|
||||
}
|
||||
|
||||
describe('matchesStatusCommand', () => {
|
||||
it('matches a single text segment equal to the trigger', () => {
|
||||
expect(matchesStatusCommand(textSeg('#sl'))).toBe(true);
|
||||
});
|
||||
|
||||
it('is case-insensitive and trims surrounding whitespace', () => {
|
||||
expect(matchesStatusCommand(textSeg('#SL'))).toBe(true);
|
||||
expect(matchesStatusCommand(textSeg(' #Sl '))).toBe(true);
|
||||
});
|
||||
|
||||
it('matches a bare string (string-format adapters)', () => {
|
||||
expect(matchesStatusCommand('#sl' as unknown as JsonValue)).toBe(true);
|
||||
expect(matchesStatusCommand(' #SL ' as unknown as JsonValue)).toBe(true);
|
||||
});
|
||||
|
||||
it('does NOT match as a prefix — only exact', () => {
|
||||
expect(matchesStatusCommand(textSeg('#slogan'))).toBe(false);
|
||||
expect(matchesStatusCommand(textSeg('#sl help'))).toBe(false);
|
||||
expect(matchesStatusCommand(textSeg('你好 #sl'))).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects mixed-segment messages', () => {
|
||||
const mixed = [
|
||||
{ type: 'text', data: { text: '#sl' } },
|
||||
{ type: 'image', data: { file: 'x.png' } },
|
||||
] as unknown as JsonValue;
|
||||
expect(matchesStatusCommand(mixed)).toBe(false);
|
||||
});
|
||||
|
||||
it('rejects non-text leading segments and empty/garbage input', () => {
|
||||
expect(matchesStatusCommand([{ type: 'at', data: { qq: '1' } }] as unknown as JsonValue)).toBe(false);
|
||||
expect(matchesStatusCommand([] as unknown as JsonValue)).toBe(false);
|
||||
expect(matchesStatusCommand(undefined)).toBe(false);
|
||||
expect(matchesStatusCommand(123 as unknown as JsonValue)).toBe(false);
|
||||
});
|
||||
|
||||
it('exposes the hardcoded trigger', () => {
|
||||
expect(STATUS_COMMAND_TRIGGER).toBe('#sl');
|
||||
});
|
||||
});
|
||||
|
||||
describe('statusCooldownElapsed', () => {
|
||||
it('allows the first reply (no prior timestamp)', () => {
|
||||
expect(statusCooldownElapsed(undefined, 1_000, 5)).toBe(true);
|
||||
});
|
||||
|
||||
it('ignores the cooldown when set to 0 or negative', () => {
|
||||
expect(statusCooldownElapsed(1_000, 1_500, 0)).toBe(true);
|
||||
expect(statusCooldownElapsed(1_000, 1_000, -1)).toBe(true);
|
||||
});
|
||||
|
||||
it('blocks within the window and allows once elapsed', () => {
|
||||
const last = 10_000;
|
||||
expect(statusCooldownElapsed(last, last + 4_999, 5)).toBe(false);
|
||||
expect(statusCooldownElapsed(last, last + 5_000, 5)).toBe(true);
|
||||
expect(statusCooldownElapsed(last, last + 9_999, 5)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('formatUptime', () => {
|
||||
it('drops leading zero units', () => {
|
||||
expect(formatUptime(0)).toBe('0秒');
|
||||
expect(formatUptime(45_000)).toBe('45秒');
|
||||
expect(formatUptime(90_000)).toBe('1分钟 30秒');
|
||||
expect(formatUptime(3_600_000)).toBe('1小时 0分钟');
|
||||
expect(formatUptime((26 * 3600 + 12 * 60) * 1000)).toBe('1天 2小时 12分钟');
|
||||
});
|
||||
|
||||
it('never goes negative', () => {
|
||||
expect(formatUptime(-5_000)).toBe('0秒');
|
||||
});
|
||||
});
|
||||
|
||||
describe('buildStatusText', () => {
|
||||
it('renders version / platform / uptime, no leaked uin or topology', () => {
|
||||
const text = buildStatusText({
|
||||
version: '1.9.3-node',
|
||||
platform: 'linux',
|
||||
arch: 'x64',
|
||||
uptimeMs: 90_000,
|
||||
});
|
||||
expect(text).toContain('SnowLuma');
|
||||
expect(text).toContain('版本: 1.9.3-node');
|
||||
expect(text).toContain('平台: linux-x64');
|
||||
expect(text).toContain('运行时长: 1分钟 30秒');
|
||||
// public-safe: must not echo a uin or connection count
|
||||
expect(text).not.toMatch(/uin|连接|adapter/i);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user