mioki 0.3.0 → 0.4.1
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 +46 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +46 -22
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -821,12 +821,30 @@ function getLogFilePath$1(uin, platformName) {
|
|
|
821
821
|
//#endregion
|
|
822
822
|
//#region src/logger.ts
|
|
823
823
|
const LEVEL_MAP = {
|
|
824
|
-
0:
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
824
|
+
0: {
|
|
825
|
+
name: "ERROR",
|
|
826
|
+
color: "red"
|
|
827
|
+
},
|
|
828
|
+
1: {
|
|
829
|
+
name: "WARN",
|
|
830
|
+
color: "yellow"
|
|
831
|
+
},
|
|
832
|
+
2: {
|
|
833
|
+
name: "LOG",
|
|
834
|
+
color: "white"
|
|
835
|
+
},
|
|
836
|
+
3: {
|
|
837
|
+
name: "INFO",
|
|
838
|
+
color: "green"
|
|
839
|
+
},
|
|
840
|
+
4: {
|
|
841
|
+
name: "DEBUG",
|
|
842
|
+
color: "blue"
|
|
843
|
+
},
|
|
844
|
+
5: {
|
|
845
|
+
name: "TRACE",
|
|
846
|
+
color: "gray"
|
|
847
|
+
}
|
|
830
848
|
};
|
|
831
849
|
/**
|
|
832
850
|
* 获取日志文件名
|
|
@@ -844,11 +862,11 @@ const getMiokiLogger = (level) => {
|
|
|
844
862
|
defaults: { tag: "mioki" },
|
|
845
863
|
reporters: [{ log: (logObj) => {
|
|
846
864
|
const message = stripAnsi(logObj.message || logObj.args?.join(" ") || "");
|
|
847
|
-
const line = `${`[${logObj.date.toISOString()}] [${LEVEL_MAP[logObj.level]}] ${logObj.tag ? `[${logObj.tag}] ` : ""}`}${message}`;
|
|
865
|
+
const line = `${`[${logObj.date.toISOString()}] [${LEVEL_MAP[logObj.level].name}] ${logObj.tag ? `[${logObj.tag}] ` : ""}`}${message}`;
|
|
848
866
|
fs$1.appendFileSync(logFile, line + "\n");
|
|
849
867
|
} }, { log: (logObj) => {
|
|
850
868
|
const message = logObj.message || logObj.args?.join(" ") || "";
|
|
851
|
-
const line = `${colors.gray(`[${logObj.date.toLocaleTimeString("zh-CN")}]`) + " " + colors.bold(colors.
|
|
869
|
+
const line = `${colors.gray(`[${logObj.date.toLocaleTimeString("zh-CN")}]`) + " " + colors.bold(colors[LEVEL_MAP[logObj.level].color](LEVEL_MAP[logObj.level].name)) + " " + (logObj.tag ? colors.dim(`[${logObj.tag}] `) : "")}${message}`;
|
|
852
870
|
if (logObj.level <= LogLevels["info"]) console.log(line);
|
|
853
871
|
else if (logObj.level === LogLevels["warn"]) console.warn(line);
|
|
854
872
|
else console.debug(line);
|
|
@@ -863,7 +881,7 @@ const getMiokiLogger = (level) => {
|
|
|
863
881
|
|
|
864
882
|
//#endregion
|
|
865
883
|
//#region package.json
|
|
866
|
-
var version = "0.
|
|
884
|
+
var version = "0.4.1";
|
|
867
885
|
|
|
868
886
|
//#endregion
|
|
869
887
|
//#region src/actions.ts
|
|
@@ -1228,15 +1246,15 @@ async function enablePlugin(bot, plugin, type = "external") {
|
|
|
1228
1246
|
await Promise.all([...clears, ...userClears].map((fn) => fn?.()));
|
|
1229
1247
|
runtimePlugins.delete(name);
|
|
1230
1248
|
} catch (err) {
|
|
1231
|
-
throw new Error(`>>> 禁用插件 [${typeDesc}]${name}@${version$1} 失败: ${err?.message}`);
|
|
1249
|
+
throw new Error(`>>> 禁用插件 [${colors.bold(colors.yellow(typeDesc))}]${colors.bold(colors.yellow(name))}@${colors.bold(colors.yellow(version$1))} 失败: ${err?.message}`);
|
|
1232
1250
|
}
|
|
1233
1251
|
}
|
|
1234
1252
|
});
|
|
1235
1253
|
const end = hrtime.bigint();
|
|
1236
1254
|
const time = Math.round(Number(end - start$1)) / 1e6;
|
|
1237
|
-
bot.logger.info(`>>> 启用插件 [${typeDesc}]${name}@${version$1} => 耗时 ${time} ms`);
|
|
1255
|
+
bot.logger.info(`>>> 启用插件 ${colors.bold(colors.yellow(`[${typeDesc}]`))}${colors.bold(colors.yellow(`${name}@${version$1}`))} => 耗时 ${colors.bold(colors.green(time.toFixed(2)))} ms`);
|
|
1238
1256
|
} catch (e) {
|
|
1239
|
-
throw new Error(`>>> 启用插件 [${typeDesc}]${name}@${version$1} 失败: ${e?.message}`);
|
|
1257
|
+
throw new Error(`>>> 启用插件 ${colors.bold(colors.yellow(`[${typeDesc}]`))}${colors.bold(colors.yellow(`${name}@${version$1}`))} 失败: ${e?.message}`);
|
|
1240
1258
|
}
|
|
1241
1259
|
return plugin;
|
|
1242
1260
|
}
|
|
@@ -1671,15 +1689,20 @@ async function start(options = {}) {
|
|
|
1671
1689
|
process.title = `mioki v${version}`;
|
|
1672
1690
|
const logger = getMiokiLogger(botConfig.log_level || "info");
|
|
1673
1691
|
const plugin_dir = getAbsPluginDir();
|
|
1674
|
-
logger.info(`>>> mioki v${version}
|
|
1675
|
-
logger.info(`>>>
|
|
1676
|
-
logger.info(`>>>
|
|
1692
|
+
logger.info(`>>> -> ${colors.bold(colors.cyan("mioki"))} ${colors.bold(colors.green(`v${version}`))} <-`);
|
|
1693
|
+
logger.info(`>>> ${colors.yellow(colors.underline(`基于 NapCat 的 TypeScript 🤖️ 机器人框架。`))}`);
|
|
1694
|
+
logger.info(`>>> ${colors.italic(`作者: Viki <hi@viki.moe> (https://github.com/vikiboss)`)}`);
|
|
1695
|
+
logger.info(`>>> ${colors.italic(`协议: Licensed under MIT License.`)}`);
|
|
1696
|
+
logger.info(`>>> ${colors.cyan(`GitHub: https://github.com/vikiboss/mioki`)}`);
|
|
1697
|
+
logger.info(">>> ----------------------------------------");
|
|
1698
|
+
logger.info(`>>> 工作目录: ${colors.bold(colors.blue(BOT_CWD.value))}`);
|
|
1699
|
+
logger.info(`>>> 插件目录: ${colors.bold(colors.blue(plugin_dir))}`);
|
|
1677
1700
|
const napcat = new NapCat({
|
|
1678
1701
|
...botConfig.napcat,
|
|
1679
1702
|
logger
|
|
1680
1703
|
});
|
|
1681
1704
|
napcat.on("napcat.connected", async ({ user_id, nickname }) => {
|
|
1682
|
-
logger.info(`>>> 已连接到 NapCat: ${nickname}
|
|
1705
|
+
logger.info(`>>> 已连接到 NapCat: ${colors.bold(colors.green(nickname))}(${colors.bold(colors.green(user_id))})`);
|
|
1683
1706
|
let lastNoticeTime = 0;
|
|
1684
1707
|
process.on("uncaughtException", async (err) => {
|
|
1685
1708
|
const msg = stringifyError(err);
|
|
@@ -1706,7 +1729,7 @@ async function start(options = {}) {
|
|
|
1706
1729
|
absPath: path$1.resolve(plugin_dir, p)
|
|
1707
1730
|
})).filter((p) => {
|
|
1708
1731
|
if (!fs$1.existsSync(p.absPath)) {
|
|
1709
|
-
napcat.logger.warn(`>>> 插件 ${p.dirName} 不存在,已忽略`);
|
|
1732
|
+
napcat.logger.warn(`>>> 插件 ${colors.bold(colors.red(p.dirName))} 不存在,已忽略`);
|
|
1710
1733
|
return false;
|
|
1711
1734
|
}
|
|
1712
1735
|
return true;
|
|
@@ -1716,7 +1739,7 @@ async function start(options = {}) {
|
|
|
1716
1739
|
try {
|
|
1717
1740
|
const plugin = await jiti.import(absPath, { default: true });
|
|
1718
1741
|
if (plugin.name !== dirName) {
|
|
1719
|
-
const tip = `>>> 插件目录名 [${dirName}] 和插件声明的 name [${plugin.name}] 不一致,可能导致重载异常,请修改一致后重启。`;
|
|
1742
|
+
const tip = `>>> 插件目录名 [${colors.bold(colors.yellow(dirName))}] 和插件声明的 name [${colors.bold(colors.yellow(plugin.name))}] 不一致,可能导致重载异常,请修改一致后重启。`;
|
|
1720
1743
|
napcat.logger.warn(tip);
|
|
1721
1744
|
noticeMainOwner(napcat, tip);
|
|
1722
1745
|
}
|
|
@@ -1730,7 +1753,7 @@ async function start(options = {}) {
|
|
|
1730
1753
|
const start$1 = hrtime.bigint();
|
|
1731
1754
|
const sortedUserPlugins = (await Promise.all(promises)).filter(Boolean).toSorted((prev, next) => (prev.priority ?? 100) - (next.priority ?? 100));
|
|
1732
1755
|
if (failedImportPlugins.length) {
|
|
1733
|
-
const tip = `>>> ${failedImportPlugins.length} 个插件加载失败: \n\n${failedImportPlugins.map(([dirName, err]) => `${dirName}: ${err}`).join("\n\n")}`;
|
|
1756
|
+
const tip = `>>> ${colors.bold(colors.red(failedImportPlugins.length))} 个插件加载失败: \n\n${failedImportPlugins.map(([dirName, err]) => `${dirName}: ${err}`).join("\n\n")}`;
|
|
1734
1757
|
napcat.logger.warn(tip);
|
|
1735
1758
|
noticeMainOwner(napcat, tip);
|
|
1736
1759
|
}
|
|
@@ -1743,7 +1766,7 @@ async function start(options = {}) {
|
|
|
1743
1766
|
const sortedGroups = Array.from(pluginGroups.entries()).toSorted(([a], [b]) => a - b);
|
|
1744
1767
|
const failedEnablePlugins = [];
|
|
1745
1768
|
try {
|
|
1746
|
-
napcat.logger.info(`>>> 加载内置插件: ${BUILTIN_PLUGINS.map((p) => p.name).join(", ")}`);
|
|
1769
|
+
napcat.logger.info(`>>> 加载内置插件: ${BUILTIN_PLUGINS.map((p) => colors.bold(colors.cyan(p.name))).join(", ")}`);
|
|
1747
1770
|
await Promise.all(BUILTIN_PLUGINS.map((p) => enablePlugin(napcat, p, "builtin")));
|
|
1748
1771
|
for (const [_, plugins$1] of sortedGroups) await Promise.all(plugins$1.map(async (p) => {
|
|
1749
1772
|
try {
|
|
@@ -1761,8 +1784,9 @@ async function start(options = {}) {
|
|
|
1761
1784
|
const end = hrtime.bigint();
|
|
1762
1785
|
const costTime = Math.round(Number(end - start$1)) / 1e6;
|
|
1763
1786
|
const failedCount = failedImportPlugins.length + failedEnablePlugins.length;
|
|
1764
|
-
const failedInfo = failedCount > 0 ? `${failedCount} 个失败 (导入 ${failedImportPlugins.length},启用 ${
|
|
1765
|
-
napcat.logger.info(`>>> 成功加载了 ${runtimePlugins.size} 个插件。${failedInfo ? failedInfo : ""}总耗时 ${costTime} ms`);
|
|
1787
|
+
const failedInfo = failedCount > 0 ? `${colors.bold(colors.red(failedCount))} 个失败 (导入 ${colors.bold(colors.red(failedImportPlugins.length))},启用 ${colors.bold(colors.red(failedEnablePlugins.length))})。` : "";
|
|
1788
|
+
napcat.logger.info(`>>> 成功加载了 ${colors.bold(colors.green(runtimePlugins.size))} 个插件。${failedInfo ? failedInfo : ""}总耗时 ${colors.bold(colors.green(costTime.toFixed(2)))} ms`);
|
|
1789
|
+
napcat.logger.info(colors.bold(colors.green(`>>> mioki v${version} 启动完成!祝您使用愉快!🎉️`)));
|
|
1766
1790
|
if (botConfig.online_push) await noticeMainOwner(napcat, `✅ mioki v${version} 已就绪`).catch((err) => {
|
|
1767
1791
|
napcat.logger.error(`>>> 发送就绪通知失败: ${stringifyError(err)}`);
|
|
1768
1792
|
});
|