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 +100 -31
- 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 +99 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -4
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { LevelWithSilentOrString } from "pino";
|
|
2
1
|
import fs from "node:fs";
|
|
3
2
|
import path from "node:path";
|
|
4
3
|
import mri from "mri";
|
|
@@ -6,16 +5,17 @@ import { BinaryLike, BinaryToTextEncoding } from "node:crypto";
|
|
|
6
5
|
import { Low } from "lowdb";
|
|
7
6
|
import { Jiti } from "jiti";
|
|
8
7
|
import { string2argv } from "string2argv";
|
|
9
|
-
import { EventMap, GroupMessageEvent,
|
|
8
|
+
import { EventMap, GroupMessageEvent, LogLevel, MessageEvent, NapCat, PrivateMessageEvent, RecvElement, RecvImageElement, Sendable } from "napcat-sdk";
|
|
10
9
|
import prettyMs from "pretty-ms";
|
|
11
10
|
import { filesize } from "filesize";
|
|
12
11
|
import dayjs from "dayjs";
|
|
13
12
|
import dedent from "dedent";
|
|
14
13
|
import systemInfo from "systeminformation";
|
|
14
|
+
import { ConsolaInstance } from "consola/core";
|
|
15
15
|
import { ScheduledTask, TaskContext } from "node-cron";
|
|
16
16
|
|
|
17
17
|
//#region src/logger.d.ts
|
|
18
|
-
declare const getMiokiLogger: (level:
|
|
18
|
+
declare const getMiokiLogger: (level: LogLevel) => ConsolaInstance;
|
|
19
19
|
//#endregion
|
|
20
20
|
//#region src/start.d.ts
|
|
21
21
|
interface StartOptions {
|
|
@@ -365,7 +365,7 @@ interface MiokiConfig {
|
|
|
365
365
|
admins: number[];
|
|
366
366
|
plugins: string[];
|
|
367
367
|
online_push?: boolean;
|
|
368
|
-
log_level?:
|
|
368
|
+
log_level?: LogLevel;
|
|
369
369
|
plugins_dir?: string;
|
|
370
370
|
napcat: {
|
|
371
371
|
protocol?: "ws" | "wss";
|
|
@@ -609,6 +609,8 @@ interface MiokiContext extends Services, Configs, Utils, RemoveBotParam<Actions>
|
|
|
609
609
|
cron: (cronExpression: string, handler: (ctx: MiokiContext, task: TaskContext) => any) => ScheduledTask;
|
|
610
610
|
/** 待清理的函数集合,在插件卸载时会被调用 */
|
|
611
611
|
clears: Set<(() => any) | null | undefined>;
|
|
612
|
+
/** 日志器 */
|
|
613
|
+
logger: ConsolaInstance;
|
|
612
614
|
}
|
|
613
615
|
declare const runtimePlugins: Map<string, {
|
|
614
616
|
name: string;
|
|
@@ -652,5 +654,5 @@ declare function findLocalPlugins(): Promise<{
|
|
|
652
654
|
absPath: string;
|
|
653
655
|
}[]>;
|
|
654
656
|
//#endregion
|
|
655
|
-
export { AnyFunc, Arrayable, BOT_CWD, ChromeUA, CreateCmdOptions, Gettable, HasMessage, MiokiConfig, MiokiContext, MiokiPlugin, MiokiServices, Noop, OmitBotParamFromFunc, PureObject, START_TIME, StartOptions, addService, base64Decode, base64Encode, bindBot, botConfig, clamp, createCmd, createDB, createForwardMsg, createStore, dayjs, dedent, definePlugin, enablePlugin, ensureBuffer, ensurePluginDir, filesize, filter, find, findLocalPlugins, formatDuration, formatQQLevel, fs, getAbsPluginDir, getAuthCodeViaTicket, getBfaceUrl, getGroupAvatarLink, getImage, getImageUrl,
|
|
657
|
+
export { AnyFunc, Arrayable, BOT_CWD, ChromeUA, CreateCmdOptions, Gettable, HasMessage, MiokiConfig, MiokiContext, MiokiPlugin, MiokiServices, Noop, OmitBotParamFromFunc, PureObject, START_TIME, StartOptions, addService, base64Decode, base64Encode, bindBot, botConfig, clamp, createCmd, createDB, createForwardMsg, createStore, dayjs, dedent, definePlugin, enablePlugin, ensureBuffer, ensurePluginDir, filesize, filter, find, findLocalPlugins, formatDuration, formatQQLevel, fs, getAbsPluginDir, getAuthCodeViaTicket, getBfaceUrl, getGroupAvatarLink, getImage, getImageUrl, getMentionedImage, getMentionedImageUrl, getMentionedUserId, getMinicoTokenViaAuthCode, getMiokiLogger, getQQAvatarLink, getQuoteImage, getQuoteImageUrl, getQuoteMessage, getQuoteText, getTerminalInput, getViolationRecords, hasRight, isAdmin, isBoolean, isDefined, isFunction, isGroupMsg, isInPm2, isNumber, isObject, isOwner, isOwnerOrAdmin, isPrivateMsg, isString, jiti, localNum, localeDate, localeTime, match, md5, mri, noNullish, noticeAdmins, noticeFriends, noticeGroups, noticeMainOwner, noticeOwners, path, prettyMs, qs, queryDevToolsLoginStatus, randomId, randomInt, randomItem, randomItems, readMiokiConfig, readPackageJson, requestLoginViaDevTools, runWithErrorHandler, runWithReaction, runtimePlugins, services, signArk, start, string2argv, stringifyError, systemInfo, text, toArray, toMsgId, unique, updateBotCWD, updateBotConfig, uploadImageToCollection, uploadImageToGroupHomework, uploadImageToGroupNotice, uuid, wait, writePackageJson };
|
|
656
658
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { t as __export } from "./chunk-C6wwvPpM.mjs";
|
|
2
|
-
import pino from "pino";
|
|
3
2
|
import fs, { default as fs$1 } from "node:fs";
|
|
4
3
|
import path, { default as path$1 } from "node:path";
|
|
5
|
-
import { hrtime } from "node:process";
|
|
6
4
|
import mri, { default as mri$1 } from "mri";
|
|
7
5
|
import crypto from "node:crypto";
|
|
8
6
|
import { Low } from "lowdb";
|
|
@@ -16,23 +14,13 @@ import { filesize } from "filesize";
|
|
|
16
14
|
import dayjs from "dayjs";
|
|
17
15
|
import dedent, { default as dedent$1 } from "dedent";
|
|
18
16
|
import systemInfo from "systeminformation";
|
|
17
|
+
import { colors, stripAnsi } from "consola/utils";
|
|
18
|
+
import { LogLevels, createConsola } from "consola/core";
|
|
19
|
+
import { hrtime } from "node:process";
|
|
19
20
|
import os from "node:os";
|
|
20
21
|
import cp from "node:child_process";
|
|
21
22
|
import nodeCron from "node-cron";
|
|
22
23
|
|
|
23
|
-
//#region src/logger.ts
|
|
24
|
-
const getMiokiLogger = (level) => {
|
|
25
|
-
return pino({
|
|
26
|
-
level,
|
|
27
|
-
name: "mioki",
|
|
28
|
-
transport: {
|
|
29
|
-
target: "pino-pretty",
|
|
30
|
-
options: { colorize: true }
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
//#endregion
|
|
36
24
|
//#region src/utils.ts
|
|
37
25
|
var utils_exports = /* @__PURE__ */ __export({
|
|
38
26
|
ChromeUA: () => ChromeUA,
|
|
@@ -740,7 +728,7 @@ const START_TIME = /* @__PURE__ */ new Date();
|
|
|
740
728
|
var config_exports = /* @__PURE__ */ __export({
|
|
741
729
|
BOT_CWD: () => BOT_CWD,
|
|
742
730
|
botConfig: () => botConfig,
|
|
743
|
-
getLogFilePath: () => getLogFilePath,
|
|
731
|
+
getLogFilePath: () => getLogFilePath$1,
|
|
744
732
|
hasRight: () => hasRight,
|
|
745
733
|
isAdmin: () => isAdmin,
|
|
746
734
|
isInPm2: () => isInPm2,
|
|
@@ -825,14 +813,75 @@ const isInPm2 = Boolean("pm_id" in process.env || "PM2_USAGE" in process.env);
|
|
|
825
813
|
/**
|
|
826
814
|
* 获取日志文件名
|
|
827
815
|
*/
|
|
828
|
-
function getLogFilePath(uin, platformName) {
|
|
816
|
+
function getLogFilePath$1(uin, platformName) {
|
|
829
817
|
const startTime = dayjs().format("YYYY-MM-DD_HH-mm-ss");
|
|
830
818
|
return path$1.join(BOT_CWD.value, `logs/${uin}_${platformName}_${startTime}.log`);
|
|
831
819
|
}
|
|
832
820
|
|
|
821
|
+
//#endregion
|
|
822
|
+
//#region src/logger.ts
|
|
823
|
+
const LEVEL_MAP = {
|
|
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
|
+
}
|
|
848
|
+
};
|
|
849
|
+
/**
|
|
850
|
+
* 获取日志文件名
|
|
851
|
+
*/
|
|
852
|
+
function getLogFilePath(type = "") {
|
|
853
|
+
const startTime = dayjs().format("YYYY-MM-DD_HH-mm-ss");
|
|
854
|
+
return path$1.join(BOT_CWD.value, `logs/${startTime}${type ? "." + type : ""}.log`);
|
|
855
|
+
}
|
|
856
|
+
const getMiokiLogger = (level) => {
|
|
857
|
+
const logDir = path$1.join(BOT_CWD.value, "logs");
|
|
858
|
+
if (!fs$1.existsSync(logDir)) fs$1.mkdirSync(logDir, { recursive: true });
|
|
859
|
+
const logFile = getLogFilePath();
|
|
860
|
+
return createConsola({
|
|
861
|
+
level: LogLevels[level],
|
|
862
|
+
defaults: { tag: "mioki" },
|
|
863
|
+
reporters: [{ log: (logObj) => {
|
|
864
|
+
const message = stripAnsi(logObj.message || logObj.args?.join(" ") || "");
|
|
865
|
+
const line = `${`[${logObj.date.toISOString()}] [${LEVEL_MAP[logObj.level].name}] ${logObj.tag ? `[${logObj.tag}] ` : ""}`}${message}`;
|
|
866
|
+
fs$1.appendFileSync(logFile, line + "\n");
|
|
867
|
+
} }, { log: (logObj) => {
|
|
868
|
+
const message = logObj.message || logObj.args?.join(" ") || "";
|
|
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.bold(colors.dim(`[${logObj.tag}] `)) : "")}${message}`;
|
|
870
|
+
if (logObj.level <= LogLevels["info"]) console.log(line);
|
|
871
|
+
else if (logObj.level === LogLevels["warn"]) console.warn(line);
|
|
872
|
+
else console.debug(line);
|
|
873
|
+
} }],
|
|
874
|
+
formatOptions: {
|
|
875
|
+
colors: true,
|
|
876
|
+
compact: true,
|
|
877
|
+
date: true
|
|
878
|
+
}
|
|
879
|
+
});
|
|
880
|
+
};
|
|
881
|
+
|
|
833
882
|
//#endregion
|
|
834
883
|
//#region package.json
|
|
835
|
-
var version = "0.
|
|
884
|
+
var version = "0.4.0";
|
|
836
885
|
|
|
837
886
|
//#endregion
|
|
838
887
|
//#region src/actions.ts
|
|
@@ -1140,6 +1189,10 @@ async function enablePlugin(bot, plugin, type = "external") {
|
|
|
1140
1189
|
const start$1 = hrtime.bigint();
|
|
1141
1190
|
const clears = /* @__PURE__ */ new Set();
|
|
1142
1191
|
const userClears = /* @__PURE__ */ new Set();
|
|
1192
|
+
const logger = bot.logger.withDefaults({
|
|
1193
|
+
tag: `plugin:${name}`,
|
|
1194
|
+
args: [name]
|
|
1195
|
+
});
|
|
1143
1196
|
const context = {
|
|
1144
1197
|
bot,
|
|
1145
1198
|
segment: bot.segment,
|
|
@@ -1147,22 +1200,35 @@ async function enablePlugin(bot, plugin, type = "external") {
|
|
|
1147
1200
|
...utils_exports,
|
|
1148
1201
|
...config_exports,
|
|
1149
1202
|
...buildRemovedActions(bot),
|
|
1203
|
+
logger,
|
|
1150
1204
|
services,
|
|
1151
1205
|
clears: userClears,
|
|
1152
1206
|
addService: (name$1, service, cover) => {
|
|
1153
|
-
|
|
1207
|
+
logger.debug(`Adding service: ${name$1} (cover: ${cover ? "yes" : "no"})`);
|
|
1208
|
+
const removeService = () => {
|
|
1209
|
+
logger.debug(`Removing service: ${name$1}`);
|
|
1210
|
+
addService(name$1, service, cover);
|
|
1211
|
+
};
|
|
1154
1212
|
clears.add(removeService);
|
|
1155
1213
|
return removeService;
|
|
1156
1214
|
},
|
|
1157
1215
|
handle: (eventName, handler) => {
|
|
1216
|
+
logger.debug(`Registering event handler for event: ${String(eventName)}`);
|
|
1158
1217
|
bot.on(eventName, handler);
|
|
1159
|
-
const unsubscribe = () =>
|
|
1218
|
+
const unsubscribe = () => {
|
|
1219
|
+
logger.debug(`Unregistering event handler for event: ${String(eventName)}`);
|
|
1220
|
+
bot.off(eventName, handler);
|
|
1221
|
+
};
|
|
1160
1222
|
clears.add(unsubscribe);
|
|
1161
1223
|
return unsubscribe;
|
|
1162
1224
|
},
|
|
1163
1225
|
cron: (cronExpression, handler) => {
|
|
1226
|
+
logger.debug(`Scheduling cron job: ${cronExpression}`);
|
|
1164
1227
|
const job = nodeCron.schedule(cronExpression, (now) => handler(context, now));
|
|
1165
|
-
const clear = () =>
|
|
1228
|
+
const clear = () => {
|
|
1229
|
+
logger.debug(`Stopping cron job: ${cronExpression}`);
|
|
1230
|
+
job.stop();
|
|
1231
|
+
};
|
|
1166
1232
|
clears.add(clear);
|
|
1167
1233
|
return job;
|
|
1168
1234
|
}
|
|
@@ -1176,6 +1242,7 @@ async function enablePlugin(bot, plugin, type = "external") {
|
|
|
1176
1242
|
plugin,
|
|
1177
1243
|
disable: async () => {
|
|
1178
1244
|
try {
|
|
1245
|
+
logger.debug(`Disabling plugin [${typeDesc}]${name}@${version$1}`);
|
|
1179
1246
|
await Promise.all([...clears, ...userClears].map((fn) => fn?.()));
|
|
1180
1247
|
runtimePlugins.delete(name);
|
|
1181
1248
|
} catch (err) {
|
|
@@ -1622,16 +1689,19 @@ async function start(options = {}) {
|
|
|
1622
1689
|
process.title = `mioki v${version}`;
|
|
1623
1690
|
const logger = getMiokiLogger(botConfig.log_level || "info");
|
|
1624
1691
|
const plugin_dir = getAbsPluginDir();
|
|
1625
|
-
logger.info(`>>> mioki v${version}
|
|
1626
|
-
logger.info(`>>>
|
|
1627
|
-
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.yellow(colors.underline(`作者: Viki <hi@viki.moe> (https://github.com/vikiboss)`))}`);
|
|
1695
|
+
logger.info(`>>> ${colors.yellow(colors.underline(`协议: Licensed under MIT License.`))}`);
|
|
1696
|
+
logger.info(`>>> ${colors.cyan(colors.underline(`GitHub: http://github.com/vikiboss/mioki`))}`);
|
|
1697
|
+
logger.info(`>>> 工作目录: ${colors.bold(colors.blue(BOT_CWD.value))}`);
|
|
1698
|
+
logger.info(`>>> 插件目录: ${colors.bold(colors.blue(plugin_dir))}`);
|
|
1628
1699
|
const napcat = new NapCat({
|
|
1629
1700
|
...botConfig.napcat,
|
|
1630
1701
|
logger
|
|
1631
1702
|
});
|
|
1632
|
-
napcat.on("napcat.connected", async ({
|
|
1633
|
-
logger.info(`>>> 已连接到 NapCat
|
|
1634
|
-
logger.info(`>>> 当前登录 QQ 账号: ${uin}`);
|
|
1703
|
+
napcat.on("napcat.connected", async ({ user_id, nickname }) => {
|
|
1704
|
+
logger.info(`>>> 已连接到 NapCat: ${colors.bold(colors.green(nickname))} (${colors.bold(colors.green(user_id))})`);
|
|
1635
1705
|
let lastNoticeTime = 0;
|
|
1636
1706
|
process.on("uncaughtException", async (err) => {
|
|
1637
1707
|
const msg = stringifyError(err);
|
|
@@ -1715,6 +1785,7 @@ async function start(options = {}) {
|
|
|
1715
1785
|
const failedCount = failedImportPlugins.length + failedEnablePlugins.length;
|
|
1716
1786
|
const failedInfo = failedCount > 0 ? `${failedCount} 个失败 (导入 ${failedImportPlugins.length},启用 ${failedImportPlugins.length})。` : "";
|
|
1717
1787
|
napcat.logger.info(`>>> 成功加载了 ${runtimePlugins.size} 个插件。${failedInfo ? failedInfo : ""}总耗时 ${costTime} ms`);
|
|
1788
|
+
napcat.logger.info(`>>> mioki 启动完成!祝您使用愉快!🎉️`);
|
|
1718
1789
|
if (botConfig.online_push) await noticeMainOwner(napcat, `✅ mioki v${version} 已就绪`).catch((err) => {
|
|
1719
1790
|
napcat.logger.error(`>>> 发送就绪通知失败: ${stringifyError(err)}`);
|
|
1720
1791
|
});
|
|
@@ -1723,5 +1794,5 @@ async function start(options = {}) {
|
|
|
1723
1794
|
}
|
|
1724
1795
|
|
|
1725
1796
|
//#endregion
|
|
1726
|
-
export { BOT_CWD, ChromeUA, START_TIME, addService, base64Decode, base64Encode, bindBot, botConfig, clamp, createCmd, createDB, createForwardMsg, createStore, dayjs, dedent, definePlugin, enablePlugin, ensureBuffer, ensurePluginDir, filesize, filter, find, findLocalPlugins, formatDuration, formatQQLevel, fs, getAbsPluginDir, getAuthCodeViaTicket, getBfaceUrl, getGroupAvatarLink, getImage, getImageUrl,
|
|
1797
|
+
export { BOT_CWD, ChromeUA, START_TIME, addService, base64Decode, base64Encode, bindBot, botConfig, clamp, createCmd, createDB, createForwardMsg, createStore, dayjs, dedent, definePlugin, enablePlugin, ensureBuffer, ensurePluginDir, filesize, filter, find, findLocalPlugins, formatDuration, formatQQLevel, fs, getAbsPluginDir, getAuthCodeViaTicket, getBfaceUrl, getGroupAvatarLink, getImage, getImageUrl, getMentionedImage, getMentionedImageUrl, getMentionedUserId, getMinicoTokenViaAuthCode, getMiokiLogger, getQQAvatarLink, getQuoteImage, getQuoteImageUrl, getQuoteMessage, getQuoteText, getTerminalInput, getViolationRecords, hasRight, isAdmin, isBoolean, isDefined, isFunction, isGroupMsg, isInPm2, isNumber, isObject, isOwner, isOwnerOrAdmin, isPrivateMsg, isString, jiti, localNum, localeDate, localeTime, match, md5, mri, noNullish, noticeAdmins, noticeFriends, noticeGroups, noticeMainOwner, noticeOwners, path, prettyMs, qs, queryDevToolsLoginStatus, randomId, randomInt, randomItem, randomItems, readMiokiConfig, readPackageJson, requestLoginViaDevTools, runWithErrorHandler, runWithReaction, runtimePlugins, services, signArk, start, string2argv, stringifyError, systemInfo, text, toArray, toMsgId, unique, updateBotCWD, updateBotConfig, uploadImageToCollection, uploadImageToGroupHomework, uploadImageToGroupNotice, uuid, wait, writePackageJson };
|
|
1727
1798
|
//# sourceMappingURL=index.mjs.map
|