Skip to content

Commit

Permalink
细节优化
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeRainStarSky committed Jun 2, 2024
1 parent ed4d36b commit 4c2f82b
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
70 changes: 39 additions & 31 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ export default class Yunzai extends EventEmitter {
this.__proto__.toString.apply(this, args) :
this.toJSON().toString(raw, ...args)
},
includes(value) {
return this.some(i => i == value)
},
})
adapter = []

Expand All @@ -61,7 +64,7 @@ export default class Yunzai extends EventEmitter {
}
req.rid = `${req.ip}:${req.socket.remotePort}`
req.sid = `${req.protocol}://${req.hostname}:${req.socket.localPort}${req.originalUrl}`
this.makeLog(quiet ? "debug" : "mark", ["HTTP", req.method, "请求", req.headers, req.query, req.body], `${req.sid} <= ${req.rid}`)
this.makeLog(quiet?"debug":"mark", ["HTTP", req.method, "请求", req.headers, req.query, req.body], `${req.sid} <= ${req.rid}`)
})
.use("/exit", req => {
if (["::1", "::ffff:127.0.0.1"].includes(req.ip) || req.hostname === "localhost")
Expand Down Expand Up @@ -412,14 +415,12 @@ export default class Yunzai extends EventEmitter {
if (Buffer.isBuffer(file.buffer)) {
file.type = await fileTypeFromBuffer(file.buffer)
file.md5 = md5(file.buffer)
if (!file.name)
file.name = `${Date.now()}.${file.md5.slice(0,8)}.${file.type.ext}`
file.name ??= `${Date.now().toString(36)}.${file.md5.slice(0,8)}.${file.type.ext}`
}
} catch (err) {
this.makeLog("error", ["文件类型检测错误", file, err])
}
if (!file.name)
file.name = `${Date.now()}-${path.basename(file.url)}`
file.name ??= `${Date.now().toString(36)}-${path.basename(file.url)}`
return file
}

Expand All @@ -442,8 +443,7 @@ export default class Yunzai extends EventEmitter {

fileSend(req) {
const url = req.url.replace(/^\//, "")
let file = this.fs[url]
if (!file) file = this.fs[404]
let file = this.fs[url] || this.fs[404]

if (typeof file.times === "number") {
if (file.times > 0) file.times--
Expand All @@ -459,15 +459,15 @@ export default class Yunzai extends EventEmitter {

async exec(cmd, opts = {}) { return new Promise(resolve => {
const name = logger.cyan(this.String(cmd))
this.makeLog(opts.quiet ? "debug" : "mark", name, "执行命令")
this.makeLog(opts.quiet?"debug":"mark", name, "执行命令")
opts.encoding ??= "buffer"
const callback = (error, stdout, stderr) => {
const raw = { stdout, stderr }
stdout = String(stdout).trim()
stderr = String(stderr).trim()
resolve({ error, stdout, stderr, raw })
this.makeLog(opts.quiet ? "debug" : "mark", `${name} ${logger.green(`[完成${this.getTimeDiff(start_time)}]`)} ${stdout?`\n${stdout}`:""}${stderr?logger.red(`\n${stderr}`):""}`, "执行命令")
if (error) this.makeLog(opts.quiet ? "debug" : "error", error, "执行命令")
this.makeLog(opts.quiet?"debug":"mark", `${name} ${logger.green(`[完成${this.getTimeDiff(start_time)}]`)} ${stdout?`\n${stdout}`:""}${stderr?logger.red(`\n${stderr}`):""}`, "执行命令")
if (error) this.makeLog(opts.quiet?"debug":"error", error, "执行命令")
}
const start_time = Date.now()
if (Array.isArray(cmd))
Expand Down Expand Up @@ -516,14 +516,10 @@ export default class Yunzai extends EventEmitter {

for (const i of [data.friend, data.group, data.member]) {
if (typeof i !== "object") continue
if (!i.sendFile)
i.sendFile = (file, name) => i.sendMsg(segment.file(file, name))
if (!i.makeForwardMsg)
i.makeForwardMsg = this.makeForwardMsg
if (!i.sendForwardMsg)
i.sendForwardMsg = msg => this.sendForwardMsg(msg => i.sendMsg(msg), msg)
if (!i.getInfo)
i.getInfo = () => i.info || i
i.sendFile ??= (file, name) => i.sendMsg(segment.file(file, name))
i.makeForwardMsg ??= this.makeForwardMsg
i.sendForwardMsg ??= msg => this.sendForwardMsg(msg => i.sendMsg(msg), msg)
i.getInfo ??= () => i.info || i
}
}

Expand Down Expand Up @@ -625,16 +621,22 @@ export default class Yunzai extends EventEmitter {
if (!bot_id)
return this.pickFriend(user_id).sendMsg(msg)

if (this.bots[bot_id])
if (this.uin.includes(bot_id) && this.bots[bot_id])
return this.bots[bot_id].pickFriend(user_id).sendMsg(msg)

return new Promise(resolve =>
this.once(`connect.${bot_id}`, data =>
return new Promise((resolve, reject) => {
const listener = data => {
resolve(data.bot.pickFriend(user_id).sendMsg(msg))
)
)
clearTimeout(timeout)
}
const timeout = setTimeout(() => {
reject(Object.assign(Error("等待 Bot 上线超时"), { bot_id, user_id, msg }))
this.off(`connect.${bot_id}`, listener)
}, 300000)
this.once(`connect.${bot_id}`, listener)
})
} catch (err) {
this.makeLog("error", [`发送好友消息错误:[${user_id}]`, err], bot_id)
this.makeLog("error", ["发送好友消息错误", msg, err], `${bot_id} => ${user_id}`)
}
return false
}
Expand All @@ -644,24 +646,30 @@ export default class Yunzai extends EventEmitter {
if (!bot_id)
return this.pickGroup(group_id).sendMsg(msg)

if (this.bots[bot_id])
if (this.uin.includes(bot_id) && this.bots[bot_id])
return this.bots[bot_id].pickGroup(group_id).sendMsg(msg)

return new Promise(resolve =>
this.once(`connect.${bot_id}`, data =>
return new Promise((resolve, reject) => {
const listener = data => {
resolve(data.bot.pickGroup(group_id).sendMsg(msg))
)
)
clearTimeout(timeout)
}
const timeout = setTimeout(() => {
reject(Object.assign(Error("等待 Bot 上线超时"), { bot_id, group_id, msg }))
this.off(`connect.${bot_id}`, listener)
}, 300000)
this.once(`connect.${bot_id}`, listener)
})
} catch (err) {
this.makeLog("error", [`发送群消息错误:[${group_id}]`, err], bot_id)
this.makeLog("error", ["发送群消息错误", msg, err], `${bot_id} => ${group_id}`)
}
return false
}

getTextMsg(fnc = () => true) {
if (typeof fnc !== "function") {
const { self_id, user_id } = fnc
fnc = data => data.self_id === self_id && data.user_id === user_id
fnc = data => data.self_id == self_id && data.user_id == user_id
}

return new Promise(resolve => {
Expand Down
2 changes: 1 addition & 1 deletion lib/plugins/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ class PluginsLoader {
}

a: for (const plugin of priority) {
/** 正则匹配 */
if (plugin.rule) for (const v of plugin.rule) {
/** 判断事件 */
if (v.event && !this.filtEvent(e, v)) continue

/** 正则匹配 */
if (!new RegExp(v.reg).test(e.msg)) continue
e.logFnc = `${logger.blue(`[${plugin.name}(${v.fnc})]`)}`

Expand Down
10 changes: 5 additions & 5 deletions lib/plugins/stdin.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Bot.adapter.push(new class stdinAdapter {
if (i.file) {
file = await Bot.fileType(i)
if (Buffer.isBuffer(file.buffer)) {
file.path = `${this.path}${file.name || Date.now()}`
file.path = `${this.path}${file.name}`
await fs.writeFile(file.path, file.buffer)
}
}
Expand Down Expand Up @@ -58,7 +58,7 @@ Bot.adapter.push(new class stdinAdapter {
Bot.makeLog("info", i, this.id)
}
}
return { message_id: Date.now() }
return { message_id: Date.now().toString(36) }
}

recallMsg(message_id) {
Expand All @@ -72,7 +72,7 @@ Bot.adapter.push(new class stdinAdapter {
return false
}

const files = `${this.path}${Date.now()}-${name}`
const files = `${this.path}${Date.now().toString(36)}-${name}`
Bot.makeLog("info", `发送文件:${file}\n文件已保存到:${logger.cyan(files)}`, this.id)
return fs.writeFile(files, buffer)
}
Expand All @@ -87,7 +87,7 @@ Bot.adapter.push(new class stdinAdapter {
}

message(msg) {
fs.appendFile(`${this.path}history`, `${Date.now()/1000}:${msg}\n`, "utf8")
fs.appendFile(`${this.path}history`, `${Date.now().toString(36)}:${msg}\n`, "utf8")
const data = {
bot: Bot[this.id],
self_id: this.id,
Expand Down Expand Up @@ -136,7 +136,7 @@ Bot.adapter.push(new class stdinAdapter {
Bot[this.id].gml.set(this.id, Bot[this.id].fl)

try {
Bot[this.id].sdk.history = (await fs.readFile(`${this.path}history`, "utf8")).split("\n").slice(-Bot[this.id].sdk.historySize-1, -1).map(i => i.replace(/^[\d.]+:/, "")).reverse()
Bot[this.id].sdk.history = (await fs.readFile(`${this.path}history`, "utf8")).split("\n").slice(-Bot[this.id].sdk.historySize-1, -1).map(i => i.replace(/^[0-9a-z]+?:/, "")).reverse()
} catch (err) {
Bot.makeLog("trace", err, this.id)
}
Expand Down
4 changes: 2 additions & 2 deletions plugins/adapter/GSUIDCore.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Bot.adapter.push(new class GSUIDCoreAdapter {
target_id: data.user_id,
content,
})
return { message_id: Date.now() }
return { message_id: Date.now().toString(36) }
}

async sendGroupMsg(data, msg) {
Expand All @@ -135,7 +135,7 @@ Bot.adapter.push(new class GSUIDCoreAdapter {
target_id: target[1],
content,
})
return { message_id: Date.now() }
return { message_id: Date.now().toString(36) }
}

pickFriend(id, user_id) {
Expand Down

0 comments on commit 4c2f82b

Please sign in to comment.