mioki 0.2.5 → 0.4.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 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,75 @@ 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: {
875
+ name: "ERROR",
876
+ color: "red"
877
+ },
878
+ 1: {
879
+ name: "WARN",
880
+ color: "yellow"
881
+ },
882
+ 2: {
883
+ name: "LOG",
884
+ color: "white"
885
+ },
886
+ 3: {
887
+ name: "INFO",
888
+ color: "green"
889
+ },
890
+ 4: {
891
+ name: "DEBUG",
892
+ color: "blue"
893
+ },
894
+ 5: {
895
+ name: "TRACE",
896
+ color: "gray"
897
+ }
898
+ };
899
+ /**
900
+ * 获取日志文件名
901
+ */
902
+ function getLogFilePath(type = "") {
903
+ const startTime = (0, dayjs.default)().format("YYYY-MM-DD_HH-mm-ss");
904
+ return node_path.default.join(BOT_CWD.value, `logs/${startTime}${type ? "." + type : ""}.log`);
905
+ }
906
+ const getMiokiLogger = (level) => {
907
+ const logDir = node_path.default.join(BOT_CWD.value, "logs");
908
+ if (!node_fs.default.existsSync(logDir)) node_fs.default.mkdirSync(logDir, { recursive: true });
909
+ const logFile = getLogFilePath();
910
+ return (0, consola_core.createConsola)({
911
+ level: consola_core.LogLevels[level],
912
+ defaults: { tag: "mioki" },
913
+ reporters: [{ log: (logObj) => {
914
+ const message = (0, consola_utils.stripAnsi)(logObj.message || logObj.args?.join(" ") || "");
915
+ const line = `${`[${logObj.date.toISOString()}] [${LEVEL_MAP[logObj.level].name}] ${logObj.tag ? `[${logObj.tag}] ` : ""}`}${message}`;
916
+ node_fs.default.appendFileSync(logFile, line + "\n");
917
+ } }, { log: (logObj) => {
918
+ const message = logObj.message || logObj.args?.join(" ") || "";
919
+ const line = `${consola_utils.colors.gray(`[${logObj.date.toLocaleTimeString("zh-CN")}]`) + " " + consola_utils.colors.bold(consola_utils.colors[LEVEL_MAP[logObj.level].color](LEVEL_MAP[logObj.level].name)) + " " + (logObj.tag ? consola_utils.colors.bold(consola_utils.colors.dim(`[${logObj.tag}] `)) : "")}${message}`;
920
+ if (logObj.level <= consola_core.LogLevels["info"]) console.log(line);
921
+ else if (logObj.level === consola_core.LogLevels["warn"]) console.warn(line);
922
+ else console.debug(line);
923
+ } }],
924
+ formatOptions: {
925
+ colors: true,
926
+ compact: true,
927
+ date: true
928
+ }
929
+ });
930
+ };
931
+
884
932
  //#endregion
885
933
  //#region package.json
886
- var version = "0.2.5";
934
+ var version = "0.4.0";
887
935
 
888
936
  //#endregion
889
937
  //#region src/actions.ts
@@ -1191,6 +1239,10 @@ async function enablePlugin(bot, plugin, type = "external") {
1191
1239
  const start$1 = node_process.hrtime.bigint();
1192
1240
  const clears = /* @__PURE__ */ new Set();
1193
1241
  const userClears = /* @__PURE__ */ new Set();
1242
+ const logger = bot.logger.withDefaults({
1243
+ tag: `plugin:${name}`,
1244
+ args: [name]
1245
+ });
1194
1246
  const context = {
1195
1247
  bot,
1196
1248
  segment: bot.segment,
@@ -1198,22 +1250,35 @@ async function enablePlugin(bot, plugin, type = "external") {
1198
1250
  ...utils_exports,
1199
1251
  ...config_exports,
1200
1252
  ...buildRemovedActions(bot),
1253
+ logger,
1201
1254
  services,
1202
1255
  clears: userClears,
1203
1256
  addService: (name$1, service, cover) => {
1204
- const removeService = addService(name$1, service, cover);
1257
+ logger.debug(`Adding service: ${name$1} (cover: ${cover ? "yes" : "no"})`);
1258
+ const removeService = () => {
1259
+ logger.debug(`Removing service: ${name$1}`);
1260
+ addService(name$1, service, cover);
1261
+ };
1205
1262
  clears.add(removeService);
1206
1263
  return removeService;
1207
1264
  },
1208
1265
  handle: (eventName, handler) => {
1266
+ logger.debug(`Registering event handler for event: ${String(eventName)}`);
1209
1267
  bot.on(eventName, handler);
1210
- const unsubscribe = () => bot.off(eventName, handler);
1268
+ const unsubscribe = () => {
1269
+ logger.debug(`Unregistering event handler for event: ${String(eventName)}`);
1270
+ bot.off(eventName, handler);
1271
+ };
1211
1272
  clears.add(unsubscribe);
1212
1273
  return unsubscribe;
1213
1274
  },
1214
1275
  cron: (cronExpression, handler) => {
1276
+ logger.debug(`Scheduling cron job: ${cronExpression}`);
1215
1277
  const job = node_cron.default.schedule(cronExpression, (now) => handler(context, now));
1216
- const clear = () => job.stop();
1278
+ const clear = () => {
1279
+ logger.debug(`Stopping cron job: ${cronExpression}`);
1280
+ job.stop();
1281
+ };
1217
1282
  clears.add(clear);
1218
1283
  return job;
1219
1284
  }
@@ -1227,6 +1292,7 @@ async function enablePlugin(bot, plugin, type = "external") {
1227
1292
  plugin,
1228
1293
  disable: async () => {
1229
1294
  try {
1295
+ logger.debug(`Disabling plugin [${typeDesc}]${name}@${version$1}`);
1230
1296
  await Promise.all([...clears, ...userClears].map((fn) => fn?.()));
1231
1297
  runtimePlugins.delete(name);
1232
1298
  } catch (err) {
@@ -1354,9 +1420,9 @@ async function getMiokiStatusStr(client) {
1354
1420
  ${diskValid ? `💾 ${diskDesc}` : ""}
1355
1421
  `.trim();
1356
1422
  }
1357
- async function getDiskUsageInUnix(path$6 = "/") {
1423
+ async function getDiskUsageInUnix(path$7 = "/") {
1358
1424
  return new Promise((resolve) => {
1359
- node_child_process.default.exec(`df -k ${path$6} | tail -1 | awk '{print $2,$4}'`, (err, stdout) => {
1425
+ node_child_process.default.exec(`df -k ${path$7} | tail -1 | awk '{print $2,$4}'`, (err, stdout) => {
1360
1426
  if (err) {
1361
1427
  console.error(err);
1362
1428
  return resolve({
@@ -1673,16 +1739,19 @@ async function start(options = {}) {
1673
1739
  process.title = `mioki v${version}`;
1674
1740
  const logger = getMiokiLogger(botConfig.log_level || "info");
1675
1741
  const plugin_dir = getAbsPluginDir();
1676
- logger.info(`>>> mioki v${version} 启动中`);
1677
- logger.info(`>>> 工作目录: ${BOT_CWD.value}`);
1678
- logger.info(`>>> 插件目录: ${plugin_dir}`);
1742
+ logger.info(`>>> 〓 ${consola_utils.colors.bold(consola_utils.colors.cyan("mioki"))} ${consola_utils.colors.bold(consola_utils.colors.green(`v${version}`))} 〓`);
1743
+ logger.info(`>>> ${consola_utils.colors.yellow(consola_utils.colors.underline(`基于 NapCat 的 TypeScript 🤖️ 机器人框架。`))}`);
1744
+ logger.info(`>>> ${consola_utils.colors.yellow(consola_utils.colors.underline(`作者: Viki <hi@viki.moe> (https://github.com/vikiboss)`))}`);
1745
+ logger.info(`>>> ${consola_utils.colors.yellow(consola_utils.colors.underline(`协议: Licensed under MIT License.`))}`);
1746
+ logger.info(`>>> ${consola_utils.colors.cyan(consola_utils.colors.underline(`GitHub: http://github.com/vikiboss/mioki`))}`);
1747
+ logger.info(`>>> 工作目录: ${consola_utils.colors.bold(consola_utils.colors.blue(BOT_CWD.value))}`);
1748
+ logger.info(`>>> 插件目录: ${consola_utils.colors.bold(consola_utils.colors.blue(plugin_dir))}`);
1679
1749
  const napcat = new napcat_sdk.NapCat({
1680
1750
  ...botConfig.napcat,
1681
1751
  logger
1682
1752
  });
1683
- napcat.on("napcat.connected", async ({ uin }) => {
1684
- logger.info(`>>> 已连接到 NapCat 服务器`);
1685
- logger.info(`>>> 当前登录 QQ 账号: ${uin}`);
1753
+ napcat.on("napcat.connected", async ({ user_id, nickname }) => {
1754
+ logger.info(`>>> 已连接到 NapCat: ${consola_utils.colors.bold(consola_utils.colors.green(nickname))} (${consola_utils.colors.bold(consola_utils.colors.green(user_id))})`);
1686
1755
  let lastNoticeTime = 0;
1687
1756
  process.on("uncaughtException", async (err) => {
1688
1757
  const msg = stringifyError(err);
@@ -1766,6 +1835,7 @@ async function start(options = {}) {
1766
1835
  const failedCount = failedImportPlugins.length + failedEnablePlugins.length;
1767
1836
  const failedInfo = failedCount > 0 ? `${failedCount} 个失败 (导入 ${failedImportPlugins.length},启用 ${failedImportPlugins.length})。` : "";
1768
1837
  napcat.logger.info(`>>> 成功加载了 ${runtimePlugins.size} 个插件。${failedInfo ? failedInfo : ""}总耗时 ${costTime} ms`);
1838
+ napcat.logger.info(`>>> mioki 启动完成!祝您使用愉快!🎉️`);
1769
1839
  if (botConfig.online_push) await noticeMainOwner(napcat, `✅ mioki v${version} 已就绪`).catch((err) => {
1770
1840
  napcat.logger.error(`>>> 发送就绪通知失败: ${stringifyError(err)}`);
1771
1841
  });
@@ -1826,7 +1896,6 @@ exports.getBfaceUrl = getBfaceUrl;
1826
1896
  exports.getGroupAvatarLink = getGroupAvatarLink;
1827
1897
  exports.getImage = getImage;
1828
1898
  exports.getImageUrl = getImageUrl;
1829
- exports.getLogFilePath = getLogFilePath;
1830
1899
  exports.getMentionedImage = getMentionedImage;
1831
1900
  exports.getMentionedImageUrl = getMentionedImageUrl;
1832
1901
  exports.getMentionedUserId = getMentionedUserId;