Skip to content

Commit

Permalink
细节优化
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeRainStarSky committed May 25, 2024
1 parent 3f2530a commit f69d350
Showing 1 changed file with 61 additions and 42 deletions.
103 changes: 61 additions & 42 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 })

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit f69d350

Please sign in to comment.