增加 #喵喵更新 功能

* 若更新成功会重启Yunzai,需要Yunzai以 npm run start 模式启动
* 尚未经充分测试,请有一定容错能力的勇士尝试
* 感谢 @清秋 @碎月 的代码支持
This commit is contained in:
yoimiya-kokomi 2022-04-26 22:25:47 +08:00
parent 788012205b
commit 9290346ce3
4 changed files with 125 additions and 4 deletions

10
CHANGELOG.md Normal file
View File

@ -0,0 +1,10 @@
#1.2.0
* `#角色面板` 增加伤害计算功能
* 目前支持角色:雷神、胡桃、魈
* 可通过 `#怪物等级85` 命令设定怪物等级,以获得更准确的计算结果
* 计算伤害为满Buff情况后续会出更详细的Buff及计算展示
* `#获取游戏角色详情`命令在服务侧增加基于UID的天频度限制
* 增加 `#喵喵更新` 功能
* 若更新成功会重启Yunzai需要Yunzai以 npm run start 模式启动
* 尚未经充分测试,请有一定容错能力的勇士尝试
* 感谢 @碎月 @清秋 的代码支持

View File

@ -5,6 +5,7 @@ import { createRequire } from "module";
import { exec } from "child_process";
import { Cfg } from "../components/index.js";
const require = createRequire(import.meta.url);
let cfgMap = {
@ -25,6 +26,11 @@ export const rule = {
reg: "^#喵喵更新图像$",
describe: "【#管理】更新素材",
},
updateMiaoPlugin: {
hashMark: true,
reg: "^#喵喵(强制)?更新",
describe: "【#管理】喵喵更新",
},
sysCfg: {
hashMark: true,
reg: sysCfgReg,
@ -143,3 +149,51 @@ export async function updateRes(e) {
}
return true;
}
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) {
//console.log(stdout);
if (/Already up to date/.test(stdout)) {
e.reply("目前已经是最新版了~");
return true;
}
if (error) {
e.reply("更新失败!\nError code: " + error.code + "\n" + error.stack + "\n 请稍后重试。");
return true;
}
e.reply("更新成功尝试重新启动Yunzai以应用更新...");
timer && clearTimeout(timer);
redis.set("miao:restart-msg", JSON.stringify({
msg: "重启成功新版喵喵Plugin已经生效",
qq: e.user_id
}), { EX: 30 });
timer = setTimeout(function () {
let command = "npm run restart";
exec(command, function (error, stdout, stderr) {
if (error) {
if (/Yunzai not found/.test(error)) {
e.reply("自动重启失败,请使用 npm run start 命令启动Yunzai-Bot");
} else {
e.reply("重启失败!\nError code: " + error.code + "\n" + error.stack + "\n 请稍后重试。");
}
return true;
}
})
}, 1000);
});
}

45
components/Changelog.js Normal file
View File

@ -0,0 +1,45 @@
import fs from "fs";
import lodash from "lodash";
const _path = process.cwd();
const _logPath = `${_path}/plugins/miao-plugin/CHANGELOG.md`;
let logs = {};
let changelogs = [];
let currentVersion;
let isNew = 1;
let lastVersion = await redis.get("miao:last-version");
try {
if (fs.existsSync(_logPath)) {
logs = fs.readFileSync(_logPath, "utf8") || "";
logs = logs.split("\n");
lodash.forEach(logs, (line) => {
if (isNew === -1) {
return false;
}
let versionRet = /^#\s*([0-9\\.]+)\s*$/.exec(line);
if (versionRet && versionRet[1]) {
let v = versionRet[1];
if (!currentVersion) {
currentVersion = v;
}
if (v === lastVersion) {
isNew = 0;
} else if (isNew === 0) {
isNew = -1;
}
return;
}
if (isNew > -1) {
changelogs.push(line);
}
});
redis.set("miao:last-version", currentVersion, { EX: 3600 * 24 * 300 });
}
} catch (e) {
// do nth
}
export { currentVersion, changelogs };

View File

@ -3,10 +3,11 @@ import { consStat, abyssPct } from "./apps/stat.js";
import { wiki } from "./apps/wiki.js";
import { help } from "./apps/help.js";
import lodash from "lodash";
import common from "../../lib/common.js";
import { rule as adminRule, updateRes, sysCfg, updateMiaoPlugin } from "./apps/admin.js";
import { currentVersion, changelogs } from "./components/Changelog.js";
import { rule as adminRule, updateRes, sysCfg } from "./apps/admin.js";
export { character, wife, consStat, abyssPct, wiki, updateRes, sysCfg, help, getProfile, enemyLv };
export { character, wife, consStat, abyssPct, wiki, updateRes, updateMiaoPlugin, sysCfg, help, getProfile, enemyLv };
let rule = {
@ -54,3 +55,14 @@ lodash.forEach(rule, (r) => {
export { rule };
console.log("喵喵插件初始化~");
setTimeout(async function () {
let msgStr = await redis.get("miao:restart-msg");
if (msgStr) {
let msg = JSON.parse(msgStr);
await common.relpyPrivate(msg.qq, msg.msg);
await redis.del("miao:restart-msg");
let msgs = [`当前喵喵版本: ${currentVersion}`, ...changelogs];
await common.relpyPrivate(msg.qq, msgs.join("\n"));
}
}, 1000);