2023-08-06 15:01:56 +08:00
|
|
|
import plugin from "../../lib/plugins/plugin.js"
|
|
|
|
import common from "../../lib/common/common.js"
|
|
|
|
import fs from "node:fs"
|
|
|
|
import lodash from "lodash"
|
|
|
|
import moment from "moment"
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
export class sendLog extends plugin {
|
2023-07-26 05:23:14 +08:00
|
|
|
constructor() {
|
2023-03-04 14:30:13 +08:00
|
|
|
super({
|
2023-08-06 15:01:56 +08:00
|
|
|
name: "发送日志",
|
|
|
|
dsc: "发送最近100条运行日志",
|
|
|
|
event: "message",
|
2023-03-04 14:30:13 +08:00
|
|
|
rule: [
|
|
|
|
{
|
2023-08-06 15:01:56 +08:00
|
|
|
reg: "^#(运行|错误)*日志[0-9]*(.*)",
|
|
|
|
fnc: "sendLog",
|
|
|
|
permission: "master"
|
2023-03-04 14:30:13 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
})
|
|
|
|
|
2023-08-06 15:01:56 +08:00
|
|
|
this.lineNum = 100
|
|
|
|
this.maxNum = 1000
|
2023-03-04 14:30:13 +08:00
|
|
|
|
2023-08-06 15:01:56 +08:00
|
|
|
this.logFile = `logs/command.${moment().format("YYYY-MM-DD")}.log`
|
|
|
|
this.errFile = "logs/error.log"
|
2023-03-04 14:30:13 +08:00
|
|
|
}
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
async sendLog() {
|
2023-03-04 14:30:13 +08:00
|
|
|
let lineNum = this.e.msg.match(/\d+/g)
|
|
|
|
if (lineNum) {
|
|
|
|
this.lineNum = lineNum[0]
|
|
|
|
} else {
|
2023-08-06 15:01:56 +08:00
|
|
|
this.keyWord = this.e.msg.replace(/#|运行|错误|日志|\d/g, "")
|
2023-03-04 14:30:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
let logFile = this.logFile
|
2023-08-06 15:01:56 +08:00
|
|
|
let type = "运行"
|
|
|
|
if (this.e.msg.includes("错误")) {
|
2023-03-04 14:30:13 +08:00
|
|
|
logFile = this.errFile
|
2023-08-06 15:01:56 +08:00
|
|
|
type = "错误"
|
2023-03-04 14:30:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.keyWord) type = this.keyWord
|
|
|
|
|
2023-08-06 15:01:56 +08:00
|
|
|
const log = this.getLog(logFile)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
2023-08-06 15:01:56 +08:00
|
|
|
if (lodash.isEmpty(log))
|
|
|
|
return this.reply(`暂无相关日志:${type}`)
|
2023-03-04 14:30:13 +08:00
|
|
|
|
2023-08-06 15:01:56 +08:00
|
|
|
return this.reply(await common.makeForwardMsg(this.e, [log.join("\n")], `最近${log.length}条${type}日志`))
|
2023-03-04 14:30:13 +08:00
|
|
|
}
|
|
|
|
|
2023-07-26 05:23:14 +08:00
|
|
|
getLog(logFile) {
|
2023-08-06 15:01:56 +08:00
|
|
|
let log = fs.readFileSync(logFile, { encoding: "utf-8" })
|
|
|
|
log = log.split("\n")
|
2023-03-04 14:30:13 +08:00
|
|
|
|
|
|
|
if (this.keyWord) {
|
2023-08-06 15:01:56 +08:00
|
|
|
for (const i in log)
|
|
|
|
if (!log[i].includes(this.keyWord))
|
|
|
|
delete log[i]
|
2023-03-04 14:30:13 +08:00
|
|
|
} else {
|
|
|
|
log = lodash.slice(log, (Number(this.lineNum) + 1) * -1)
|
|
|
|
}
|
|
|
|
log = log.reverse()
|
2023-08-06 15:01:56 +08:00
|
|
|
|
|
|
|
const tmp = []
|
|
|
|
for (let i of log) {
|
|
|
|
if (!i) continue
|
2023-03-04 14:30:13 +08:00
|
|
|
if (this.keyWord && tmp.length >= this.maxNum) return
|
|
|
|
/* eslint-disable no-control-regex */
|
2023-08-06 15:01:56 +08:00
|
|
|
i = i.replace(/\x1b[[0-9;]*m/g, "")
|
|
|
|
i = i.replace(/\r|\n/, "")
|
|
|
|
tmp.push(i)
|
|
|
|
}
|
2023-03-04 14:30:13 +08:00
|
|
|
return tmp
|
|
|
|
}
|
|
|
|
}
|