miao-plugin/apps/admin.js

241 lines
7.0 KiB
JavaScript
Raw Normal View History

2023-10-24 19:34:36 +00:00
import fs from 'node:fs'
2022-07-23 20:32:10 +00:00
import lodash from 'lodash'
import { exec } from 'child_process'
import { Cfg, Common, Data, Version, App } from '#miao'
import makemsg from "../../../lib/common/common.js"
import { execSync } from "child_process";
import fetch from 'node-fetch'
let keys = lodash.map(Cfg.getCfgSchemaMap(), (i) => i.key)
2022-09-16 20:37:09 +00:00
let app = App.init({
id: 'admin',
name: '喵喵设置',
desc: '喵喵设置'
})
let sysCfgReg = new RegExp(`^#喵喵设置\\s*(${keys.join('|')})?\\s*(.*)$`)
2022-09-16 20:37:09 +00:00
app.reg({
updateRes: {
rule: /^#喵喵(强制)?(更新图像|图像更新)$/,
fn: updateRes,
desc: '【#管理】更新素材'
},
update: {
2023-10-12 13:45:58 +00:00
rule: /^#喵喵(强制)?更新$/,
fn: updateMiaoPlugin,
desc: '【#管理】喵喵更新'
},
updatelog: {
rule: /^#?喵喵更新日志$/,
fn: Miaoupdatelog,
desc: '【#管理】喵喵更新'
},
sysCfg: {
rule: sysCfgReg,
fn: sysCfg,
desc: '【#管理】系统设置'
},
miaoApiInfo: {
2023-10-12 13:45:58 +00:00
rule: /^#喵喵api$/,
fn: miaoApiInfo,
desc: '【#管理】喵喵Api'
}
2022-09-16 20:37:09 +00:00
})
export default app
2022-04-09 21:33:21 +00:00
2022-07-23 20:32:10 +00:00
const _path = process.cwd()
const resPath = `${_path}/plugins/miao-plugin/resources/`
const plusPath = `${resPath}/miao-res-plus/`
2022-04-09 21:33:21 +00:00
const checkAuth = async function (e) {
if (!e.isMaster) {
e.reply(`只有主人才能命令喵喵哦~
(*/ω*)`)
2022-09-21 19:28:33 +00:00
return false
}
2022-12-03 21:17:07 +00:00
return true
2022-04-09 21:33:21 +00:00
}
async function sysCfg(e) {
2022-04-09 21:33:21 +00:00
if (!await checkAuth(e)) {
2022-07-23 20:32:10 +00:00
return true
2022-04-09 21:33:21 +00:00
}
2022-09-16 20:37:09 +00:00
let cfgReg = sysCfgReg
2022-07-23 20:32:10 +00:00
let regRet = cfgReg.exec(e.msg)
let cfgSchemaMap = Cfg.getCfgSchemaMap()
2022-04-09 21:33:21 +00:00
if (!regRet) {
2022-07-23 20:32:10 +00:00
return true
2022-04-09 21:33:21 +00:00
}
if (regRet[1]) {
// 设置模式
2022-07-23 20:32:10 +00:00
let val = regRet[2] || ''
2022-04-09 21:33:21 +00:00
let cfgSchema = cfgSchemaMap[regRet[1]]
if (cfgSchema.input) {
val = cfgSchema.input(val)
2023-11-27 20:18:06 +00:00
} else if (cfgSchema.type === 'str') {
val = (val || cfgSchema.def) + ''
2022-04-09 21:33:21 +00:00
} else {
val = cfgSchema.type === 'num' ? (val * 1 || cfgSchema.def) : !/关闭/.test(val)
2022-04-09 21:33:21 +00:00
}
Cfg.set(cfgSchema.cfgKey, val)
2022-04-09 21:33:21 +00:00
}
let schema = Cfg.getCfgSchema()
let cfg = Cfg.getCfg()
let imgPlus = fs.existsSync(plusPath)
2022-04-09 21:33:21 +00:00
2022-07-23 20:32:10 +00:00
// 渲染图像
return await Common.render('admin/index', {
schema,
cfg,
imgPlus,
isMiao: Version.isMiao
}, { e, scale: 1.4 })
2022-04-09 21:33:21 +00:00
}
async function updateRes(e) {
2022-04-09 21:33:21 +00:00
if (!await checkAuth(e)) {
2022-07-23 20:32:10 +00:00
return true
}
2022-10-30 18:36:01 +00:00
let isForce = e.msg.includes('强制')
2022-07-23 20:32:10 +00:00
let command = ''
if (fs.existsSync(`${resPath}/miao-res-plus/`)) {
2022-07-23 20:32:10 +00:00
e.reply('开始尝试更新,请耐心等待~')
command = 'git pull'
2022-10-30 18:36:01 +00:00
if (isForce) {
command = 'git checkout . && git pull'
}
exec(command, { cwd: `${resPath}/miao-res-plus/` }, function (error, stdout, stderr) {
if (/(Already up[ -]to[ -]date|已经是最新的)/.test(stdout)) {
2022-07-23 20:32:10 +00:00
e.reply('目前所有图片都已经是最新了~')
return true
}
2022-07-23 20:32:10 +00:00
let numRet = /(\d*) files changed,/.exec(stdout)
if (numRet && numRet[1]) {
2022-07-23 20:32:10 +00:00
e.reply(`报告主人,更新成功,此次更新了${numRet[1]}个图片~`)
return true
}
if (error) {
2022-07-23 20:32:10 +00:00
e.reply('更新失败!\nError code: ' + error.code + '\n' + error.stack + '\n 请稍后重试。')
} else {
2022-07-23 20:32:10 +00:00
e.reply('图片加量包更新成功~')
}
2022-07-23 20:32:10 +00:00
})
} else {
2022-10-30 18:36:01 +00:00
command = `git clone https://gitee.com/yoimiya-kokomi/miao-res-plus.git "${resPath}/miao-res-plus/" --depth=1`
2022-07-23 20:32:10 +00:00
e.reply('开始尝试安装图片加量包,可能会需要一段时间,请耐心等待~')
exec(command, function (error, stdout, stderr) {
if (error) {
2022-07-23 20:32:10 +00:00
e.reply('角色图片加量包安装失败!\nError code: ' + error.code + '\n' + error.stack + '\n 请稍后重试。')
} else {
2022-07-23 20:32:10 +00:00
e.reply('角色图片加量包安装成功!您后续也可以通过 #喵喵更新图像 命令来更新图像')
}
2022-07-23 20:32:10 +00:00
})
}
2022-07-23 20:32:10 +00:00
return true
}
2022-07-23 20:32:10 +00:00
let timer
async function updateMiaoPlugin(e) {
if (!await checkAuth(e)) {
2022-07-23 20:32:10 +00:00
return true
}
2022-07-23 20:32:10 +00:00
let isForce = e.msg.includes('强制')
let command = 'git pull'
if (isForce) {
2022-07-23 20:32:10 +00:00
command = 'git checkout . && git pull'
e.reply('正在执行强制更新操作,请稍等')
} else {
2022-07-23 20:32:10 +00:00
e.reply('正在执行更新操作,请稍等')
}
exec(command, { cwd: `${_path}/plugins/miao-plugin/` }, function (error, stdout, stderr) {
if (/(Already up[ -]to[ -]date|已经是最新的)/.test(stdout)) {
2022-07-23 20:32:10 +00:00
e.reply('目前已经是最新版喵喵了~')
return true
}
if (error) {
2022-07-23 20:32:10 +00:00
e.reply('喵喵更新失败!\nError code: ' + error.code + '\n' + error.stack + '\n 请稍后重试。')
return true
}
2022-07-23 20:32:10 +00:00
e.reply('喵喵更新成功正在尝试重新启动Yunzai以应用更新...')
timer && clearTimeout(timer)
2022-09-04 09:33:14 +00:00
Data.setCacheJSON('miao:restart-msg', {
2022-07-23 20:32:10 +00:00
msg: '重启成功,新版喵喵已经生效',
qq: e.user_id
2022-09-04 09:33:14 +00:00
}, 30)
timer = setTimeout(function () {
2022-07-23 20:32:10 +00:00
let command = 'npm run start'
if (process.argv[1].includes('pm2')) {
command = 'npm run restart'
}
exec(command, function (error, stdout, stderr) {
if (error) {
2022-07-23 20:32:10 +00:00
e.reply('自动重启失败,请手动重启以应用新版喵喵。\nError code: ' + error.code + '\n' + error.stack + '\n')
Bot.logger.error(`重启失败\n${error.stack}`)
return true
} else if (stdout) {
2022-07-23 20:32:10 +00:00
Bot.logger.mark('重启成功运行已转为后台查看日志请用命令npm run log')
Bot.logger.mark('停止后台运行命令npm stop')
process.exit()
}
})
2022-07-23 20:32:10 +00:00
}, 1000)
})
return true
}
async function Miaoupdatelog(e, plugin = 'miao-plugin') {
let cm = 'git log -20 --oneline --pretty=format:"%h||[%cd] %s" --date=format:"%F %T"'
if (plugin) {
cm = `cd ./plugins/${plugin}/ && ${cm}`
}
let logAll
try {
logAll = await execSync(cm, { encoding: 'utf-8', windowsHide: true })
} catch (error) {
logger.error(error.toString())
this.reply(error.toString())
}
if (!logAll) return false
logAll = logAll.split('\n')
let log = []
for (let str of logAll) {
str = str.split('||')
if (str[0] == this.oldCommitId) break
if (str[1].includes('Merge branch')) continue
log.push(str[1])
}
let line = log.length
log = log.join('\n\n')
if (log.length <= 0) return ''
let end = '更多详细信息请前往gitee查看\nhttps://gitee.com/yoimiya-kokomi/miao-plugin'
log = await makemsg.makeForwardMsg(this.e, [log, end], `${plugin}更新日志,共${line}`)
e.reply(log)
}
async function miaoApiInfo(e) {
if (!await checkAuth(e)) {
return true
}
let { diyCfg } = await Data.importCfg('profile')
let { qq, token } = (diyCfg?.miaoApi || {})
if (!qq || !token) {
return e.reply('未正确填写miaoApi token请检查miao-plugin/config/profile.js文件')
}
if (token.length !== 32) {
return e.reply('miaoApi token格式错误')
}
let req = await fetch(`http://miao.games/api/info?qq=${qq}&token=${token}`)
let data = await req.json()
if (data.status !== 0) {
return e.reply('token检查错误请求失败')
}
e.reply(data.msg)
}