From f69d350c94aa2816512126658604e40d13fea377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=F0=9F=8C=8C?= Date: Sat, 25 May 2024 14:38:17 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BB=86=E8=8A=82=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/bot.js | 103 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 42 deletions(-) diff --git a/lib/bot.js b/lib/bot.js index d6169fbd..5361ce5d 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -19,32 +19,39 @@ import { ulid } from "ulid" import { Level } from "level" export default class Yunzai extends EventEmitter { - constructor() { - super() - this.stat = { start_time: Date.now()/1000 } - this.bot = this - this.bots = {} - this.uin = Object.assign([], { - toJSON() { - if (!this.now) { - this.now = this[Math.floor(Math.random()*this.length)] - setTimeout(() => delete this.now, 60000) + stat = { start_time: Date.now()/1000 } + bot = this + bots = {} + uin = Object.assign([], { + toJSON() { + if (!this.now) { + switch (this.length) { + case 0: + return "" + case 1: + case 2: + return this[this.length-1] } - return this.now ?? "" - }, - toString(raw, ...args) { - return raw === true ? - this.__proto__.toString.apply(this, args) : - this.toJSON().toString(raw, ...args) - }, - }) - this.adapter = [] - - this.express = express() - this.express.quiet = [] - for (const i of ["urlencoded", "json", "raw", "text"]) - this.express.use(express[i]({ extended: false })) - this.express.use(req => { + const array = this.slice(1) + this.now = array[Math.floor(Math.random()*array.length)] + setTimeout(() => delete this.now, 60000) + } + return this.now + }, + toString(raw, ...args) { + return raw === true ? + this.__proto__.toString.apply(this, args) : + this.toJSON().toString(raw, ...args) + }, + }) + adapter = [] + + express = Object.assign(express(), { quiet: [] }) + .use(express.urlencoded({ extended: false })) + .use(express.json()) + .use(express.raw()) + .use(express.text()) + .use(req => { req.next() let quiet = false for (const i of req.app.quiet) @@ -56,23 +63,27 @@ export default class Yunzai extends EventEmitter { 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.express.use("/exit", req => { + .use("/exit", req => { if (["::1", "::ffff:127.0.0.1"].includes(req.ip) || req.hostname == "localhost") process.exit(1) }) - this.server = http.createServer(this.express) - this.server.on("error", err => { + .use("/File", (...args) => this.fileSend(...args)) + + server = http.createServer(this.express) + .on("error", err => { if (typeof this[`server${err.code}`] == "function") return this[`server${err.code}`](err) this.makeLog("error", err, "Server") }) + .on("upgrade", (...args) => this.wsConnect(...args)) + + wss = new WebSocketServer({ noServer: true }) + wsf = Object.create(null) + fs = Object.create(null) - this.server.on("upgrade", (...args) => this.wsConnect(...args)) - this.wss = new WebSocketServer({ noServer: true }) - this.wsf = Object.create(null) + constructor() { + super() - this.fs = Object.create(null) - this.express.use("/File", (...args) => this.fileSend(...args)) for (const name of [404, "timeout"]) this.fileToUrl(`resources/http/File/${name}.jpg`, { name, time: false, times: false }) @@ -512,7 +523,7 @@ export default class Yunzai extends EventEmitter { } get nickname() { - return this.bots[this.uin].nickname + return this.bots[this.uin]?.nickname || "TRSS Yunzai" } getFriendArray() { @@ -566,28 +577,36 @@ export default class Yunzai extends EventEmitter { const map = new Map for (const bot_id of this.uin) for (const [id, i] of this.bots[bot_id].gml || []) - map.set(id, i) + map.set(id, Object.assign(new Map(i), { bot_id })) return map } pickFriend(user_id) { - user_id = Number(user_id) || String(user_id) - const user = this.fl.get(user_id) + user_id = Number(user_id) || user_id + let user = this.fl.get(user_id) + if (!user) for (const [id, ml] of this.gml) { + user = ml.get(user_id) + if (user) { + user.bot_id = ml.bot_id + break + } + } if (user) return this.bots[user.bot_id].pickFriend(user_id) - this.makeLog("error", ["获取用户对象错误:找不到用户", user_id]) + this.makeLog("debug", ["因不存在用户", user_id, "而随机选择Bot", this.uin.toJSON()]) + return this.bots[this.uin].pickFriend(user_id) } get pickUser() { return this.pickFriend } pickGroup(group_id) { - group_id = Number(group_id) || String(group_id) + group_id = Number(group_id) || group_id const group = this.gl.get(group_id) if (group) return this.bots[group.bot_id].pickGroup(group_id) - this.makeLog("error", ["获取群对象错误:找不到群", group_id]) + this.makeLog("debug", ["因不存在群", group_id, "而随机选择Bot", this.uin.toJSON()]) + return this.bots[this.uin].pickGroup(group_id) } pickMember(group_id, user_id) { - const group = this.pickGroup(group_id) - if (group) return group.pickMember(user_id) + return this.pickGroup(group_id).pickMember(user_id) } sendFriendMsg(bot_id, user_id, msg) {