export interface CommandContext { status(): string; pluginCommands(): readonly string[]; active(level: string): string; room(roomId: string): Promise; follow(value: string): string; } export interface CommandDefinition { name: string; usage: string; description: string; execute(args: readonly string[], context: CommandContext): string | Promise; } /** Adapter 当前公开的全部本地命令;help 文本由这里生成,避免文档漂移。 */ export const COMMANDS: readonly CommandDefinition[] = [ { name: "help", usage: "/help", description: "显示所有本地白名单命令", execute: (_args, context) => renderHelp(context.pluginCommands()), }, { name: "ping", usage: "/ping", description: "检查 Adapter 是否仍能响应", execute: () => "pong", }, { name: "status", usage: "/status", description: "显示当前连接状态", execute: (_args, context) => context.status(), }, { name: "active", usage: "/active {off|low|medium|high}", description: "切换主动响应等级", execute: (args, context) => context.active(args[0] ?? ""), }, { name: "room", usage: "/room {roomId}", description: "切换机器人所在房间", execute: (args, context) => context.room(args[0] ?? ""), }, { name: "follow", usage: "/follow {true|false}", description: "切换是否跟随管理员换房", execute: (args, context) => context.follow(args[0] ?? ""), }, ]; export function renderHelp(pluginCommands: readonly string[] = []): string { return [ "ToBe · IIROSE 本地命令", ...COMMANDS.map((command) => `${command.usage} — ${command.description}`), ...pluginCommands, ].join("\n"); } export async function runCommand( content: string, prefix: string, whitelist: readonly string[], context: CommandContext, authorized = true, ): Promise<{ handled: boolean; response?: string; command?: string }> { if (!content.startsWith(prefix)) return { handled: false }; const [name = "", ...args] = content.slice(prefix.length).trim().split(/\s+/); const command = COMMANDS.find((item) => item.name === name.toLowerCase()); if (!command || !whitelist.includes(command.name)) return { handled: false }; if (!authorized) return { handled: true, command: command.name }; return { handled: true, command: command.name, response: await command.execute(args, context) }; }