diff --git a/apps/bym.js b/apps/bym.js index 447ef7b..4e02064 100644 --- a/apps/bym.js +++ b/apps/bym.js @@ -20,6 +20,7 @@ import { SerpTool } from '../utils/tools/SerpTool.js' import { initializeImageTool } from '../utils/tools/ImageTool.js' import { DailyNewsTool } from '../utils/tools/DailyNewsTool.js' import { SendMessageToSpecificGroupOrUserTool } from '../utils/tools/SendMessageToSpecificGroupOrUserTool.js' +import { SystemCommandTool } from '../utils/tools/SystemCommandTool.js' const DefaultConfig = { returnQQ: [], @@ -322,6 +323,8 @@ export class bym extends plugin { tools.push(new SetTitleTool()) } + // 你没见过bot能控制系统吗( + tools.push(new SystemCommandTool()) const imageTool = await initializeImageTool(e, previousRole, bymGo) if (Config.AutoToDownImg) { tools.push(imageTool) diff --git a/utils/tools/SystemCommandTool.js b/utils/tools/SystemCommandTool.js new file mode 100644 index 0000000..0ce5405 --- /dev/null +++ b/utils/tools/SystemCommandTool.js @@ -0,0 +1,86 @@ +// SystemCommandTool.js +import { AbstractTool } from './AbstractTool.js' +import { exec } from 'child_process' +import { promisify } from 'util' +import os from 'os' +import cfg from '../../../../lib/config/config.js' + +const execAsync = promisify(exec) + +export class SystemCommandTool extends AbstractTool { + name = 'systemCommand' + + #safeCommands = { + windows: [ + 'dir', 'echo', 'type', 'systeminfo', + 'tasklist', 'ver', 'hostname', 'time', + 'date', 'ping', 'ipconfig' + ], + linux: [ + 'ls', 'echo', 'cat', 'uname', 'ps', + 'pwd', 'date', 'uptime', 'free', + 'df', 'ping', 'ifconfig', 'ip','lspci' + ] + } + + parameters = { + properties: { + command: { + type: 'string', + description: 'The command to execute' + } + }, + required: ['command'] + } + + description = `Execute system commands. Commands: ${ + os.platform() === 'win32' + ? this.#safeCommands.windows.join(', ') + : this.#safeCommands.linux.join(', ') + }` + + #isWindows = os.platform() === 'win32' + + func = async (opts, e) => { + if (!cfg.masterQQ?.includes(e.user_id)) return `用户权限不足` + try { + const { command } = opts + if (!command) { + return '命令不能为空' + } + + // 安全性检查 + const mainCommand = command.split(' ')[0].toLowerCase() + const platform = this.#isWindows ? 'windows' : 'linux' + if (!this.#safeCommands[platform].includes(mainCommand)) { + return `命令不在允许列表中。\n可用命令:\n${this.#safeCommands[platform].join(', ')}` + } + + // 执行命令 + logger.info(`[SystemCommandTool] Executing: ${command}`) + const { stdout, stderr } = await execAsync(command, { + timeout: 10000, + maxBuffer: 1024 * 1024 + }) + + // 格式化输出 + let output = '' + if (stdout) output += stdout + if (stderr) output += `\nErrors:\n${stderr}` + + return output.trim() || '命令执行成功,无输出' + + } catch (error) { + logger.error('[SystemCommandTool] Error:', error) + return `命令执行失败: ${error.message}` + } + } + + async processText(text, e) { + const cmdMatch = text.match(/^[!/](cmd|系统|命令)\s+(.+)$/i) + if (cmdMatch) { + return await this.func({ command: cmdMatch[2].trim() }, e) + } + return null + } +} \ No newline at end of file