server 适配trss

This commit is contained in:
zyc404 2023-10-14 19:38:14 +08:00
parent fe132985fd
commit fb05cccd5b
48 changed files with 900 additions and 963 deletions

View file

@ -7,7 +7,6 @@ import websocket from '@fastify/websocket'
import fs from 'fs'
import path from 'path'
import os from 'os'
import schedule from 'node-schedule'
import websocketclient from 'ws'
import { Config } from '../utils/config.js'
@ -24,25 +23,6 @@ const server = fastify({
logger: Config.debug
})
let Statistics = {
SystemAccess: {
count: 0,
oldCount: 0
},
CacheFile: {
count: 0,
oldCount: 0
},
WebAccess: {
count: 0,
oldCount: 0
},
SystemLoad: {
count: 0,
oldCount: 0
}
}
async function getLoad() {
// 获取当前操作系统平台
const platform = os.platform()
@ -290,7 +270,6 @@ export async function createServer() {
time: data.time
})
await setUserData(body.qq, user)
Statistics.CacheFile.count += 1
reply.send({ file: body.entry, cacheUrl: `http://${ip}:${Config.serverPort || 3321}/page/${body.entry}` })
} catch (err) {
server.log.error(`用户生成缓存${body.entry}时发生错误: ${err}`)
@ -299,12 +278,6 @@ export async function createServer() {
}
return reply
})
// 获取系统状态
server.post('/system-statistics', async (request, reply) => {
Statistics.SystemLoad.count = await getLoad()
reply.send(Statistics)
return reply
})
// 清除缓存数据
server.post('/cleanCache', async (request, reply) => {
@ -333,6 +306,7 @@ export async function createServer() {
connection.socket.send(JSON.stringify(response))
})
connection.socket.on('message', async (message) => {
const isTrss = Array.isArray(Bot.uin)
try {
const data = JSON.parse(message)
const user = UserInfo(data.token)
@ -344,10 +318,14 @@ export async function createServer() {
}
if (data.id && data.message) {
if (data.group) {
Bot.sendGroupMsg(parseInt(data.id), data.message, data.quotable)
if (isTrss) {
Bot[user.user].pickGroup(parseInt(data.id)).sendMsg(data.message)
} else {
Bot.sendGroupMsg(parseInt(data.id), data.message, data.quotable)
}
} else {
if (Array.isArray(Bot.uin)) {
Bot.pickFriend(parseInt(data.id)).sendMsg(data.message)
if (isTrss) {
Bot[user.user].pickFriend(parseInt(data.id)).sendMsg(data.message)
} else {
Bot.sendPrivateMsg(parseInt(data.id), data.message, data.quotable)
}
@ -365,7 +343,6 @@ export async function createServer() {
}
break
case 'login': // 登录
if (user) {
clients[user.user] = connection.socket
connection.login = true
@ -379,13 +356,13 @@ export async function createServer() {
await connection.socket.send(JSON.stringify({ command: data.command, state: false, error: '请先登录账号' }))
return
}
if (user.autho != 'admin') {
if (user?.autho != 'admin') {
await connection.socket.send(JSON.stringify({ command: data.command, state: true, error: '普通用户无需进行初始化' }))
return
}
const groupList = Bot.getGroupList()
const groupList = await Bot[user.user].getGroupList()
groupList.forEach(async (item) => {
const group = Bot.pickGroup(item.group_id)
const group = Bot[user.user].pickGroup(item.group_id)
const groupMessages = await group.getChatHistory()
groupMessages.forEach(async (e) => {
const messageData = {
@ -416,6 +393,7 @@ export async function createServer() {
break
}
} catch (error) {
console.error(error)
await connection.socket.send(JSON.stringify({ "error": error.message }))
}
})
@ -432,7 +410,7 @@ export async function createServer() {
message: e.message,
sender: e.sender,
group: {
isGroup: e.isGroup,
isGroup: e.isGroup || e.group_id != undefined,
group_id: e.group_id,
group_name: e.group_name
},
@ -606,28 +584,6 @@ export async function createServer() {
return reply
})
server.addHook('onRequest', (request, reply, done) => {
if (request.method == 'POST') { Statistics.SystemAccess.count += 1 }
if (request.method == 'GET') { Statistics.WebAccess.count += 1 }
done()
})
// 定时任务
let rule = new schedule.RecurrenceRule()
rule.hour = 0
rule.minute = 0
let job_Statistics = schedule.scheduleJob(rule, function () {
Statistics.SystemAccess.oldCount = Statistics.SystemAccess.count
Statistics.CacheFile.oldCount = Statistics.CacheFile.count
Statistics.WebAccess.oldCount = Statistics.WebAccess.count
Statistics.SystemAccess.count = 0
Statistics.CacheFile.count = 0
Statistics.WebAccess.count = 0
})
let job_Statistics_SystemLoad = schedule.scheduleJob('0 * * * *', async function () {
Statistics.SystemLoad.count = await getLoad()
Statistics.SystemLoad.oldCount = Statistics.SystemLoad.count
})
server.listen({
port: Config.serverPort || 3321,
host: '::'