miao-plugin/apps/admin.js

178 lines
5.1 KiB
JavaScript
Raw Normal View History

2022-07-23 20:32:10 +00:00
import fs from 'fs'
import lodash from 'lodash'
import { exec } from 'child_process'
2022-09-16 20:37:09 +00:00
import { Cfg, Common, Data, App } from '../components/index.js'
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: {
rule: /^#喵喵(强制)?更新/,
fn: updateMiaoPlugin,
desc: '【#管理】喵喵更新'
},
sysCfg: {
rule: sysCfgReg,
fn: sysCfg,
desc: '【#管理】系统设置'
}
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
}
2022-09-16 20:37:09 +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)
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
}, { e, scale: 1.4 })
2022-04-09 21:33:21 +00:00
}
2022-09-16 20:37:09 +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) {
2022-07-23 20:32:10 +00:00
console.log(stdout)
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
2022-09-16 20:37:09 +00:00
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
}