node-karin 0.10.0 → 0.10.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.
Files changed (129) hide show
  1. package/config/defSet/group.yaml +40 -2
  2. package/lib/adapter/onebot/11/index.js +1 -1
  3. package/lib/cli/init.js +1 -1
  4. package/lib/cli/karin.js +1 -1
  5. package/lib/core/index.d.ts +9 -9
  6. package/lib/core/index.js +9 -9
  7. package/lib/core/init/dir.js +7 -0
  8. package/lib/core/init/init.js +46 -0
  9. package/lib/core/{karin.d.ts → karin/karin.d.ts} +1 -1
  10. package/lib/core/karin/karin.js +194 -0
  11. package/lib/core/{listener.d.ts → listener/listener.d.ts} +1 -1
  12. package/lib/core/listener/listener.js +213 -0
  13. package/lib/core/{plugin.app.d.ts → plugin/app.d.ts} +1 -1
  14. package/lib/core/plugin/app.js +19 -0
  15. package/lib/core/{plugin.d.ts → plugin/base.d.ts} +1 -1
  16. package/lib/core/plugin/base.js +140 -0
  17. package/lib/core/{plugin.loader.d.ts → plugin/loader.d.ts} +3 -3
  18. package/lib/core/plugin/loader.js +579 -0
  19. package/lib/core/process/process.js +100 -0
  20. package/lib/core/server/server.js +283 -0
  21. package/lib/db/index.d.ts +2 -2
  22. package/lib/db/index.js +2 -2
  23. package/lib/db/level/level.js +36 -0
  24. package/lib/db/redis/redis.js +135 -0
  25. package/lib/db/redis/redis_level.js +287 -0
  26. package/lib/event/{event.handler.d.ts → handler/base.d.ts} +2 -2
  27. package/lib/event/handler/base.js +173 -0
  28. package/lib/event/{message.handler.d.ts → handler/message.d.ts} +3 -3
  29. package/lib/event/handler/message.js +270 -0
  30. package/lib/event/{notice.handler.d.ts → handler/notice.d.ts} +3 -3
  31. package/lib/event/handler/notice.js +212 -0
  32. package/lib/event/{request.handler.d.ts → handler/request.d.ts} +3 -3
  33. package/lib/event/handler/request.js +118 -0
  34. package/lib/event/{review.handler.d.ts → handler/review.d.ts} +3 -3
  35. package/lib/event/handler/review.js +391 -0
  36. package/lib/event/index.d.ts +5 -5
  37. package/lib/event/index.js +5 -5
  38. package/lib/render/base.d.ts +1 -1
  39. package/lib/render/client.d.ts +1 -1
  40. package/lib/render/client.js +2 -12
  41. package/lib/render/client_even.d.ts +1 -1
  42. package/lib/render/client_even.js +2 -12
  43. package/lib/render/http.d.ts +1 -1
  44. package/lib/render/http.js +1 -1
  45. package/lib/render/server.js +1 -1
  46. package/lib/types/adapter/{adapter.d.ts → base.d.ts} +2 -2
  47. package/lib/types/config/config.js +1 -0
  48. package/lib/types/element/element.js +1 -0
  49. package/lib/types/event/message.d.ts +1 -1
  50. package/lib/types/event/reply.d.ts +1 -1
  51. package/lib/types/index.d.ts +6 -6
  52. package/lib/types/index.js +6 -6
  53. package/lib/types/logger/logger.js +1 -0
  54. package/lib/types/{plugin.d.ts → plugin/plugin.d.ts} +3 -3
  55. package/lib/types/plugin/plugin.js +1 -0
  56. package/lib/types/render/render.js +1 -0
  57. package/lib/utils/{common.d.ts → common/common.d.ts} +1 -1
  58. package/lib/utils/common/common.js +591 -0
  59. package/lib/utils/{config.d.ts → config/config.d.ts} +37 -19
  60. package/lib/utils/config/config.js +328 -0
  61. package/lib/utils/config/updateVersion.js +145 -0
  62. package/lib/utils/config/yamlEditor.js +292 -0
  63. package/lib/utils/{handler.d.ts → core/handler.d.ts} +1 -1
  64. package/lib/utils/core/handler.js +115 -0
  65. package/lib/utils/core/init.js +213 -0
  66. package/lib/utils/core/logger.js +105 -0
  67. package/lib/utils/{segment.d.ts → core/segment.d.ts} +1 -1
  68. package/lib/utils/core/segment.js +441 -0
  69. package/lib/utils/index.d.ts +11 -11
  70. package/lib/utils/index.js +11 -11
  71. package/lib/utils/{button.d.ts → tools/button.d.ts} +1 -1
  72. package/lib/utils/tools/button.js +38 -0
  73. package/lib/utils/tools/exec.js +37 -0
  74. package/lib/utils/tools/ffmpeg.js +25 -0
  75. package/lib/utils/tools/update.js +139 -0
  76. package/package.json +1 -1
  77. package/lib/core/dir.js +0 -7
  78. package/lib/core/init.js +0 -42
  79. package/lib/core/karin.js +0 -194
  80. package/lib/core/listener.js +0 -217
  81. package/lib/core/plugin.app.js +0 -19
  82. package/lib/core/plugin.js +0 -145
  83. package/lib/core/plugin.loader.js +0 -561
  84. package/lib/core/process.js +0 -98
  85. package/lib/core/server.js +0 -269
  86. package/lib/db/level.js +0 -37
  87. package/lib/db/redis.js +0 -134
  88. package/lib/db/redis_level.js +0 -293
  89. package/lib/event/event.handler.js +0 -167
  90. package/lib/event/message.handler.js +0 -254
  91. package/lib/event/notice.handler.js +0 -204
  92. package/lib/event/request.handler.js +0 -110
  93. package/lib/event/review.handler.js +0 -387
  94. package/lib/types/config.js +0 -1
  95. package/lib/types/element.js +0 -1
  96. package/lib/types/logger.js +0 -1
  97. package/lib/types/plugin.js +0 -1
  98. package/lib/types/render.js +0 -1
  99. package/lib/utils/button.js +0 -34
  100. package/lib/utils/common.js +0 -572
  101. package/lib/utils/config.js +0 -318
  102. package/lib/utils/exec.js +0 -36
  103. package/lib/utils/ffmpeg.js +0 -25
  104. package/lib/utils/handler.js +0 -109
  105. package/lib/utils/init.js +0 -208
  106. package/lib/utils/logger.js +0 -104
  107. package/lib/utils/segment.js +0 -470
  108. package/lib/utils/update.js +0 -135
  109. package/lib/utils/updateVersion.js +0 -145
  110. package/lib/utils/yamlEditor.js +0 -279
  111. /package/lib/core/{dir.d.ts → init/dir.d.ts} +0 -0
  112. /package/lib/core/{init.d.ts → init/init.d.ts} +0 -0
  113. /package/lib/core/{process.d.ts → process/process.d.ts} +0 -0
  114. /package/lib/core/{server.d.ts → server/server.d.ts} +0 -0
  115. /package/lib/db/{level.d.ts → level/level.d.ts} +0 -0
  116. /package/lib/db/{redis.d.ts → redis/redis.d.ts} +0 -0
  117. /package/lib/db/{redis_level.d.ts → redis/redis_level.d.ts} +0 -0
  118. /package/lib/types/adapter/{adapter.js → base.js} +0 -0
  119. /package/lib/types/{config.d.ts → config/config.d.ts} +0 -0
  120. /package/lib/types/{element.d.ts → element/element.d.ts} +0 -0
  121. /package/lib/types/{logger.d.ts → logger/logger.d.ts} +0 -0
  122. /package/lib/types/{render.d.ts → render/render.d.ts} +0 -0
  123. /package/lib/utils/{updateVersion.d.ts → config/updateVersion.d.ts} +0 -0
  124. /package/lib/utils/{yamlEditor.d.ts → config/yamlEditor.d.ts} +0 -0
  125. /package/lib/utils/{init.d.ts → core/init.d.ts} +0 -0
  126. /package/lib/utils/{logger.d.ts → core/logger.d.ts} +0 -0
  127. /package/lib/utils/{exec.d.ts → tools/exec.d.ts} +0 -0
  128. /package/lib/utils/{ffmpeg.d.ts → tools/ffmpeg.d.ts} +0 -0
  129. /package/lib/utils/{update.d.ts → tools/update.d.ts} +0 -0
@@ -1,4 +1,4 @@
1
- # 默认配置
1
+ # 全局默认配置
2
2
  default:
3
3
  # 群聊中所有消息冷却时间,单位秒,0则无限制
4
4
  GroupCD: 0
@@ -9,7 +9,45 @@ default:
9
9
 
10
10
  # 机器人前缀 设置后前缀+指令触发机器人
11
11
  alias:
12
- - karin
12
+ - k
13
+
14
+ # 白名单插件、功能,只有在白名单中的插件、功能才会响应
15
+ enable: []
16
+
17
+ # 黑名单插件、功能,黑名单中的插件、功能不会响应
18
+ disable: []
19
+
20
+ # 单个Bot默认配置
21
+ Bot.self_id:
22
+ # 群聊中所有消息冷却时间,单位秒,0则无限制
23
+ GroupCD: 0
24
+ # 群聊中 每个人的消息冷却时间,单位秒,0则无限制。注意,开启后所有消息都会进CD,无论是否触发插件。
25
+ GroupUserCD: 1
26
+ # 机器人响应模式,0-所有 1-仅@机器人 2-仅回应主人 3-仅回应前缀 4-前缀或@机器人 5-主人无前缀,群员前缀或@机器人
27
+ mode: 0
28
+
29
+ # 机器人前缀 设置后前缀+指令触发机器人
30
+ alias:
31
+ - k
32
+
33
+ # 白名单插件、功能,只有在白名单中的插件、功能才会响应
34
+ enable: []
35
+
36
+ # 黑名单插件、功能,黑名单中的插件、功能不会响应
37
+ disable: []
38
+
39
+ # 单个Bot单个群配置
40
+ Bot.self_id.group_id:
41
+ # 群聊中所有消息冷却时间,单位秒,0则无限制
42
+ GroupCD: 0
43
+ # 群聊中 每个人的消息冷却时间,单位秒,0则无限制。注意,开启后所有消息都会进CD,无论是否触发插件。
44
+ GroupUserCD: 1
45
+ # 机器人响应模式,0-所有 1-仅@机器人 2-仅回应主人 3-仅回应前缀 4-前缀或@机器人 5-主人无前缀,群员前缀或@机器人
46
+ mode: 0
47
+
48
+ # 机器人前缀 设置后前缀+指令触发机器人
49
+ alias:
50
+ - k
13
51
 
14
52
  # 白名单插件、功能,只有在白名单中的插件、功能才会响应
15
53
  enable: []
@@ -1,6 +1,6 @@
1
1
  import WebSocket from 'ws';
2
2
  import { randomUUID } from 'crypto';
3
- import { listener } from '../../../core/listener.js';
3
+ import { listener } from '../../../core/listener/listener.js';
4
4
  import { common, config, logger, segment } from '../../../utils/index.js';
5
5
  import { OB11Event } from './event.js';
6
6
  import { AdapterConvertKarin, KarinConvertAdapter } from './convert.js';
package/lib/cli/init.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { KarinInit } from '../utils/init.js'
2
+ import { KarinInit } from '../utils/core/init.js'
3
3
  /**
4
4
  * 休眠函数
5
5
  * @param ms 毫秒
package/lib/cli/karin.js CHANGED
@@ -6,7 +6,7 @@ import axios from 'axios'
6
6
  import { fileURLToPath } from 'url'
7
7
  import { program } from 'commander'
8
8
  import { exec as execCmd, spawn } from 'child_process'
9
- import { KarinInit } from '../utils/init.js'
9
+ import { KarinInit } from '../utils/core/init.js'
10
10
  class KarinCli {
11
11
  child
12
12
  filename
@@ -1,10 +1,10 @@
1
- export * from './dir.js';
2
- export * from './init.js';
3
- export * from './karin.js';
4
- export * from './listener.js';
5
- export * from './plugin.js';
6
- export * from './plugin.app.js';
7
- export * from './plugin.loader.js';
8
- export * from './process.js';
9
- export * from './server.js';
1
+ export * from './init/dir.js';
2
+ export * from './init/init.js';
3
+ export * from './karin/karin.js';
4
+ export * from './listener/listener.js';
5
+ export * from './plugin/base.js';
6
+ export * from './plugin/app.js';
7
+ export * from './plugin/loader.js';
8
+ export * from './process/process.js';
9
+ export * from './server/server.js';
10
10
  import '../adapter/input/index.js';
package/lib/core/index.js CHANGED
@@ -1,10 +1,10 @@
1
- export * from './dir.js'
2
- export * from './init.js'
3
- export * from './karin.js'
4
- export * from './listener.js'
5
- export * from './plugin.js'
6
- export * from './plugin.app.js'
7
- export * from './plugin.loader.js'
8
- export * from './process.js'
9
- export * from './server.js'
1
+ export * from './init/dir.js'
2
+ export * from './init/init.js'
3
+ export * from './karin/karin.js'
4
+ export * from './listener/listener.js'
5
+ export * from './plugin/base.js'
6
+ export * from './plugin/app.js'
7
+ export * from './plugin/loader.js'
8
+ export * from './process/process.js'
9
+ export * from './server/server.js'
10
10
  import '../adapter/input/index.js'
@@ -0,0 +1,7 @@
1
+ import path from 'path';
2
+ import { fileURLToPath } from 'url';
3
+ const filename = fileURLToPath(import.meta.url);
4
+ /**
5
+ * - 获取当前npm包的根目录
6
+ */
7
+ export const karinDir = path.resolve(filename, '../../../../').replace(/\\/g, '/').replace(/\/$/, '');
@@ -0,0 +1,46 @@
1
+ import { logger } from '../../utils/index.js';
2
+ /**
3
+ * 启动日志
4
+ */
5
+ logger.mark('Karin 启动中...');
6
+ logger.mark('https://github.com/KarinJS/Karin');
7
+ /**
8
+ * 设置标题
9
+ */
10
+ process.title = 'Karin';
11
+ /**
12
+ * 设置时区
13
+ */
14
+ process.env.TZ = 'Asia/Shanghai';
15
+ const init = () => {
16
+ if (!process.env.karin_app_lang)
17
+ process.env.karin_app_lang = 'js';
18
+ if (!process.env.karin_app_mode)
19
+ process.env.karin_app_mode = 'prod';
20
+ if (!process.env.karin_app_runner)
21
+ process.env.karin_app_runner = 'node';
22
+ if (!process.env.karin_app_start_count)
23
+ process.env.karin_app_start_count = '0';
24
+ /** 正常启动 */
25
+ if (process.env.karin_app_runner === 'node' && process.env.karin_app_lang === 'js' && process.env.karin_app_mode === 'prod') {
26
+ logger.debug('当前为 JavaScript 生产模式');
27
+ return;
28
+ }
29
+ /** pm2 */
30
+ if (process.env.karin_app_runner === 'pm2') {
31
+ logger.info('当前为 PM2 模式');
32
+ return;
33
+ }
34
+ /** js开发模式 */
35
+ if (process.env.karin_app_lang === 'js' && process.env.karin_app_mode === 'dev') {
36
+ logger.info('当前为 JavaScript 开发模式');
37
+ return;
38
+ }
39
+ /** ts开发模式 */
40
+ if (process.env.karin_app_lang === 'ts' && process.env.karin_app_mode === 'dev') {
41
+ logger.info('当前为 TypeScript 开发模式');
42
+ return;
43
+ }
44
+ logger.error('未知的启动模式');
45
+ };
46
+ init();
@@ -1,4 +1,4 @@
1
- import { Contact, PluginApps, KarinElement, KarinMessage, KarinRenderType, PermissionType, RenderResult, NoticeListenEvent, RequestListenEvent, KarinNoticeType, KarinRequestType } from '../types/index.js';
1
+ import { Contact, PluginApps, KarinElement, KarinMessage, KarinRenderType, PermissionType, RenderResult, NoticeListenEvent, RequestListenEvent, KarinNoticeType, KarinRequestType } from '../../types/index.js';
2
2
  type FncFunction = (e: KarinMessage) => Promise<boolean>;
3
3
  type FncElement = string | KarinElement | Array<KarinElement>;
4
4
  export interface OptionsCommand {
@@ -0,0 +1,194 @@
1
+ import { server } from '../server/server.js';
2
+ import { stateArr } from '../plugin/base.js';
3
+ import PluginApp from '../plugin/app.js';
4
+ import { common } from '../../utils/index.js';
5
+ import { listener } from '../listener/listener.js';
6
+ import onebot11 from '../../adapter/onebot/11/index.js';
7
+ import { render, RenderServer } from '../../render/index.js';
8
+ export class Karin {
9
+ start;
10
+ constructor() {
11
+ this.start = false;
12
+ this.run();
13
+ }
14
+ /**
15
+ * - 快速构建命令
16
+ * @param reg - 正则表达式
17
+ * @param second - 函数或者字符串或者KarinElement、KarinElement数组
18
+ * @param options - 选项
19
+ * @returns - 返回插件对象
20
+ */
21
+ command(reg, second, options = {}) {
22
+ reg = typeof reg === 'string' ? new RegExp(reg) : reg;
23
+ const fnc = typeof second === 'function'
24
+ ? second
25
+ : async (e) => {
26
+ const element = typeof second === 'number' ? String(second) : second;
27
+ if ('delay' in options && options.delay)
28
+ await common.sleep(options.delay);
29
+ await e.reply(element, {
30
+ at: ('at' in options && options.at) || false,
31
+ reply: ('reply' in options && options.reply) || false,
32
+ recallMsg: ('recallMsg' in options && Number(options.recallMsg)) || 0,
33
+ });
34
+ return !('stop' in options && !options.stop);
35
+ };
36
+ const data = {
37
+ name: options.name || 'function',
38
+ priority: options.priority,
39
+ rule: [
40
+ {
41
+ reg,
42
+ fnc,
43
+ permission: options.permission || 'all',
44
+ log: options.log ?? true,
45
+ },
46
+ ],
47
+ };
48
+ return PluginApp(data);
49
+ }
50
+ /**
51
+ * - 构建定时任务
52
+ * @param name - 任务名称
53
+ * @param cron - cron表达式
54
+ * @param fnc - 执行函数
55
+ * @param options - 选项
56
+ */
57
+ task(name, cron, fnc, options) {
58
+ if (!name)
59
+ throw new Error('[task]: 缺少参数[name]');
60
+ if (!cron)
61
+ throw new Error('[task]: 缺少参数[cron]');
62
+ if (!fnc)
63
+ throw new Error('[task]: 缺少参数[fnc]');
64
+ const data = {
65
+ name: options?.name || 'task',
66
+ priority: options?.priority,
67
+ task: [
68
+ {
69
+ name,
70
+ cron,
71
+ fnc,
72
+ log: options?.log ?? true,
73
+ },
74
+ ],
75
+ };
76
+ return PluginApp(data);
77
+ }
78
+ /**
79
+ * - 构建handler
80
+ * @param key - 事件key
81
+ * @param fnc - 函数实现
82
+ * @param options - 选项
83
+ */
84
+ handler(key, fnc, options) {
85
+ if (!key)
86
+ throw new Error('[handler]: 缺少参数[key]');
87
+ if (!fnc)
88
+ throw new Error('[handler]: 缺少参数[fnc]');
89
+ const priority = options?.priority || 10000;
90
+ const data = {
91
+ name: options?.name || 'handler',
92
+ priority,
93
+ handler: [
94
+ {
95
+ key,
96
+ fnc,
97
+ priority,
98
+ },
99
+ ],
100
+ };
101
+ return PluginApp(data);
102
+ }
103
+ /**
104
+ * 构建contact
105
+ * @param peer - 群号或者id
106
+ * @param isGroup - 是否是群聊
107
+ * @param sub_peer - 子id
108
+ */
109
+ contact(peer, isGroup = true, sub_peer) {
110
+ if (isGroup) {
111
+ return {
112
+ scene: "group" /* Scene.Group */,
113
+ peer,
114
+ sub_peer: sub_peer || '',
115
+ };
116
+ }
117
+ return {
118
+ scene: "friend" /* Scene.Private */,
119
+ peer,
120
+ sub_peer: sub_peer || '',
121
+ };
122
+ }
123
+ /**
124
+ * - 渲染
125
+ * @param data - 渲染参数
126
+ * @param multiPage - 分片高度
127
+ * @returns - 返回图片base64或数组
128
+ */
129
+ render(data, multiPage) {
130
+ if (typeof data === 'string') {
131
+ /** 分片 */
132
+ if (typeof multiPage === 'number' || multiPage === true) {
133
+ return render.renderMultiHtml(data, multiPage);
134
+ }
135
+ /** 快速渲染 */
136
+ return render.renderHtml(data);
137
+ }
138
+ /** 正常渲染 */
139
+ return render.render(data);
140
+ }
141
+ /**
142
+ * 上下文
143
+ * @param e - 消息事件
144
+ * @param options - 上下文选项
145
+ */
146
+ async ctx(e, options) {
147
+ const time = options?.time || 120;
148
+ const userId = options?.userId || e.user_id;
149
+ const key = e.group_id ? `${e.group_id}.${userId}` : userId;
150
+ stateArr[key] = { type: 'ctx' };
151
+ // 返回promise 设置超时时间
152
+ return new Promise((resolve, reject) => {
153
+ setTimeout(() => {
154
+ if (stateArr[key]) {
155
+ delete stateArr[key];
156
+ if (options?.reply)
157
+ e.reply(options.replyMsg || '操作超时已取消');
158
+ reject(new Error('操作超时已取消'));
159
+ return true;
160
+ }
161
+ }, time * 1000);
162
+ listener.once(`ctx:${key}`, (e) => resolve(e));
163
+ });
164
+ }
165
+ /**
166
+ * accept
167
+ * @param event - 监听事件
168
+ * @param fnc - 实现函数
169
+ */
170
+ accept(event, fnc, options) {
171
+ const data = {
172
+ name: options?.name || 'accept',
173
+ priority: options?.priority,
174
+ event,
175
+ accept: fnc,
176
+ log: options?.log,
177
+ };
178
+ return PluginApp(data);
179
+ }
180
+ /**
181
+ * - 启动
182
+ */
183
+ run() {
184
+ if (this.start)
185
+ return;
186
+ this.start = true;
187
+ server.init();
188
+ listener.emit('load.plugin');
189
+ listener.emit('adapter', RenderServer);
190
+ listener.emit('adapter', onebot11);
191
+ }
192
+ }
193
+ export const karin = new Karin();
194
+ export default karin;
@@ -1,5 +1,5 @@
1
1
  import { EventEmitter } from 'events';
2
- import { KarinAdapter, Contact, KarinElement } from '../types/index.js';
2
+ import { KarinAdapter, Contact, KarinElement } from '../../types/index.js';
3
3
  /**
4
4
  * 监听器管理
5
5
  */
@@ -0,0 +1,213 @@
1
+ import { level } from '../../db/index.js';
2
+ import { EventEmitter } from 'events';
3
+ import { pluginLoader } from '../plugin/loader.js';
4
+ import { common, logger, config, segment } from '../../utils/index.js';
5
+ import { MessageHandler, NoticeHandler, RequestHandler } from '../../event/index.js';
6
+ /**
7
+ * 监听器管理
8
+ */
9
+ class Listeners extends EventEmitter {
10
+ /**
11
+ * Bot索引
12
+ * @type - Bot索引
13
+ */
14
+ index;
15
+ /**
16
+ * 框架名称
17
+ */
18
+ name;
19
+ list;
20
+ adapter;
21
+ constructor() {
22
+ super();
23
+ this.index = 0;
24
+ this.name = 'Karin';
25
+ this.list = [];
26
+ this.adapter = [];
27
+ this.on('error', data => logger.error(data));
28
+ this.on('load.plugin', () => pluginLoader.load());
29
+ this.on('adapter', data => {
30
+ let path = data.path || '无';
31
+ if (path && data.type !== 'grpc')
32
+ path = `ws://127.0.0.1:${config.Server.http.port}${data.path}`;
33
+ path = logger.green(path);
34
+ logger.info(`[适配器][注册][${data.type}]: ` + path);
35
+ this.addAdapter(data);
36
+ });
37
+ this.on('message', data => new MessageHandler(data));
38
+ this.on('notice', data => new NoticeHandler(data));
39
+ this.on('request', data => new RequestHandler(data));
40
+ }
41
+ /**
42
+ * 注册Bot 返回索引id
43
+ */
44
+ addBot(data) {
45
+ this.index++;
46
+ const index = this.index;
47
+ if (!data.bot) {
48
+ logger.error('[Bot管理][注册] 注册失败: Bot实例不能为空', JSON.stringify(data));
49
+ return false;
50
+ }
51
+ this.list.push({ index, type: data.type, bot: data.bot });
52
+ logger.info(`[机器人][注册][${data.type}] ` + logger.green(`[account:${data.bot.account.uid || data.bot.account.uin}(${data.bot.account.name})]`));
53
+ this.#online(data.bot.account.uid || data.bot.account.uin);
54
+ logger.debug('注册', this.list);
55
+ return index;
56
+ }
57
+ /**
58
+ * 发送上线通知
59
+ */
60
+ async #online(uid) {
61
+ /** 重启 */
62
+ const key = `karin:restart:${uid}`;
63
+ const options = await level.get(key);
64
+ if (!options)
65
+ return;
66
+ const { id, contact, time, message_id } = options;
67
+ /** 重启花费时间 保留2位小数 */
68
+ const restartTime = ((Date.now() - time) / 1000).toFixed(2);
69
+ /** 超过2分钟不发 */
70
+ if (Number(restartTime) > 120) {
71
+ await level.del(key);
72
+ return false;
73
+ }
74
+ const element = [
75
+ segment.reply(message_id),
76
+ segment.text(`Karin 重启成功:${restartTime}秒`),
77
+ ];
78
+ await this.sendMsg(id, contact, element);
79
+ await level.del(key);
80
+ return true;
81
+ }
82
+ /**
83
+ * 卸载Bot
84
+ * @param index - Bot的索引id
85
+ */
86
+ delBot(index) {
87
+ this.list = this.list.filter(item => item.index !== index);
88
+ logger.debug('[机器人][卸载] ', this.list);
89
+ }
90
+ /**
91
+ * 通过Bot uid 获取Bot
92
+ * @param uid - Bot的uid 未传入则返回第一个Bot
93
+ */
94
+ getBot(uid = '') {
95
+ uid = String(uid);
96
+ if (this.list.length === 0) {
97
+ logger.error('[Bot管理][UID] 当前Bot列表为空');
98
+ return undefined;
99
+ }
100
+ if (!uid)
101
+ return this.list[0].bot;
102
+ const index = this.list.findIndex(item => String(item.bot.account.uid) === uid);
103
+ if (index === -1) {
104
+ logger.error('[Bot管理][UID] 无法找到对应的 Bot 实例');
105
+ return undefined;
106
+ }
107
+ return this.list[index].bot;
108
+ }
109
+ /**
110
+ * 根据索引获取Bot
111
+ * @param index - Bot的索引id
112
+ */
113
+ getBotByIndex(index) {
114
+ index = this.list.findIndex(item => item.index === index);
115
+ if (index === -1) {
116
+ throw new Error('[Bot管理][索引] 无法找到对应的 Bot 实例');
117
+ }
118
+ return this.list[index].bot;
119
+ }
120
+ /**
121
+ * 获取当前已注册Bot数量
122
+ */
123
+ getBotCount() {
124
+ return this.list.length;
125
+ }
126
+ /**
127
+ * 获取所有Bot列表
128
+ * @param isIndex - 是否返回包含的索引列表 默认返回Bot实例列表
129
+ */
130
+ getBotAll(isIndex = false) {
131
+ if (isIndex)
132
+ return this.list;
133
+ return this.list.map(item => item.bot);
134
+ }
135
+ /**
136
+ * 注册适配器
137
+ * @param data - 适配器信息
138
+ * @param data.type - 适配器类型
139
+ * @param data.adapter - 适配器实例
140
+ * @param data.path - 适配器路径
141
+ */
142
+ addAdapter(data) {
143
+ const adapter = { type: data.type, adapter: data.adapter, path: '' };
144
+ if (data.path)
145
+ adapter.path = data.path;
146
+ this.adapter.push(adapter);
147
+ }
148
+ /**
149
+ * 通过path获取适配器 仅适用于反向WS适配器
150
+ * @param path - 适配器路径
151
+ */
152
+ getAdapter(path = '') {
153
+ const index = this.adapter.findIndex(item => item?.path === path);
154
+ if (index === -1) {
155
+ logger.error('[适配器管理] 无法找到对应的适配器实例');
156
+ return undefined;
157
+ }
158
+ return this.adapter[index].adapter;
159
+ }
160
+ /**
161
+ * 获取适配器列表
162
+ * @param isType - 是否返回包含的类型列表 默认返回适配器实例列表
163
+ */
164
+ getAdapterAll(isType = false) {
165
+ if (isType)
166
+ return this.adapter;
167
+ return this.adapter.map(item => item.adapter);
168
+ }
169
+ /**
170
+ * 发送主动消息
171
+ * @param uid - Bot的uid
172
+ * @param contact - 目标信息
173
+ * @param elements - 消息内容
174
+ * @param options - 消息选项
175
+ * @param options.recallMsg - 发送成功后撤回消息时间
176
+ * @param options.retry_count - 重试次数
177
+ */
178
+ async sendMsg(uid, contact, elements, options = { recallMsg: 0, retry_count: 1 }) {
179
+ const bot = this.getBot(uid);
180
+ if (!bot)
181
+ throw new Error('发送消息失败: 未找到对应Bot实例');
182
+ const { recallMsg, retry_count } = options;
183
+ /** 标准化 */
184
+ const NewElements = common.makeMessage(elements);
185
+ /** 结果 */
186
+ let result = {};
187
+ const reply_log = common.makeMessageLog(NewElements);
188
+ const self_id = bot.account.uid || bot.account.uin;
189
+ if (contact.scene === 'group') {
190
+ logger.bot('info', self_id, `${logger.green('Send Proactive Group')} ${contact.peer}: ${reply_log}`);
191
+ }
192
+ else {
193
+ logger.bot('info', self_id, `${logger.green('Send Proactive private')} ${contact.peer}: ${reply_log}`);
194
+ }
195
+ try {
196
+ this.emit('karin:count:send', 1);
197
+ /** 取结果 */
198
+ result = await bot.SendMessage(contact, NewElements, retry_count);
199
+ logger.bot('debug', self_id, `主动消息结果:${JSON.stringify(result, null, 2)}`);
200
+ }
201
+ catch (error) {
202
+ logger.bot('error', self_id, `主动消息发送失败:${reply_log}`);
203
+ logger.bot('error', self_id, error);
204
+ }
205
+ /** 快速撤回 */
206
+ if (recallMsg && bot.RecallMessage && recallMsg > 0 && result?.message_id) {
207
+ setTimeout(() => bot.RecallMessage(contact, result.message_id), recallMsg * 1000);
208
+ }
209
+ return result;
210
+ }
211
+ }
212
+ export const listener = new Listeners();
213
+ export const Bot = listener;
@@ -1,4 +1,4 @@
1
- import { dirName, fileName, PluginApps } from '../types/index.js';
1
+ import { dirName, fileName, PluginApps } from '../../types/index.js';
2
2
  export interface PluginAppType {
3
3
  file?: {
4
4
  dir?: dirName;
@@ -0,0 +1,19 @@
1
+ export default function PluginApp(options) {
2
+ return {
3
+ file: {
4
+ dir: options?.file?.dir || '',
5
+ name: options?.file?.name || '',
6
+ type: options?.file?.type || 'function',
7
+ Fnc: options?.file?.fnc || '',
8
+ },
9
+ name: options.name || '',
10
+ event: options.event || "message" /* EventType.Message */,
11
+ priority: options.priority || 10000,
12
+ accept: options.accept ?? false,
13
+ log: options.log ?? true,
14
+ rule: options.rule || [],
15
+ task: options.task || [],
16
+ handler: options.handler || [],
17
+ button: options.button || [],
18
+ };
19
+ }
@@ -1,4 +1,4 @@
1
- import { PluginType, KarinElement, NodeElement, stateArrType, KarinNoticeType, KarinRequestType, ReplyReturn, EType } from '../types/index.js';
1
+ import { PluginType, KarinElement, NodeElement, stateArrType, KarinNoticeType, KarinRequestType, ReplyReturn, EType } from '../../types/index.js';
2
2
  /**
3
3
  * 插件基类
4
4
  */