2022-04-08 21:32:54 +00:00
|
|
|
|
import { segment } from "oicq";
|
|
|
|
|
import fs from "fs";
|
|
|
|
|
import lodash from "lodash";
|
|
|
|
|
import { createRequire } from "module";
|
|
|
|
|
import { exec } from "child_process";
|
2022-04-09 21:33:21 +00:00
|
|
|
|
import { Cfg } from "../components/index.js";
|
2022-05-22 13:10:10 +00:00
|
|
|
|
import Common from "../components/Common.js";
|
2022-04-08 21:32:54 +00:00
|
|
|
|
|
2022-04-26 14:25:47 +00:00
|
|
|
|
|
2022-04-08 21:32:54 +00:00
|
|
|
|
const require = createRequire(import.meta.url);
|
|
|
|
|
|
2022-04-09 21:33:21 +00:00
|
|
|
|
let cfgMap = {
|
|
|
|
|
"角色": "char.char",
|
2022-05-22 13:10:10 +00:00
|
|
|
|
"面板": "char.profile",
|
2022-04-09 21:33:21 +00:00
|
|
|
|
"老婆": "char.wife",
|
2022-06-16 20:19:27 +00:00
|
|
|
|
"小清新": "char.se",
|
2022-04-09 21:33:21 +00:00
|
|
|
|
"查他人": "char.queryOther",
|
2022-05-22 13:10:10 +00:00
|
|
|
|
"图鉴": "wiki.wiki",
|
2022-04-09 21:33:21 +00:00
|
|
|
|
"图片": "wiki.pic",
|
|
|
|
|
"深渊": "wiki.abyss",
|
|
|
|
|
"渲染": "sys.scale",
|
2022-04-10 21:11:39 +00:00
|
|
|
|
"帮助": "sys.help",
|
2022-04-09 21:33:21 +00:00
|
|
|
|
};
|
|
|
|
|
let sysCfgReg = `^#喵喵设置\s*(${lodash.keys(cfgMap).join("|")})?\s*(.*)$`;
|
2022-04-08 21:32:54 +00:00
|
|
|
|
export const rule = {
|
|
|
|
|
updateRes: {
|
2022-04-10 20:48:53 +00:00
|
|
|
|
hashMark: true,
|
2022-04-09 21:33:21 +00:00
|
|
|
|
reg: "^#喵喵更新图像$",
|
|
|
|
|
describe: "【#管理】更新素材",
|
|
|
|
|
},
|
2022-04-26 14:25:47 +00:00
|
|
|
|
updateMiaoPlugin: {
|
|
|
|
|
hashMark: true,
|
|
|
|
|
reg: "^#喵喵(强制)?更新",
|
|
|
|
|
describe: "【#管理】喵喵更新",
|
|
|
|
|
},
|
2022-04-09 21:33:21 +00:00
|
|
|
|
sysCfg: {
|
2022-04-10 20:48:53 +00:00
|
|
|
|
hashMark: true,
|
2022-04-09 21:33:21 +00:00
|
|
|
|
reg: sysCfgReg,
|
|
|
|
|
describe: "【#管理】系统设置"
|
2022-05-28 21:18:13 +00:00
|
|
|
|
},
|
|
|
|
|
profileCfg: {
|
|
|
|
|
hashMark: true,
|
|
|
|
|
reg: "^#喵喵面板(?:设置)?.*",
|
|
|
|
|
describe: "【#管理】面板设置"
|
2022-04-08 21:32:54 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2022-04-09 21:33:21 +00:00
|
|
|
|
|
2022-04-08 21:32:54 +00:00
|
|
|
|
const _path = process.cwd();
|
|
|
|
|
const resPath = `${_path}/plugins/miao-plugin/resources/`;
|
2022-04-09 21:33:21 +00:00
|
|
|
|
const plusPath = `${resPath}/miao-res-plus/`;
|
|
|
|
|
|
|
|
|
|
const checkAuth = async function (e) {
|
|
|
|
|
return await e.checkAuth({
|
|
|
|
|
auth: "master",
|
|
|
|
|
replyMsg: `只有主人才能命令喵喵哦~
|
|
|
|
|
(*/ω\*)`
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function sysCfg(e, { render }) {
|
|
|
|
|
if (!await checkAuth(e)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cfgReg = new RegExp(sysCfgReg);
|
|
|
|
|
let regRet = cfgReg.exec(e.msg);
|
|
|
|
|
|
|
|
|
|
if (!regRet) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (regRet[1]) {
|
|
|
|
|
// 设置模式
|
|
|
|
|
let val = regRet[2] || "";
|
|
|
|
|
|
|
|
|
|
let cfgKey = cfgMap[regRet[1]];
|
|
|
|
|
if (cfgKey === "sys.scale") {
|
|
|
|
|
val = Math.min(200, Math.max(50, val * 1 || 100));
|
|
|
|
|
} else {
|
|
|
|
|
val = !/关闭/.test(val);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cfgKey) {
|
|
|
|
|
Cfg.set(cfgKey, val);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cfg = {
|
|
|
|
|
chars: getStatus("char.char"),
|
2022-05-22 13:10:10 +00:00
|
|
|
|
profile: getStatus("char.profile"),
|
2022-04-09 21:33:21 +00:00
|
|
|
|
wife: getStatus("char.wife"),
|
2022-06-16 20:19:27 +00:00
|
|
|
|
se: getStatus("char.se", false),
|
2022-04-09 21:33:21 +00:00
|
|
|
|
other: getStatus("char.queryOther"),
|
2022-05-22 13:10:10 +00:00
|
|
|
|
wiki: getStatus("wiki.wiki"),
|
2022-04-09 21:33:21 +00:00
|
|
|
|
pic: getStatus("wiki.pic"),
|
2022-04-11 20:22:21 +00:00
|
|
|
|
abyss: getStatus("wiki.abyss"),
|
2022-04-09 21:33:21 +00:00
|
|
|
|
imgPlus: fs.existsSync(plusPath),
|
2022-04-10 21:11:39 +00:00
|
|
|
|
help: getStatus("sys.help", false),
|
2022-04-09 21:33:21 +00:00
|
|
|
|
scale: Cfg.get("sys.scale", 100)
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 13:10:10 +00:00
|
|
|
|
//渲染图像
|
|
|
|
|
return await Common.render("admin/index", {
|
2022-04-09 21:33:21 +00:00
|
|
|
|
...cfg,
|
2022-05-22 13:10:10 +00:00
|
|
|
|
}, { e, render, scale: 1.4 });
|
2022-04-09 21:33:21 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-10 21:11:39 +00:00
|
|
|
|
const getStatus = function (rote, def = true) {
|
|
|
|
|
if (Cfg.get(rote, def)) {
|
2022-04-09 21:33:21 +00:00
|
|
|
|
return `<div class="cfg-status" >已开启</div>`;
|
|
|
|
|
} else {
|
|
|
|
|
return `<div class="cfg-status status-off">已关闭</div>`;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-08 21:32:54 +00:00
|
|
|
|
|
|
|
|
|
export async function updateRes(e) {
|
2022-04-09 21:33:21 +00:00
|
|
|
|
if (!await checkAuth(e)) {
|
2022-04-08 21:32:54 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
let command = "";
|
|
|
|
|
if (fs.existsSync(`${resPath}/miao-res-plus/`)) {
|
2022-04-10 20:48:53 +00:00
|
|
|
|
e.reply("开始尝试更新,请耐心等待~");
|
2022-04-25 21:07:15 +00:00
|
|
|
|
command = `git pull`;
|
|
|
|
|
exec(command, { cwd: `${resPath}/miao-res-plus/` }, function (error, stdout, stderr) {
|
2022-06-06 07:27:33 +00:00
|
|
|
|
if (/Already up[ -]to[ -]date/.test(stdout)) {
|
2022-04-10 20:48:53 +00:00
|
|
|
|
e.reply("目前所有图片都已经是最新了~");
|
2022-04-09 21:33:21 +00:00
|
|
|
|
return true;
|
2022-04-08 21:32:54 +00:00
|
|
|
|
}
|
2022-04-09 21:41:12 +00:00
|
|
|
|
let numRet = /(\d*) files changed,/.exec(stdout);
|
|
|
|
|
if (numRet && numRet[1]) {
|
2022-04-10 20:48:53 +00:00
|
|
|
|
e.reply(`报告主人,更新成功,此次更新了${numRet[1]}个图片~`);
|
2022-04-09 21:41:12 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-04-08 21:32:54 +00:00
|
|
|
|
if (error) {
|
2022-04-09 21:33:21 +00:00
|
|
|
|
e.reply("更新失败!\nError code: " + error.code + "\n" + error.stack + "\n 请稍后重试。");
|
2022-04-08 21:32:54 +00:00
|
|
|
|
} else {
|
2022-04-10 20:48:53 +00:00
|
|
|
|
e.reply("图片加量包更新成功~");
|
2022-04-08 21:32:54 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2022-04-24 12:40:52 +00:00
|
|
|
|
command = `git clone https://gitee.com/yoimiya-kokomi/miao-res-plus.git "${resPath}/miao-res-plus/"`;
|
2022-04-10 20:48:53 +00:00
|
|
|
|
e.reply("开始尝试安装图片加量包,可能会需要一段时间,请耐心等待~");
|
2022-04-08 21:32:54 +00:00
|
|
|
|
exec(command, function (error, stdout, stderr) {
|
|
|
|
|
if (error) {
|
2022-04-10 20:48:53 +00:00
|
|
|
|
e.reply("角色图片加量包安装失败!\nError code: " + error.code + "\n" + error.stack + "\n 请稍后重试。");
|
2022-04-08 21:32:54 +00:00
|
|
|
|
} else {
|
2022-04-10 20:48:53 +00:00
|
|
|
|
e.reply("角色图片加量包安装成功!您后续也可以通过 #喵喵更新图像 命令来更新图像");
|
2022-04-08 21:32:54 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2022-04-26 14:25:47 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let timer;
|
|
|
|
|
|
|
|
|
|
export async function updateMiaoPlugin(e) {
|
|
|
|
|
if (!await checkAuth(e)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
let isForce = e.msg.includes("强制");
|
|
|
|
|
let command = "git pull";
|
|
|
|
|
if (isForce) {
|
|
|
|
|
command = "git checkout . && git pull";
|
|
|
|
|
e.reply("正在执行强制更新操作,请稍等");
|
|
|
|
|
} else {
|
|
|
|
|
e.reply("正在执行更新操作,请稍等");
|
|
|
|
|
}
|
|
|
|
|
exec(command, { cwd: `${_path}/plugins/miao-plugin/` }, function (error, stdout, stderr) {
|
2022-06-06 07:27:33 +00:00
|
|
|
|
if (/Already up[ -]to[ -]date/.test(stdout)) {
|
2022-05-22 13:10:10 +00:00
|
|
|
|
e.reply("目前已经是最新版喵喵了~");
|
2022-04-26 14:25:47 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
if (error) {
|
2022-05-22 13:10:10 +00:00
|
|
|
|
e.reply("喵喵更新失败!\nError code: " + error.code + "\n" + error.stack + "\n 请稍后重试。");
|
2022-04-26 14:25:47 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2022-05-22 13:10:10 +00:00
|
|
|
|
e.reply("喵喵更新成功,尝试重新启动Yunzai以应用更新...");
|
2022-04-26 14:25:47 +00:00
|
|
|
|
timer && clearTimeout(timer);
|
|
|
|
|
redis.set("miao:restart-msg", JSON.stringify({
|
2022-05-22 13:10:10 +00:00
|
|
|
|
msg: "重启成功,新版喵喵已经生效",
|
2022-04-26 14:25:47 +00:00
|
|
|
|
qq: e.user_id
|
|
|
|
|
}), { EX: 30 });
|
|
|
|
|
timer = setTimeout(function () {
|
2022-06-16 00:59:43 +00:00
|
|
|
|
let command = `npm run start`;
|
2022-06-16 04:38:58 +00:00
|
|
|
|
if (process.argv[1].includes("pm2")) {
|
2022-06-16 00:59:43 +00:00
|
|
|
|
command = `npm run restart`;
|
|
|
|
|
}
|
2022-04-26 14:25:47 +00:00
|
|
|
|
exec(command, function (error, stdout, stderr) {
|
|
|
|
|
if (error) {
|
2022-06-16 04:50:01 +00:00
|
|
|
|
e.reply("自动重启失败,请手动重启以应用新版喵喵。\nError code: " + error.code + "\n" + error.stack + "\n");
|
2022-06-16 04:44:32 +00:00
|
|
|
|
Bot.logger.error('重启失败\n${error.stack}');
|
2022-04-26 14:25:47 +00:00
|
|
|
|
return true;
|
2022-06-16 05:27:49 +00:00
|
|
|
|
} else if (stdout) {
|
|
|
|
|
Bot.logger.mark("重启成功,运行已转为后台,查看日志请用命令:npm run log");
|
|
|
|
|
Bot.logger.mark("停止后台运行命令:npm stop");
|
|
|
|
|
process.exit();
|
2022-04-26 14:25:47 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
|
|
});
|
2022-04-26 19:07:58 +00:00
|
|
|
|
return true;
|
2022-05-28 21:18:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function profileCfg(e, { render }) {
|
|
|
|
|
if (!await checkAuth(e)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let keyMap = {
|
|
|
|
|
"好友": "friend",
|
|
|
|
|
"群": "group",
|
|
|
|
|
"陌生人": "stranger"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let regRet = /喵喵面板(?:设置)?\s*(好友|群|群聊|陌生人)?\s*(\d*)\s*(开启|关闭|删除)?\s*$/.exec(e.msg);
|
|
|
|
|
|
|
|
|
|
if (!regRet) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let [, target, groupId, actionType] = regRet;
|
|
|
|
|
if (target === "群聊") {
|
|
|
|
|
target = "群";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (target) {
|
|
|
|
|
if (groupId && (target === "群" || !target)) {
|
|
|
|
|
if (actionType === "删除") {
|
|
|
|
|
Cfg.del(`profile.groups.群${groupId}`);
|
|
|
|
|
} else {
|
|
|
|
|
Cfg.set(`profile.groups.群${groupId}.status`, actionType !== "关闭");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
Cfg.set(`profile.${keyMap[target]}.status`, actionType !== "关闭");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cfg = {
|
|
|
|
|
groups: []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lodash.forEach(['friend', 'group', 'stranger'], (key) => {
|
|
|
|
|
cfg[key] = getStatus(`profile.${key}.status`, true)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let groups = Cfg.get('profile.groups', {});
|
|
|
|
|
lodash.forEach(lodash.keys(groups), (group, idx) => {
|
|
|
|
|
|
|
|
|
|
if (lodash.isUndefined(groups[group])) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
cfg.groups.push({
|
|
|
|
|
group,
|
|
|
|
|
idx: idx + 1,
|
|
|
|
|
status: getStatus(`profile.groups.${group}.status`, true)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
//渲染图像
|
|
|
|
|
return await Common.render("admin/profile", {
|
|
|
|
|
...cfg,
|
|
|
|
|
}, { e, render, scale: 1.4 });
|
|
|
|
|
|
2022-06-16 05:27:49 +00:00
|
|
|
|
}
|