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.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,57 @@ 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: "FATAL",
|
|
825
|
+
1: "ERROR",
|
|
826
|
+
2: "WARN",
|
|
827
|
+
3: "INFO",
|
|
828
|
+
4: "DEBUG",
|
|
829
|
+
5: "TRACE"
|
|
830
|
+
};
|
|
831
|
+
/**
|
|
832
|
+
* 获取日志文件名
|
|
833
|
+
*/
|
|
834
|
+
function getLogFilePath(type = "") {
|
|
835
|
+
const startTime = dayjs().format("YYYY-MM-DD_HH-mm-ss");
|
|
836
|
+
return path$1.join(BOT_CWD.value, `logs/${startTime}${type ? "." + type : ""}.log`);
|
|
837
|
+
}
|
|
838
|
+
const getMiokiLogger = (level) => {
|
|
839
|
+
const logDir = path$1.join(BOT_CWD.value, "logs");
|
|
840
|
+
if (!fs$1.existsSync(logDir)) fs$1.mkdirSync(logDir, { recursive: true });
|
|
841
|
+
const logFile = getLogFilePath();
|
|
842
|
+
return createConsola({
|
|
843
|
+
level: LogLevels[level],
|
|
844
|
+
defaults: { tag: "mioki" },
|
|
845
|
+
reporters: [{ log: (logObj) => {
|
|
846
|
+
const message = stripAnsi(logObj.message || logObj.args?.join(" ") || "");
|
|
847
|
+
const line = `${`[${logObj.date.toISOString()}] [${LEVEL_MAP[logObj.level]}] ${logObj.tag ? `[${logObj.tag}] ` : ""}`}${message}`;
|
|
848
|
+
fs$1.appendFileSync(logFile, line + "\n");
|
|
849
|
+
} }, { log: (logObj) => {
|
|
850
|
+
const message = logObj.message || logObj.args?.join(" ") || "";
|
|
851
|
+
const line = `${colors.gray(`[${logObj.date.toLocaleTimeString("zh-CN")}]`) + " " + colors.bold(colors.blue(LEVEL_MAP[logObj.level])) + " " + (logObj.tag ? colors.bold(colors.green(`[${logObj.tag}] `)) : "")}${message}`;
|
|
852
|
+
if (logObj.level <= LogLevels["info"]) console.log(line);
|
|
853
|
+
else if (logObj.level === LogLevels["warn"]) console.warn(line);
|
|
854
|
+
else console.debug(line);
|
|
855
|
+
} }],
|
|
856
|
+
formatOptions: {
|
|
857
|
+
colors: true,
|
|
858
|
+
compact: true,
|
|
859
|
+
date: true
|
|
860
|
+
}
|
|
861
|
+
});
|
|
862
|
+
};
|
|
863
|
+
|
|
833
864
|
//#endregion
|
|
834
865
|
//#region package.json
|
|
835
|
-
var version = "0.
|
|
866
|
+
var version = "0.3.0";
|
|
836
867
|
|
|
837
868
|
//#endregion
|
|
838
869
|
//#region src/actions.ts
|
|
@@ -1140,6 +1171,10 @@ async function enablePlugin(bot, plugin, type = "external") {
|
|
|
1140
1171
|
const start$1 = hrtime.bigint();
|
|
1141
1172
|
const clears = /* @__PURE__ */ new Set();
|
|
1142
1173
|
const userClears = /* @__PURE__ */ new Set();
|
|
1174
|
+
const logger = bot.logger.withDefaults({
|
|
1175
|
+
tag: `plugin:${name}`,
|
|
1176
|
+
args: [name]
|
|
1177
|
+
});
|
|
1143
1178
|
const context = {
|
|
1144
1179
|
bot,
|
|
1145
1180
|
segment: bot.segment,
|
|
@@ -1147,22 +1182,35 @@ async function enablePlugin(bot, plugin, type = "external") {
|
|
|
1147
1182
|
...utils_exports,
|
|
1148
1183
|
...config_exports,
|
|
1149
1184
|
...buildRemovedActions(bot),
|
|
1185
|
+
logger,
|
|
1150
1186
|
services,
|
|
1151
1187
|
clears: userClears,
|
|
1152
1188
|
addService: (name$1, service, cover) => {
|
|
1153
|
-
|
|
1189
|
+
logger.debug(`Adding service: ${name$1} (cover: ${cover ? "yes" : "no"})`);
|
|
1190
|
+
const removeService = () => {
|
|
1191
|
+
logger.debug(`Removing service: ${name$1}`);
|
|
1192
|
+
addService(name$1, service, cover);
|
|
1193
|
+
};
|
|
1154
1194
|
clears.add(removeService);
|
|
1155
1195
|
return removeService;
|
|
1156
1196
|
},
|
|
1157
1197
|
handle: (eventName, handler) => {
|
|
1198
|
+
logger.debug(`Registering event handler for event: ${String(eventName)}`);
|
|
1158
1199
|
bot.on(eventName, handler);
|
|
1159
|
-
const unsubscribe = () =>
|
|
1200
|
+
const unsubscribe = () => {
|
|
1201
|
+
logger.debug(`Unregistering event handler for event: ${String(eventName)}`);
|
|
1202
|
+
bot.off(eventName, handler);
|
|
1203
|
+
};
|
|
1160
1204
|
clears.add(unsubscribe);
|
|
1161
1205
|
return unsubscribe;
|
|
1162
1206
|
},
|
|
1163
1207
|
cron: (cronExpression, handler) => {
|
|
1208
|
+
logger.debug(`Scheduling cron job: ${cronExpression}`);
|
|
1164
1209
|
const job = nodeCron.schedule(cronExpression, (now) => handler(context, now));
|
|
1165
|
-
const clear = () =>
|
|
1210
|
+
const clear = () => {
|
|
1211
|
+
logger.debug(`Stopping cron job: ${cronExpression}`);
|
|
1212
|
+
job.stop();
|
|
1213
|
+
};
|
|
1166
1214
|
clears.add(clear);
|
|
1167
1215
|
return job;
|
|
1168
1216
|
}
|
|
@@ -1176,6 +1224,7 @@ async function enablePlugin(bot, plugin, type = "external") {
|
|
|
1176
1224
|
plugin,
|
|
1177
1225
|
disable: async () => {
|
|
1178
1226
|
try {
|
|
1227
|
+
logger.debug(`Disabling plugin [${typeDesc}]${name}@${version$1}`);
|
|
1179
1228
|
await Promise.all([...clears, ...userClears].map((fn) => fn?.()));
|
|
1180
1229
|
runtimePlugins.delete(name);
|
|
1181
1230
|
} catch (err) {
|
|
@@ -1629,9 +1678,8 @@ async function start(options = {}) {
|
|
|
1629
1678
|
...botConfig.napcat,
|
|
1630
1679
|
logger
|
|
1631
1680
|
});
|
|
1632
|
-
napcat.on("napcat.connected", async ({
|
|
1633
|
-
logger.info(`>>> 已连接到 NapCat
|
|
1634
|
-
logger.info(`>>> 当前登录 QQ 账号: ${uin}`);
|
|
1681
|
+
napcat.on("napcat.connected", async ({ user_id, nickname }) => {
|
|
1682
|
+
logger.info(`>>> 已连接到 NapCat: ${nickname} (${user_id})`);
|
|
1635
1683
|
let lastNoticeTime = 0;
|
|
1636
1684
|
process.on("uncaughtException", async (err) => {
|
|
1637
1685
|
const msg = stringifyError(err);
|
|
@@ -1723,5 +1771,5 @@ async function start(options = {}) {
|
|
|
1723
1771
|
}
|
|
1724
1772
|
|
|
1725
1773
|
//#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,
|
|
1774
|
+
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
1775
|
//# sourceMappingURL=index.mjs.map
|