diff --git a/lib/bot.js b/lib/bot.js index 79968754..4bb99cb0 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -253,7 +253,15 @@ export default class Yunzai extends EventEmitter { if (Buffer.isBuffer(data)) return String(data) } - return JSON.stringify(data) + + try { + return JSON.stringify(data) + } catch (err) { + if (typeof data.toString == "function") + return String(data) + else + return "[object null]" + } } Loging(data) { @@ -353,15 +361,23 @@ export default class Yunzai extends EventEmitter { async exec(cmd, opts = {}) { return new Promise(resolve => { - this.makeLog("mark", `[执行命令] ${logger.blue(cmd)}`) + if (!opts.quiet) + this.makeLog("mark", `[执行命令] ${logger.blue(cmd)}`) exec(cmd, opts, (error, stdout, stderr) => { resolve({ error, stdout, stderr }) + if (opts.quiet) return this.makeLog("mark", `[执行命令完成] ${logger.blue(cmd)}${stdout?`\n${String(stdout).trim()}`:""}${stderr?logger.red(`\n${String(stderr).trim()}`):""}`) if (error) this.makeLog("error", `[执行命令错误] ${logger.blue(cmd)}\n${logger.red(this.Loging(error).trim())}`) }) }) } + async cmdPath(cmd, opts = {}) { + const ret = await this.exec(`${process.platform == "win32" ? "where" : "command -v"} "${cmd}"`, { quiet: true, ...opts }) + if (ret.error) return false + return ret.stdout.trim() + } + makeLog(level, msg, id) { const log = [] if (id) log.push(logger.blue(`[${id}]`)) diff --git a/lib/plugins/stdin.js b/lib/plugins/stdin.js index 9c9e1f48..2c7955d8 100644 --- a/lib/plugins/stdin.js +++ b/lib/plugins/stdin.js @@ -1,12 +1,14 @@ import readline from "node:readline/promises" import fs from "node:fs/promises" import path from "node:path" +import { spawn } from "node:child_process" Bot.adapter.push(new class stdinAdapter { constructor() { this.id = "stdin" this.name = "标准输入" this.path = "data/stdin/" + this.catimg = () => {} } async sendMsg(msg) { @@ -32,6 +34,7 @@ Bot.adapter.push(new class stdinAdapter { Bot.makeLog("info", `发送文本:${i.text}`, this.id) break case "image": + await this.catimg(file.path) Bot.makeLog("info", `发送图片:${file.url}\n文件已保存到:${logger.cyan(file.path)}`, this.id) break case "record": @@ -100,7 +103,14 @@ Bot.adapter.push(new class stdinAdapter { Bot.em(`${data.post_type}.${data.message_type}`, data) } + async loadCatimg() { + this.catimgPath = await Bot.cmdPath("catimg") + if (this.catimgPath) + this.catimg = file => new Promise(resolve => spawn(this.catimgPath, [file], { stdio:"inherit" }).on("close", resolve)) + } + async load() { + this.loadCatimg() await Bot.mkdir(this.path) Bot[this.id] = { adapter: this,