2022-07-23 20:32:10 +00:00
|
|
|
|
import fs from 'fs'
|
|
|
|
|
import lodash from 'lodash'
|
|
|
|
|
import { exec } from 'child_process'
|
2023-03-21 19:03:01 +00:00
|
|
|
|
import { Cfg, Common, Data, Version, App } from '#miao'
|
2023-06-06 20:45:52 +00:00
|
|
|
|
import fetch from 'node-fetch'
|
2022-04-08 21:32:54 +00:00
|
|
|
|
|
2022-11-12 21:35:46 +00:00
|
|
|
|
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: '喵喵设置'
|
|
|
|
|
})
|
|
|
|
|
|
2022-11-12 21:35:46 +00:00
|
|
|
|
let sysCfgReg = new RegExp(`^#喵喵设置\\s*(${keys.join('|')})?\\s*(.*)$`)
|
2022-09-16 20:37:09 +00:00
|
|
|
|
|
2023-02-13 19:47:22 +00:00
|
|
|
|
app.reg({
|
|
|
|
|
updateRes: {
|
|
|
|
|
rule: /^#喵喵(强制)?(更新图像|图像更新)$/,
|
|
|
|
|
fn: updateRes,
|
|
|
|
|
desc: '【#管理】更新素材'
|
|
|
|
|
},
|
|
|
|
|
update: {
|
2023-10-12 13:45:58 +00:00
|
|
|
|
rule: /^#喵喵(强制)?更新$/,
|
2023-02-13 19:47:22 +00:00
|
|
|
|
fn: updateMiaoPlugin,
|
|
|
|
|
desc: '【#管理】喵喵更新'
|
|
|
|
|
},
|
|
|
|
|
sysCfg: {
|
|
|
|
|
rule: sysCfgReg,
|
|
|
|
|
fn: sysCfg,
|
|
|
|
|
desc: '【#管理】系统设置'
|
2023-06-06 20:45:52 +00:00
|
|
|
|
},
|
|
|
|
|
miaoApiInfo: {
|
2023-10-12 13:45:58 +00:00
|
|
|
|
rule: /^#喵喵api$/,
|
2023-06-06 20:45:52 +00:00
|
|
|
|
fn: miaoApiInfo,
|
|
|
|
|
desc: '【#管理】喵喵Api'
|
2023-02-13 19:47:22 +00:00
|
|
|
|
}
|
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) {
|
2022-09-20 11:50:27 +00:00
|
|
|
|
if (!e.isMaster) {
|
|
|
|
|
e.reply(`只有主人才能命令喵喵哦~
|
|
|
|
|
(*/ω\*)`)
|
2022-09-21 19:28:33 +00:00
|
|
|
|
return false
|
2022-09-20 11:50:27 +00:00
|
|
|
|
}
|
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)
|
2022-11-12 21:35:46 +00:00
|
|
|
|
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
|
|
|
|
|
2022-11-12 21:35:46 +00:00
|
|
|
|
let cfgSchema = cfgSchemaMap[regRet[1]]
|
|
|
|
|
if (cfgSchema.input) {
|
|
|
|
|
val = cfgSchema.input(val)
|
2022-04-09 21:33:21 +00:00
|
|
|
|
} else {
|
2022-11-13 19:42:24 +00:00
|
|
|
|
val = cfgSchema.type === 'num' ? (val * 1 || cfgSchema.def) : !/关闭/.test(val)
|
2022-04-09 21:33:21 +00:00
|
|
|
|
}
|
2022-11-12 21:35:46 +00:00
|
|
|
|
Cfg.set(cfgSchema.cfgKey, val)
|
2022-04-09 21:33:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-12 21:35:46 +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', {
|
2022-11-12 21:35:46 +00:00
|
|
|
|
schema,
|
|
|
|
|
cfg,
|
2023-03-21 19:03:01 +00:00
|
|
|
|
imgPlus,
|
|
|
|
|
isMiao: Version.isMiao
|
2022-08-24 01:07:06 +00:00
|
|
|
|
}, { 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-04-08 21:32:54 +00:00
|
|
|
|
}
|
2022-10-30 18:36:01 +00:00
|
|
|
|
let isForce = e.msg.includes('强制')
|
2022-07-23 20:32:10 +00:00
|
|
|
|
let command = ''
|
2022-04-08 21:32:54 +00:00
|
|
|
|
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'
|
|
|
|
|
}
|
2022-04-25 21:07:15 +00:00
|
|
|
|
exec(command, { cwd: `${resPath}/miao-res-plus/` }, function (error, stdout, stderr) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
console.log(stdout)
|
2022-07-08 21:49:29 +00:00
|
|
|
|
if (/(Already up[ -]to[ -]date|已经是最新的)/.test(stdout)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
e.reply('目前所有图片都已经是最新了~')
|
|
|
|
|
return true
|
2022-04-08 21:32:54 +00:00
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
let numRet = /(\d*) files changed,/.exec(stdout)
|
2022-04-09 21:41:12 +00:00
|
|
|
|
if (numRet && numRet[1]) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
e.reply(`报告主人,更新成功,此次更新了${numRet[1]}个图片~`)
|
|
|
|
|
return true
|
2022-04-09 21:41:12 +00:00
|
|
|
|
}
|
2022-04-08 21:32:54 +00:00
|
|
|
|
if (error) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
e.reply('更新失败!\nError code: ' + error.code + '\n' + error.stack + '\n 请稍后重试。')
|
2022-04-08 21:32:54 +00:00
|
|
|
|
} else {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
e.reply('图片加量包更新成功~')
|
2022-04-08 21:32:54 +00:00
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
})
|
2022-04-08 21:32:54 +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('开始尝试安装图片加量包,可能会需要一段时间,请耐心等待~')
|
2022-04-08 21:32:54 +00:00
|
|
|
|
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 请稍后重试。')
|
2022-04-08 21:32:54 +00:00
|
|
|
|
} else {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
e.reply('角色图片加量包安装成功!您后续也可以通过 #喵喵更新图像 命令来更新图像')
|
2022-04-08 21:32:54 +00:00
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
})
|
2022-04-08 21:32:54 +00:00
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return true
|
2022-04-26 14:25:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-23 20:32:10 +00:00
|
|
|
|
let timer
|
2022-04-26 14:25:47 +00:00
|
|
|
|
|
2022-09-16 20:37:09 +00:00
|
|
|
|
async function updateMiaoPlugin (e) {
|
2022-04-26 14:25:47 +00:00
|
|
|
|
if (!await checkAuth(e)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
return true
|
2022-04-26 14:25:47 +00:00
|
|
|
|
}
|
2022-07-23 20:32:10 +00:00
|
|
|
|
let isForce = e.msg.includes('强制')
|
|
|
|
|
let command = 'git pull'
|
2022-04-26 14:25:47 +00:00
|
|
|
|
if (isForce) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
command = 'git checkout . && git pull'
|
|
|
|
|
e.reply('正在执行强制更新操作,请稍等')
|
2022-04-26 14:25:47 +00:00
|
|
|
|
} else {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
e.reply('正在执行更新操作,请稍等')
|
2022-04-26 14:25:47 +00:00
|
|
|
|
}
|
|
|
|
|
exec(command, { cwd: `${_path}/plugins/miao-plugin/` }, function (error, stdout, stderr) {
|
2022-07-08 21:49:29 +00:00
|
|
|
|
if (/(Already up[ -]to[ -]date|已经是最新的)/.test(stdout)) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
e.reply('目前已经是最新版喵喵了~')
|
|
|
|
|
return true
|
2022-04-26 14:25:47 +00:00
|
|
|
|
}
|
|
|
|
|
if (error) {
|
2022-07-23 20:32:10 +00:00
|
|
|
|
e.reply('喵喵更新失败!\nError code: ' + error.code + '\n' + error.stack + '\n 请稍后重试。')
|
|
|
|
|
return true
|
2022-04-26 14:25:47 +00:00
|
|
|
|
}
|
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: '重启成功,新版喵喵已经生效',
|
2022-04-26 14:25:47 +00:00
|
|
|
|
qq: e.user_id
|
2022-09-04 09:33:14 +00:00
|
|
|
|
}, 30)
|
2022-04-26 14:25:47 +00:00
|
|
|
|
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'
|
2022-06-16 00:59:43 +00:00
|
|
|
|
}
|
2022-04-26 14:25:47 +00:00
|
|
|
|
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
|
2022-06-16 05:27:49 +00:00
|
|
|
|
} 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-04-26 14:25:47 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
2022-07-23 20:32:10 +00:00
|
|
|
|
}, 1000)
|
|
|
|
|
})
|
|
|
|
|
return true
|
2022-05-28 21:18:13 +00:00
|
|
|
|
}
|
2023-06-06 20:45:52 +00:00
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
}
|