修复 米游社公告 重复标题 (#219)

* 修复 米游社公告 重复标题

* 优化 日志
This commit is contained in:
时雨◎星空 2023-08-06 15:01:56 +08:00 committed by GitHub
parent d3c2da6bdb
commit 9b9ec42ffa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 36 additions and 41 deletions

View File

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