miao-plugin/apps/admin.js

267 lines
7.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'
import { MysApi } from '../models/index.js'
2022-04-09 21:33:21 +00:00
let cfgMap = {
2022-07-23 20:32:10 +00:00
角色: 'char.char',
2022-08-18 10:13:42 +00:00
面板: 'char.profile',
2022-07-23 20:32:10 +00:00
老婆: 'char.wife',
戳一戳: 'char.poke',
小清新: 'char.se',
查他人: 'char.queryOther',
图鉴: 'wiki.wiki',
图片: 'wiki.pic',
统计: 'wiki.stat',
2022-07-23 20:32:10 +00:00
深渊: 'wiki.abyss',
渲染: 'sys.scale',
帮助: 'sys.help'
}
2022-09-16 20:37:09 +00:00
let app = App.init({
id: 'admin',
name: '喵喵设置',
desc: '喵喵设置'
})
let sysCfgReg = new RegExp(`^#喵喵设置\\s*(${lodash.keys(cfgMap).join('|')})?\\s*(.*)$`)
app.reg('update-res', updateRes, {
rule: /^#喵喵(更新图像|图像更新)$/,
desc: '【#管理】更新素材'
})
app.reg('update', updateMiaoPlugin, {
rule: /^#喵喵(强制)?更新/,
desc: '【#管理】喵喵更新'
})
app.reg('sys-cfg', sysCfg, {
rule: sysCfgReg,
desc: '【#管理】系统设置'
})
app.reg('profile-cfg', profileCfg, {
rule: /^#喵喵面板(?:设置)?.*/,
desc: '面板设置'
})
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
}
return await MysApi.initUser(e)
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-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-07-23 20:32:10 +00:00
let cfgKey = cfgMap[regRet[1]]
if (cfgKey === 'sys.scale') {
val = Math.min(200, Math.max(50, val * 1 || 100))
2022-04-09 21:33:21 +00:00
} else {
2022-07-23 20:32:10 +00:00
val = !/关闭/.test(val)
2022-04-09 21:33:21 +00:00
}
if (cfgKey) {
2022-07-23 20:32:10 +00:00
Cfg.set(cfgKey, val)
2022-04-09 21:33:21 +00:00
}
}
let cfg = {
2022-07-23 20:32:10 +00:00
chars: getStatus('char.char'),
2022-08-18 10:13:42 +00:00
profile: getStatus('char.profile'),
2022-07-23 20:32:10 +00:00
wife: getStatus('char.wife'),
poke: getStatus('char.poke'),
se: getStatus('char.se', false),
other: getStatus('char.queryOther'),
wiki: getStatus('wiki.wiki'),
pic: getStatus('wiki.pic'),
stat: getStatus('wiki.stat'),
abyss: getStatus('wiki.abyss', false),
2022-04-09 21:33:21 +00:00
imgPlus: fs.existsSync(plusPath),
2022-07-23 20:32:10 +00:00
help: getStatus('sys.help', false),
scale: Cfg.get('sys.scale', 100)
2022-04-09 21:33:21 +00:00
}
2022-07-23 20:32:10 +00:00
// 渲染图像
return await Common.render('admin/index', {
...cfg
}, { e, scale: 1.4 })
2022-04-09 21:33:21 +00:00
}
const getStatus = function (rote, def = true) {
if (Cfg.get(rote, def)) {
2022-07-23 20:32:10 +00:00
return '<div class="cfg-status" >已开启</div>'
2022-04-09 21:33:21 +00:00
} else {
2022-07-23 20:32:10 +00:00
return '<div class="cfg-status status-off">已关闭</div>'
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-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'
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-07-23 20:32:10 +00:00
command = `git clone https://gitee.com/yoimiya-kokomi/miao-res-plus.git "${resPath}/miao-res-plus/"`
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
}
2022-09-16 20:37:09 +00:00
async function profileCfg (e) {
if (!await checkAuth(e)) {
2022-07-23 20:32:10 +00:00
return true
}
let keyMap = {
2022-07-23 20:32:10 +00:00
好友: 'friend',
: 'group',
陌生人: 'stranger'
}
2022-07-23 20:32:10 +00:00
let regRet = /喵喵面板(?:设置)?\s*(好友|群|群聊|陌生人)?\s*(\d*)\s*(开启|关闭|删除)?\s*$/.exec(e.msg)
if (!regRet) {
2022-07-23 20:32:10 +00:00
return
}
2022-07-23 20:32:10 +00:00
let [, target, groupId, actionType] = regRet
if (target === '群聊') {
target = '群'
}
if (target) {
2022-07-23 20:32:10 +00:00
if (groupId && (target === '群' || !target)) {
if (actionType === '删除') {
Cfg.del(`profile.groups.群${groupId}`)
} else {
2022-07-23 20:32:10 +00:00
Cfg.set(`profile.groups.群${groupId}.status`, actionType !== '关闭')
}
} else {
2022-07-23 20:32:10 +00:00
Cfg.set(`profile.${keyMap[target]}.status`, actionType !== '关闭')
}
}
let cfg = {
groups: []
}
lodash.forEach(['friend', 'group', 'stranger'], (key) => {
cfg[key] = getStatus(`profile.${key}.status`, true)
2022-07-23 20:32:10 +00:00
})
2022-08-18 10:13:42 +00:00
let groups = Cfg.get('profile.groups', {})
lodash.forEach(lodash.keys(groups), (group, idx) => {
if (lodash.isUndefined(groups[group])) {
2022-07-23 20:32:10 +00:00
return
}
cfg.groups.push({
group,
idx: idx + 1,
status: getStatus(`profile.groups.${group}.status`, true)
})
})
2022-07-23 20:32:10 +00:00
// 渲染图像
return await Common.render('admin/profile', {
...cfg
}, { e, scale: 1.4 })
}