mioki 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +74 -28
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +7 -5
- package/dist/index.d.mts +7 -5
- package/dist/index.mjs +73 -25
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -4
- package/readme.md +63 -0
package/dist/index.cjs
CHANGED
|
@@ -38,13 +38,10 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
38
38
|
}) : target, mod));
|
|
39
39
|
|
|
40
40
|
//#endregion
|
|
41
|
-
let pino = require("pino");
|
|
42
|
-
pino = __toESM(pino);
|
|
43
41
|
let node_fs = require("node:fs");
|
|
44
42
|
node_fs = __toESM(node_fs);
|
|
45
43
|
let node_path = require("node:path");
|
|
46
44
|
node_path = __toESM(node_path);
|
|
47
|
-
let node_process = require("node:process");
|
|
48
45
|
let mri = require("mri");
|
|
49
46
|
mri = __toESM(mri);
|
|
50
47
|
let node_crypto = require("node:crypto");
|
|
@@ -64,6 +61,9 @@ let dedent = require("dedent");
|
|
|
64
61
|
dedent = __toESM(dedent);
|
|
65
62
|
let systeminformation = require("systeminformation");
|
|
66
63
|
systeminformation = __toESM(systeminformation);
|
|
64
|
+
let consola_utils = require("consola/utils");
|
|
65
|
+
let consola_core = require("consola/core");
|
|
66
|
+
let node_process = require("node:process");
|
|
67
67
|
let node_os = require("node:os");
|
|
68
68
|
node_os = __toESM(node_os);
|
|
69
69
|
let node_child_process = require("node:child_process");
|
|
@@ -71,19 +71,6 @@ node_child_process = __toESM(node_child_process);
|
|
|
71
71
|
let node_cron = require("node-cron");
|
|
72
72
|
node_cron = __toESM(node_cron);
|
|
73
73
|
|
|
74
|
-
//#region src/logger.ts
|
|
75
|
-
const getMiokiLogger = (level) => {
|
|
76
|
-
return (0, pino.default)({
|
|
77
|
-
level,
|
|
78
|
-
name: "mioki",
|
|
79
|
-
transport: {
|
|
80
|
-
target: "pino-pretty",
|
|
81
|
-
options: { colorize: true }
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
//#endregion
|
|
87
74
|
//#region src/utils.ts
|
|
88
75
|
var utils_exports = /* @__PURE__ */ __export({
|
|
89
76
|
ChromeUA: () => ChromeUA,
|
|
@@ -791,7 +778,7 @@ const START_TIME = /* @__PURE__ */ new Date();
|
|
|
791
778
|
var config_exports = /* @__PURE__ */ __export({
|
|
792
779
|
BOT_CWD: () => BOT_CWD,
|
|
793
780
|
botConfig: () => botConfig,
|
|
794
|
-
getLogFilePath: () => getLogFilePath,
|
|
781
|
+
getLogFilePath: () => getLogFilePath$1,
|
|
795
782
|
hasRight: () => hasRight,
|
|
796
783
|
isAdmin: () => isAdmin,
|
|
797
784
|
isInPm2: () => isInPm2,
|
|
@@ -876,14 +863,57 @@ const isInPm2 = Boolean("pm_id" in process.env || "PM2_USAGE" in process.env);
|
|
|
876
863
|
/**
|
|
877
864
|
* 获取日志文件名
|
|
878
865
|
*/
|
|
879
|
-
function getLogFilePath(uin, platformName) {
|
|
866
|
+
function getLogFilePath$1(uin, platformName) {
|
|
880
867
|
const startTime = (0, dayjs.default)().format("YYYY-MM-DD_HH-mm-ss");
|
|
881
868
|
return node_path.default.join(BOT_CWD.value, `logs/${uin}_${platformName}_${startTime}.log`);
|
|
882
869
|
}
|
|
883
870
|
|
|
871
|
+
//#endregion
|
|
872
|
+
//#region src/logger.ts
|
|
873
|
+
const LEVEL_MAP = {
|
|
874
|
+
0: "FATAL",
|
|
875
|
+
1: "ERROR",
|
|
876
|
+
2: "WARN",
|
|
877
|
+
3: "INFO",
|
|
878
|
+
4: "DEBUG",
|
|
879
|
+
5: "TRACE"
|
|
880
|
+
};
|
|
881
|
+
/**
|
|
882
|
+
* 获取日志文件名
|
|
883
|
+
*/
|
|
884
|
+
function getLogFilePath(type = "") {
|
|
885
|
+
const startTime = (0, dayjs.default)().format("YYYY-MM-DD_HH-mm-ss");
|
|
886
|
+
return node_path.default.join(BOT_CWD.value, `logs/${startTime}${type ? "." + type : ""}.log`);
|
|
887
|
+
}
|
|
888
|
+
const getMiokiLogger = (level) => {
|
|
889
|
+
const logDir = node_path.default.join(BOT_CWD.value, "logs");
|
|
890
|
+
if (!node_fs.default.existsSync(logDir)) node_fs.default.mkdirSync(logDir, { recursive: true });
|
|
891
|
+
const logFile = getLogFilePath();
|
|
892
|
+
return (0, consola_core.createConsola)({
|
|
893
|
+
level: consola_core.LogLevels[level],
|
|
894
|
+
defaults: { tag: "mioki" },
|
|
895
|
+
reporters: [{ log: (logObj) => {
|
|
896
|
+
const message = (0, consola_utils.stripAnsi)(logObj.message || logObj.args?.join(" ") || "");
|
|
897
|
+
const line = `${`[${logObj.date.toISOString()}] [${LEVEL_MAP[logObj.level]}] ${logObj.tag ? `[${logObj.tag}] ` : ""}`}${message}`;
|
|
898
|
+
node_fs.default.appendFileSync(logFile, line + "\n");
|
|
899
|
+
} }, { log: (logObj) => {
|
|
900
|
+
const message = logObj.message || logObj.args?.join(" ") || "";
|
|
901
|
+
const line = `${consola_utils.colors.gray(`[${logObj.date.toLocaleTimeString("zh-CN")}]`) + " " + consola_utils.colors.bold(consola_utils.colors.blue(LEVEL_MAP[logObj.level])) + " " + (logObj.tag ? consola_utils.colors.bold(consola_utils.colors.green(`[${logObj.tag}] `)) : "")}${message}`;
|
|
902
|
+
if (logObj.level <= consola_core.LogLevels["info"]) console.log(line);
|
|
903
|
+
else if (logObj.level === consola_core.LogLevels["warn"]) console.warn(line);
|
|
904
|
+
else console.debug(line);
|
|
905
|
+
} }],
|
|
906
|
+
formatOptions: {
|
|
907
|
+
colors: true,
|
|
908
|
+
compact: true,
|
|
909
|
+
date: true
|
|
910
|
+
}
|
|
911
|
+
});
|
|
912
|
+
};
|
|
913
|
+
|
|
884
914
|
//#endregion
|
|
885
915
|
//#region package.json
|
|
886
|
-
var version = "0.
|
|
916
|
+
var version = "0.3.0";
|
|
887
917
|
|
|
888
918
|
//#endregion
|
|
889
919
|
//#region src/actions.ts
|
|
@@ -1191,6 +1221,10 @@ async function enablePlugin(bot, plugin, type = "external") {
|
|
|
1191
1221
|
const start$1 = node_process.hrtime.bigint();
|
|
1192
1222
|
const clears = /* @__PURE__ */ new Set();
|
|
1193
1223
|
const userClears = /* @__PURE__ */ new Set();
|
|
1224
|
+
const logger = bot.logger.withDefaults({
|
|
1225
|
+
tag: `plugin:${name}`,
|
|
1226
|
+
args: [name]
|
|
1227
|
+
});
|
|
1194
1228
|
const context = {
|
|
1195
1229
|
bot,
|
|
1196
1230
|
segment: bot.segment,
|
|
@@ -1198,22 +1232,35 @@ async function enablePlugin(bot, plugin, type = "external") {
|
|
|
1198
1232
|
...utils_exports,
|
|
1199
1233
|
...config_exports,
|
|
1200
1234
|
...buildRemovedActions(bot),
|
|
1235
|
+
logger,
|
|
1201
1236
|
services,
|
|
1202
1237
|
clears: userClears,
|
|
1203
1238
|
addService: (name$1, service, cover) => {
|
|
1204
|
-
|
|
1239
|
+
logger.debug(`Adding service: ${name$1} (cover: ${cover ? "yes" : "no"})`);
|
|
1240
|
+
const removeService = () => {
|
|
1241
|
+
logger.debug(`Removing service: ${name$1}`);
|
|
1242
|
+
addService(name$1, service, cover);
|
|
1243
|
+
};
|
|
1205
1244
|
clears.add(removeService);
|
|
1206
1245
|
return removeService;
|
|
1207
1246
|
},
|
|
1208
1247
|
handle: (eventName, handler) => {
|
|
1248
|
+
logger.debug(`Registering event handler for event: ${String(eventName)}`);
|
|
1209
1249
|
bot.on(eventName, handler);
|
|
1210
|
-
const unsubscribe = () =>
|
|
1250
|
+
const unsubscribe = () => {
|
|
1251
|
+
logger.debug(`Unregistering event handler for event: ${String(eventName)}`);
|
|
1252
|
+
bot.off(eventName, handler);
|
|
1253
|
+
};
|
|
1211
1254
|
clears.add(unsubscribe);
|
|
1212
1255
|
return unsubscribe;
|
|
1213
1256
|
},
|
|
1214
1257
|
cron: (cronExpression, handler) => {
|
|
1258
|
+
logger.debug(`Scheduling cron job: ${cronExpression}`);
|
|
1215
1259
|
const job = node_cron.default.schedule(cronExpression, (now) => handler(context, now));
|
|
1216
|
-
const clear = () =>
|
|
1260
|
+
const clear = () => {
|
|
1261
|
+
logger.debug(`Stopping cron job: ${cronExpression}`);
|
|
1262
|
+
job.stop();
|
|
1263
|
+
};
|
|
1217
1264
|
clears.add(clear);
|
|
1218
1265
|
return job;
|
|
1219
1266
|
}
|
|
@@ -1227,6 +1274,7 @@ async function enablePlugin(bot, plugin, type = "external") {
|
|
|
1227
1274
|
plugin,
|
|
1228
1275
|
disable: async () => {
|
|
1229
1276
|
try {
|
|
1277
|
+
logger.debug(`Disabling plugin [${typeDesc}]${name}@${version$1}`);
|
|
1230
1278
|
await Promise.all([...clears, ...userClears].map((fn) => fn?.()));
|
|
1231
1279
|
runtimePlugins.delete(name);
|
|
1232
1280
|
} catch (err) {
|
|
@@ -1354,9 +1402,9 @@ async function getMiokiStatusStr(client) {
|
|
|
1354
1402
|
${diskValid ? `💾 ${diskDesc}` : ""}
|
|
1355
1403
|
`.trim();
|
|
1356
1404
|
}
|
|
1357
|
-
async function getDiskUsageInUnix(path$
|
|
1405
|
+
async function getDiskUsageInUnix(path$7 = "/") {
|
|
1358
1406
|
return new Promise((resolve) => {
|
|
1359
|
-
node_child_process.default.exec(`df -k ${path$
|
|
1407
|
+
node_child_process.default.exec(`df -k ${path$7} | tail -1 | awk '{print $2,$4}'`, (err, stdout) => {
|
|
1360
1408
|
if (err) {
|
|
1361
1409
|
console.error(err);
|
|
1362
1410
|
return resolve({
|
|
@@ -1680,9 +1728,8 @@ async function start(options = {}) {
|
|
|
1680
1728
|
...botConfig.napcat,
|
|
1681
1729
|
logger
|
|
1682
1730
|
});
|
|
1683
|
-
napcat.on("napcat.connected", async ({
|
|
1684
|
-
logger.info(`>>> 已连接到 NapCat
|
|
1685
|
-
logger.info(`>>> 当前登录 QQ 账号: ${uin}`);
|
|
1731
|
+
napcat.on("napcat.connected", async ({ user_id, nickname }) => {
|
|
1732
|
+
logger.info(`>>> 已连接到 NapCat: ${nickname} (${user_id})`);
|
|
1686
1733
|
let lastNoticeTime = 0;
|
|
1687
1734
|
process.on("uncaughtException", async (err) => {
|
|
1688
1735
|
const msg = stringifyError(err);
|
|
@@ -1826,7 +1873,6 @@ exports.getBfaceUrl = getBfaceUrl;
|
|
|
1826
1873
|
exports.getGroupAvatarLink = getGroupAvatarLink;
|
|
1827
1874
|
exports.getImage = getImage;
|
|
1828
1875
|
exports.getImageUrl = getImageUrl;
|
|
1829
|
-
exports.getLogFilePath = getLogFilePath;
|
|
1830
1876
|
exports.getMentionedImage = getMentionedImage;
|
|
1831
1877
|
exports.getMentionedImageUrl = getMentionedImageUrl;
|
|
1832
1878
|
exports.getMentionedUserId = getMentionedUserId;
|