Miao-Yunzai/apps/sendLog.ts

93 lines
1.9 KiB
TypeScript
Raw Normal View History

2024-06-12 10:47:19 +08:00
import { Plugin } from 'yunzai/core'
2024-06-11 22:10:46 +08:00
import {makeForwardMsg} from 'yunzai/core'
2024-06-11 21:03:42 +08:00
import fs from "node:fs"
import lodash from "lodash"
import moment from "moment"
2024-06-15 11:18:45 +08:00
/**
* tudo
*/
2024-06-11 21:03:42 +08:00
/**
*
*/
2024-06-12 10:47:19 +08:00
export class sendLog extends Plugin {
2024-06-11 21:03:42 +08:00
lineNum = 100
maxNum = 1000
errFile = "logs/error.log"
logFile = `logs/command.${moment().format("YYYY-MM-DD")}.log`
constructor() {
/**
name: "发送日志",
dsc: "发送最近100条运行日志",
*/
2024-06-12 22:36:39 +08:00
super()
this.rule = [
{
reg: "^#(运行|错误)*日志[0-9]*(.*)",
fnc: this.sendLog.name,
permission: "master"
}
]
2024-06-11 21:03:42 +08:00
}
/**
*
* @returns
*/
async sendLog() {
let lineNum = this.e.msg.match(/\d+/g)
if (lineNum) {
this.lineNum = lineNum[0]
} else {
this.keyWord = this.e.msg.replace(/#|运行|错误|日志|\d/g, "")
}
let logFile = this.logFile
let type = "运行"
if (this.e.msg.includes("错误")) {
logFile = this.errFile
type = "错误"
}
if (this.keyWord) type = this.keyWord
const log = this.getLog(logFile)
if (lodash.isEmpty(log))
return this.reply(`暂无相关日志:${type}`)
return this.reply(await makeForwardMsg(this.e, [log.join("\n")], `最近${log.length}${type}日志`))
}
/**
*
* @param logFile
* @returns
*/
getLog(logFile) {
let log = fs.readFileSync(logFile, { encoding: "utf-8" })
log = log.split("\n")
if (this.keyWord) {
for (const i in log)
if (!log[i].includes(this.keyWord))
delete log[i]
} else {
log = lodash.slice(log, (Number(this.lineNum) + 1) * -1)
}
log = log.reverse()
const tmp = []
for (let i of log) {
if (!i) continue
if (this.keyWord && tmp.length >= this.maxNum) return
/* eslint-disable no-control-regex */
i = i.replace(/\x1b[[0-9;]*m/g, "")
i = i.replace(/\r|\n/, "")
tmp.push(i)
}
return tmp
}
}