@karinjs/adapter-qqbot 2.1.0 → 3.0.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.
@@ -1,170 +0,0 @@
1
- import { common, karinPathBase, logger, requireFileSync, watch } from "node-karin";
2
- import path from "path";
3
- import { fileURLToPath } from "url";
4
- import fs from "node:fs";
5
-
6
- //#region src/root.ts
7
- /** 当前文件的绝对路径 */
8
- const filePath = fileURLToPath(import.meta.url);
9
- /** 插件包绝对路径 */
10
- const dirPath = path.resolve(filePath, "../..");
11
- /** 插件包的名称 */
12
- const basename = path.basename(dirPath);
13
-
14
- //#endregion
15
- //#region src/utils/logger.ts
16
- const PREFIX = "[QQ Official Bot]";
17
- const m = {
18
- trace: (...a) => logger.trace(PREFIX, ...a),
19
- debug: (...a) => logger.debug(PREFIX, ...a),
20
- info: (...a) => logger.info(PREFIX, ...a),
21
- warn: (...a) => logger.warn(PREFIX, ...a),
22
- error: (...a) => logger.error(PREFIX, ...a),
23
- fatal: (...a) => logger.fatal(PREFIX, ...a),
24
- mark: (...a) => logger.mark(PREFIX, ...a)
25
- };
26
- /**
27
- * QQBot 日志函数
28
- * - log('info', '消息')
29
- * - log.info('消息')
30
- *
31
- * 日志级别由 karin 框架的 log.level 控制
32
- */
33
- function log(level, ...args) {
34
- m[level](...args);
35
- }
36
- Object.assign(log, m);
37
-
38
- //#endregion
39
- //#region src/utils/plugin.ts
40
- /**
41
- * 读取当前插件的 package.json。
42
- *
43
- * @returns package.json 内容。
44
- */
45
- const pkg = () => requireFileSync(`${dirPath}/package.json`);
46
- /** 当前插件 npm 包名,如 @karinjs/adapter-qqbot。 */
47
- const pluginName = pkg().name;
48
- /** 文件系统目录名:把 npm 包名里的 `/` 规范化为 `-`,与 Karin 框架对齐。 */
49
- const pluginDirName = pluginName.replace(/\//g, "-");
50
-
51
- //#endregion
52
- //#region src/utils/config.ts
53
- let cache;
54
- const cacheMap = {};
55
- /** 延迟注入,避免与 core/index.ts 循环依赖 */
56
- let _createBot;
57
- let _destroyBot;
58
- const bindHandlers = (createBot, destroyBot) => {
59
- _createBot = createBot;
60
- _destroyBot = destroyBot;
61
- };
62
- /** 配置目录:${karinPathBase}/@karinjs-adapter-qqbot/config */
63
- const dirConfig = `${karinPathBase}/${pluginDirName}/config`;
64
- /**
65
- * 读取配置
66
- */
67
- const config = () => {
68
- const cfgPath = `${dirConfig}/config.json`;
69
- if (!fs.existsSync(cfgPath)) {
70
- fs.mkdirSync(dirConfig, { recursive: true });
71
- fs.writeFileSync(cfgPath, JSON.stringify([], null, 2));
72
- }
73
- if (cache) return cache;
74
- const result = formatConfig(requireFileSync(cfgPath));
75
- cache = result;
76
- result.forEach((v) => {
77
- cacheMap[v.appId] = v;
78
- });
79
- return result;
80
- };
81
- /**
82
- * 获取单 bot 配置
83
- */
84
- const getConfig = (appid) => cacheMap[appid];
85
- /**
86
- * 写入配置
87
- */
88
- const writeConfig = (data) => {
89
- fs.mkdirSync(dirConfig, { recursive: true });
90
- fs.writeFileSync(`${dirConfig}/config.json`, JSON.stringify(data, null, 2));
91
- };
92
- /**
93
- * 用默认值补齐用户配置
94
- */
95
- const formatConfig = (user) => {
96
- const def = getDefaultConfig()[0];
97
- return user.map((item) => ({
98
- ...def,
99
- ...item,
100
- keyboard: {
101
- ...def.keyboard,
102
- ...item.keyboard
103
- },
104
- messageCache: {
105
- ...def.messageCache,
106
- ...item.messageCache
107
- },
108
- event: {
109
- ...def.event,
110
- ...item.event
111
- }
112
- }));
113
- };
114
- /**
115
- * 默认配置
116
- */
117
- const getDefaultConfig = () => [{
118
- name: "",
119
- appId: "",
120
- secret: "",
121
- prodApi: "https://api.sgroup.qq.com",
122
- sandboxApi: "https://sandbox.api.sgroup.qq.com",
123
- tokenApi: "https://bots.qq.com/app/getAppAccessToken",
124
- sandbox: false,
125
- qqEnable: true,
126
- guildEnable: true,
127
- guildMode: 0,
128
- regex: [{
129
- reg: "^/",
130
- rep: "#"
131
- }],
132
- keyboard: { enable: true },
133
- messageCache: {
134
- enable: false,
135
- self: false
136
- },
137
- event: { type: 2 }
138
- }];
139
- /**
140
- * 监听配置文件,比较前后差异决定重建 / 销毁 bot
141
- */
142
- setTimeout(() => {
143
- watch(`${dirConfig}/config.json`, (old, now) => {
144
- cache = formatConfig(now);
145
- cache.forEach((v) => {
146
- cacheMap[v.appId] = v;
147
- });
148
- const diff = common.diffArray(old, now);
149
- diff.removed.forEach((v) => _destroyBot?.(v.appId));
150
- if (diff.added.length > 0) {
151
- const ids = new Set(diff.added.map((v) => v.appId));
152
- cache.forEach((v) => {
153
- if (!ids.has(v.appId)) return;
154
- if (v.event.type === 0) return;
155
- _createBot?.(v);
156
- });
157
- }
158
- diff.common.forEach((curr) => {
159
- const prev = old.find((c) => c.appId === curr.appId);
160
- if (!prev) return;
161
- if (JSON.stringify(prev) === JSON.stringify(curr)) return;
162
- logger.info(`[QQ Official Bot][配置监听] 配置已变更: ${curr.appId},重新初始化`);
163
- _destroyBot?.(prev.appId);
164
- if (curr.event.type !== 0) _createBot?.(curr);
165
- });
166
- });
167
- }, 2e3);
168
-
169
- //#endregion
170
- export { writeConfig as a, log as c, getDefaultConfig as i, basename as l, config as n, pkg as o, getConfig as r, pluginDirName as s, bindHandlers as t };