node-karin 0.11.10 → 0.11.12

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.
@@ -98,7 +98,7 @@ export class AdapterInput {
98
98
  this.SendMessage(e.contact, elements);
99
99
  return { message_id: e.message_id, message_time: Date.now(), raw_data: elements };
100
100
  };
101
- listener.emit('message', e);
101
+ listener.emit('adapter.message', e);
102
102
  }
103
103
  async #MsgToFile(type, file) {
104
104
  if (!msgToFile)
@@ -81,7 +81,7 @@ export class OB11Event {
81
81
  * 快速回复 开发者不应该使用这个方法,应该使用由karin封装过后的reply方法
82
82
  */
83
83
  e.replyCallback = async (elements) => await this.adapter.SendMessage(e.contact, elements);
84
- listener.emit('message', e);
84
+ listener.emit('adapter.message', e);
85
85
  }
86
86
  /**
87
87
  * 通知事件
@@ -360,7 +360,7 @@ export class OB11Event {
360
360
  * 快速回复 开发者不应该使用这个方法,应该使用由karin封装过后的reply方法
361
361
  */
362
362
  notice.replyCallback = async (elements) => await this.adapter.SendMessage(notice.contact, elements);
363
- listener.emit('notice', notice);
363
+ listener.emit('adapter.notice', notice);
364
364
  }
365
365
  /** 请求事件 */
366
366
  requestEvent(data) {
@@ -395,7 +395,7 @@ export class OB11Event {
395
395
  * 快速回复 开发者不应该使用这个方法,应该使用由karin封装过后的reply方法
396
396
  */
397
397
  request.replyCallback = async (elements) => await this.adapter.SendMessage(request.contact, elements);
398
- listener.emit('request', request);
398
+ listener.emit('adapter.request', request);
399
399
  return;
400
400
  }
401
401
  case 'group': {
@@ -431,7 +431,7 @@ export class OB11Event {
431
431
  * 快速回复 开发者不应该使用这个方法,应该使用由karin封装过后的reply方法
432
432
  */
433
433
  request.replyCallback = async (elements) => await this.adapter.SendMessage(request.contact, elements);
434
- listener.emit('request', request);
434
+ listener.emit('adapter.request', request);
435
435
  return;
436
436
  }
437
437
  default: {
@@ -1,6 +1,6 @@
1
1
  import WebSocket from 'ws';
2
2
  import { randomUUID } from 'crypto';
3
- import { listener } from '../../../core/listener/listener.js';
3
+ import { karin } from '../../../core/karin/karin.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';
@@ -80,7 +80,7 @@ export class AdapterOneBot11 {
80
80
  /** 停止全部监听 */
81
81
  this.socket.removeAllListeners();
82
82
  /** 注销bot */
83
- this.adapter.index && listener.delBot(this.adapter.index);
83
+ this.adapter.index && karin.delBot(this.adapter.index);
84
84
  /** 正向ws需要重连 */
85
85
  if (this.adapter.sub_type === 'client') {
86
86
  this.index++;
@@ -121,7 +121,7 @@ export class AdapterOneBot11 {
121
121
  this.account.name = data.account_name;
122
122
  this.logger('info', `[加载完成][app_name:${this.version.name}][version:${this.version.version}] ` + logger.green(this.adapter.connect));
123
123
  /** 注册bot */
124
- const index = listener.addBot({ type: this.adapter.type, bot: this });
124
+ const index = karin.addBot({ type: this.adapter.type, bot: this });
125
125
  if (index)
126
126
  this.adapter.index = index;
127
127
  }
@@ -1,6 +1,6 @@
1
+ export * from './karin/karin.js';
1
2
  export * from './init/dir.js';
2
3
  export * from './init/init.js';
3
- export * from './karin/karin.js';
4
4
  export * from './listener/listener.js';
5
5
  export * from './plugin/base.js';
6
6
  export * from '../types/plugin/app.js';
package/lib/core/index.js CHANGED
@@ -1,6 +1,6 @@
1
+ export * from './karin/karin.js'
1
2
  export * from './init/dir.js'
2
3
  export * from './init/init.js'
3
- export * from './karin/karin.js'
4
4
  export * from './listener/listener.js'
5
5
  export * from './plugin/base.js'
6
6
  export * from '../types/plugin/app.js'
@@ -1,4 +1,5 @@
1
1
  import { Contact, KarinElement, KarinMessage, KarinRenderType, PermissionType, RenderResult, KarinNoticeType, KarinRequestType, KarinMessageType, AllMessageSubType, CommandInfo, TaskInfo, HandlerInfo, AcceptInfo, UseInfo, AllNoticeSubType, AllRequestSubType } from '../../types/index.js';
2
+ import { Listeners } from '../listener/listener.js';
2
3
  type FncFunction = (e: KarinMessage) => Promise<boolean>;
3
4
  type FncElement = string | KarinElement | Array<KarinElement>;
4
5
  type UseReceive = (e: KarinMessageType, next: Function, exit: Function) => Promise<void>;
@@ -68,8 +69,8 @@ export interface OptionsElement extends OptionsCommand {
68
69
  */
69
70
  stop?: boolean;
70
71
  }
71
- export declare class Karin {
72
- start: boolean;
72
+ export declare class Karin extends Listeners {
73
+ #private;
73
74
  constructor();
74
75
  /**
75
76
  * @param reg - 正则表达式
@@ -165,7 +166,17 @@ export declare class Karin {
165
166
  /**
166
167
  * - 启动
167
168
  */
168
- run(): void;
169
+ run(): Promise<void>;
169
170
  }
170
171
  export declare const karin: Karin;
172
+ /**
173
+ * 即将废弃 请使用karin
174
+ * @deprecated
175
+ */
176
+ export declare const listener: Karin;
177
+ /**
178
+ * 即将废弃 请使用karin
179
+ * @deprecated
180
+ */
181
+ export declare const Bot: Karin;
171
182
  export default karin;
@@ -1,13 +1,17 @@
1
- import { server } from '../server/server.js';
2
1
  import { stateArr } from '../plugin/base.js';
3
- import { common } from '../../utils/index.js';
4
- import { listener } from '../listener/listener.js';
5
2
  import onebot11 from '../../adapter/onebot/11/index.js';
6
- import { render, RenderServer } from '../../render/index.js';
7
- export class Karin {
8
- start;
3
+ import { render } from '../../render/app.js';
4
+ import { RenderServer } from '../../render/server.js';
5
+ import { pluginLoader } from '../plugin/loader.js';
6
+ import { common } from '../../utils/common/common.js';
7
+ import { logger } from '../../utils/core/logger.js';
8
+ import { Listeners } from '../listener/listener.js';
9
+ export class Karin extends Listeners {
10
+ /** 是否启动 */
11
+ #start;
9
12
  constructor() {
10
- this.start = false;
13
+ super();
14
+ this.#start = false;
11
15
  this.run();
12
16
  }
13
17
  /**
@@ -138,7 +142,6 @@ export class Karin {
138
142
  const userId = options?.userId || e.user_id;
139
143
  const key = e.group_id ? `${e.group_id}.${userId}` : userId;
140
144
  stateArr[key] = { type: 'ctx' };
141
- // 返回promise 设置超时时间
142
145
  return new Promise((resolve, reject) => {
143
146
  setTimeout(() => {
144
147
  if (stateArr[key]) {
@@ -149,7 +152,7 @@ export class Karin {
149
152
  return true;
150
153
  }
151
154
  }, time * 1000);
152
- listener.once(`ctx:${key}`, (e) => resolve(e));
155
+ this.once(`ctx:${key}`, (e) => resolve(e));
153
156
  });
154
157
  }
155
158
  /**
@@ -187,15 +190,26 @@ export class Karin {
187
190
  /**
188
191
  * - 启动
189
192
  */
190
- run() {
191
- if (this.start)
193
+ async run() {
194
+ if (this.#start)
192
195
  return;
193
- this.start = true;
196
+ this.#start = true;
197
+ const { server } = await import('../server/server.js');
194
198
  server.init();
195
- listener.emit('load.plugin');
196
- listener.emit('adapter', RenderServer);
197
- listener.emit('adapter', onebot11);
199
+ pluginLoader.load();
200
+ this.emit('adapter', RenderServer);
201
+ this.emit('adapter', onebot11);
198
202
  }
199
203
  }
200
204
  export const karin = new Karin();
205
+ /**
206
+ * 即将废弃 请使用karin
207
+ * @deprecated
208
+ */
209
+ export const listener = karin;
210
+ /**
211
+ * 即将废弃 请使用karin
212
+ * @deprecated
213
+ */
214
+ export const Bot = karin;
201
215
  export default karin;
@@ -1,36 +1,81 @@
1
1
  import { EventEmitter } from 'events';
2
- import { KarinAdapter, Contact, KarinElement } from '../../types/index.js';
2
+ import { Contact, KarinElement, KarinAdapter, KarinMessage, KarinNoticeType, KarinRequestType, KarinMessageType } from '../../types/index.js';
3
+ type AdapterType = KarinAdapter['adapter']['type'];
4
+ type onAdapter = {
5
+ type: AdapterType;
6
+ adapter: new () => KarinAdapter;
7
+ path?: string;
8
+ };
3
9
  /**
4
- * 监听器管理
10
+ * 产生事件映射
11
+ */
12
+ export interface EmittEventMap {
13
+ 'karin:count:send': number;
14
+ 'karin:count:fnc': string;
15
+ error: any;
16
+ adapter: onAdapter;
17
+ 'adapter.message': KarinMessageType;
18
+ 'adapter.notice': KarinNoticeType;
19
+ 'adapter.request': KarinRequestType;
20
+ message: KarinMessageType;
21
+ notice: KarinNoticeType;
22
+ request: KarinRequestType;
23
+ 'plugin.watch': undefined;
24
+ 'restart.grpc': undefined;
25
+ 'restart.http': undefined;
26
+ }
27
+ /** 上下文 */
28
+ type ContextEvents = {
29
+ [K in `ctx:${string}`]: (e: KarinMessage) => void;
30
+ };
31
+ /**
32
+ * 监听事件映射
33
+ */
34
+ export interface OnEventMap extends ContextEvents {
35
+ 'karin:count:send': (count: number) => void;
36
+ 'karin:count:fnc': (fnc: string) => void;
37
+ error: (error: any) => void;
38
+ adapter: (data: onAdapter) => void;
39
+ 'adapter.message': (data: KarinMessageType) => void;
40
+ 'adapter.notice': (data: KarinNoticeType) => void;
41
+ 'adapter.request': (data: KarinRequestType) => void;
42
+ message: (data: KarinMessageType) => void;
43
+ notice: (data: KarinNoticeType) => void;
44
+ request: (data: KarinRequestType) => void;
45
+ 'plugin.watch': undefined;
46
+ 'restart.grpc': undefined;
47
+ 'restart.http': undefined;
48
+ }
49
+ /**
50
+ * 监听器实例
5
51
  */
6
52
  export declare class Listeners extends EventEmitter {
7
53
  #private;
8
- /**
9
- * Bot索引
10
- * @type - Bot索引
11
- */
12
- index: number;
13
- /**
14
- * 框架名称
15
- */
54
+ /** 框架名称 */
16
55
  name: string;
56
+ /** Bot列表 */
17
57
  list: Array<{
18
58
  index: number;
19
- type: KarinAdapter['adapter']['type'];
59
+ type: AdapterType;
20
60
  bot: KarinAdapter;
21
61
  }>;
62
+ /** 适配器列表 */
22
63
  adapter: Array<{
23
- type: KarinAdapter['adapter']['type'];
64
+ type: AdapterType;
24
65
  adapter: new () => KarinAdapter;
25
66
  path: string;
26
67
  }>;
27
68
  constructor();
69
+ on<K extends keyof OnEventMap>(event: K, listener: OnEventMap[K]): this;
70
+ on(event: string, listener: (...args: any[]) => void): this;
71
+ emit<K extends keyof EmittEventMap>(event: K, ...args: Parameters<EmittEventMap[K]>): boolean;
72
+ emit(event: string | symbol, ...args: any[]): boolean;
28
73
  /**
29
74
  * 注册Bot 返回索引id
30
75
  */
31
76
  addBot(data: {
32
77
  bot: KarinAdapter;
33
- type: KarinAdapter['adapter']['type'];
78
+ type: AdapterType;
34
79
  }): number | false;
35
80
  /**
36
81
  * 卸载Bot
@@ -66,11 +111,7 @@ export declare class Listeners extends EventEmitter {
66
111
  * @param data.adapter - 适配器实例
67
112
  * @param data.path - 适配器路径
68
113
  */
69
- addAdapter(data: {
70
- type: KarinAdapter['adapter']['type'];
71
- adapter: new () => KarinAdapter;
72
- path?: string;
73
- }): void;
114
+ addAdapter(data: onAdapter): void;
74
115
  /**
75
116
  * 通过path获取适配器 仅适用于反向WS适配器
76
117
  * @param path - 适配器路径
@@ -81,7 +122,7 @@ export declare class Listeners extends EventEmitter {
81
122
  * @param isType - 是否返回包含的类型列表 默认返回适配器实例列表
82
123
  */
83
124
  getAdapterAll(isType?: boolean): {
84
- type: KarinAdapter["adapter"]["type"];
125
+ type: AdapterType;
85
126
  adapter: new () => KarinAdapter;
86
127
  path: string;
87
128
  }[] | (new () => KarinAdapter)[];
@@ -101,5 +142,4 @@ export declare class Listeners extends EventEmitter {
101
142
  message_id: string;
102
143
  }>;
103
144
  }
104
- export declare const listener: Listeners;
105
- export declare const Bot: Listeners;
145
+ export {};
@@ -4,46 +4,44 @@ import { pluginLoader } from '../plugin/loader.js';
4
4
  import { common, logger, config, segment } from '../../utils/index.js';
5
5
  import { MessageHandler, NoticeHandler, RequestHandler } from '../../event/index.js';
6
6
  /**
7
- * 监听器管理
7
+ * 监听器实例
8
8
  */
9
9
  export class Listeners extends EventEmitter {
10
- /**
11
- * Bot索引
12
- * @type - Bot索引
13
- */
14
- index;
15
- /**
16
- * 框架名称
17
- */
10
+ /** 框架名称 */
18
11
  name;
12
+ /** Bot列表 */
19
13
  list;
14
+ /** 适配器列表 */
20
15
  adapter;
16
+ /** Bot自增索引 */
17
+ #index;
18
+ /** 是否启动 */
19
+ #start;
21
20
  constructor() {
22
21
  super();
23
- this.index = 0;
22
+ this.#index = 0;
24
23
  this.name = 'Karin';
25
24
  this.list = [];
26
25
  this.adapter = [];
27
26
  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));
27
+ this.on('adapter', data => this.addAdapter(data));
28
+ this.on('adapter.message', data => new MessageHandler(data));
29
+ this.on('adapter.notice', data => new NoticeHandler(data));
30
+ this.on('adapter.request', data => new RequestHandler(data));
31
+ this.#start = false;
32
+ }
33
+ on(event, listener) {
34
+ return super.on(event, listener);
35
+ }
36
+ emit(event, ...args) {
37
+ return super.emit(event, ...args);
40
38
  }
41
39
  /**
42
40
  * 注册Bot 返回索引id
43
41
  */
44
42
  addBot(data) {
45
- this.index++;
46
- const index = this.index;
43
+ this.#index++;
44
+ const index = this.#index;
47
45
  if (!data.bot) {
48
46
  logger.error('[Bot管理][注册] 注册失败: Bot实例不能为空', JSON.stringify(data));
49
47
  return false;
@@ -55,8 +53,8 @@ export class Listeners extends EventEmitter {
55
53
  return index;
56
54
  }
57
55
  /**
58
- * 发送上线通知
59
- */
56
+ * 发送上线通知
57
+ */
60
58
  async #online(uid) {
61
59
  /** 重启 */
62
60
  const key = `karin:restart:${uid}`;
@@ -140,6 +138,11 @@ export class Listeners extends EventEmitter {
140
138
  * @param data.path - 适配器路径
141
139
  */
142
140
  addAdapter(data) {
141
+ let path = data.path || '无';
142
+ if (path && data.type !== 'grpc')
143
+ path = `ws://127.0.0.1:${config.Server.http.port}${data.path}`;
144
+ path = logger.green(path);
145
+ logger.info(`[适配器][注册][${data.type}]: ` + path);
143
146
  const adapter = { type: data.type, adapter: data.adapter, path: '' };
144
147
  if (data.path)
145
148
  adapter.path = data.path;
@@ -230,5 +233,3 @@ export class Listeners extends EventEmitter {
230
233
  return result;
231
234
  }
232
235
  }
233
- export const listener = new Listeners();
234
- export const Bot = listener;
@@ -3,8 +3,8 @@ import path from 'path';
3
3
  import lodash from 'lodash';
4
4
  import chokidar from 'chokidar';
5
5
  import schedule from 'node-schedule';
6
- import { listener } from '../listener/listener.js';
7
6
  import { render } from '../../render/index.js';
7
+ import { karin } from '../karin/karin.js';
8
8
  import { common, logger } from '../../utils/index.js';
9
9
  class PluginLoader {
10
10
  dir;
@@ -67,7 +67,7 @@ class PluginLoader {
67
67
  * 插件初始化
68
68
  */
69
69
  async load() {
70
- listener.once('plugin.watch', () => {
70
+ karin.once('plugin.watch', () => {
71
71
  this.watchList.forEach(async ({ plugin, path }) => {
72
72
  await this.watchDir(plugin, path);
73
73
  logger.debug(`[热更新][${plugin}][${path}] 监听中...`);
@@ -84,7 +84,7 @@ class PluginLoader {
84
84
  this.printDependErr();
85
85
  /** 优先级排序并打印插件信息 */
86
86
  this.orderBy(true);
87
- listener.emit('plugin.watch');
87
+ karin.emit('plugin.watch');
88
88
  return this;
89
89
  }
90
90
  /**
@@ -290,7 +290,7 @@ class PluginLoader {
290
290
  });
291
291
  if (!isPrint)
292
292
  return;
293
- logger.info(`[插件][${Object.keys(this.plugin).length}个] 加载完成`);
293
+ logger.info(`[插件][${this.plugin.size}个] 加载完成`);
294
294
  logger.info(`[渲染器][${render.Apps.length}个] 加载完成`);
295
295
  logger.info(`[command][${this.command.length}个] 加载完成`);
296
296
  logger.info(`[button][${this.button.length}个] 加载完成`);
@@ -1,4 +1,4 @@
1
- import { listener } from '../listener/listener.js';
1
+ import { karin } from '../karin/karin.js';
2
2
  import { logger, common, config } from '../../utils/index.js';
3
3
  /**
4
4
  * 处理基本事件
@@ -35,15 +35,15 @@ export default class Process {
35
35
  /**
36
36
  * 捕获警告
37
37
  */
38
- process.on('warning', warning => listener.emit('warn', warning));
38
+ process.on('warning', warning => karin.emit('warn', warning));
39
39
  /**
40
40
  * 捕获错误
41
41
  */
42
- process.on('uncaughtException', error => listener.emit('error', error));
42
+ process.on('uncaughtException', error => karin.emit('error', error));
43
43
  /**
44
44
  * 捕获未处理的Promise错误
45
45
  */
46
- process.on('unhandledRejection', error => listener.emit('error', error));
46
+ process.on('unhandledRejection', error => karin.emit('error', error));
47
47
  return this;
48
48
  }
49
49
  /**
@@ -21,5 +21,5 @@ export declare const server: {
21
21
  */
22
22
  staticPath(): Promise<void>;
23
23
  /** 重启当前HTTP服务器 */
24
- "__#14@#restartServer"(): Promise<void>;
24
+ "__#15@#restartServer"(): Promise<void>;
25
25
  };
@@ -2,7 +2,7 @@ import fs from 'fs';
2
2
  import Process from '../process/process.js';
3
3
  import { WebSocketServer } from 'ws';
4
4
  import { createServer } from 'http';
5
- import { listener } from '../listener/listener.js';
5
+ import { karin } from '../karin/karin.js';
6
6
  import express from 'express';
7
7
  import { exec, config, logger, common } from '../../utils/index.js';
8
8
  import { AdapterOneBot11 } from '../../adapter/onebot/11/index.js';
@@ -34,7 +34,7 @@ export const server = new (class Server {
34
34
  const headers = request.headers;
35
35
  logger.debug('[反向WS]', path, JSON.stringify(headers, null, 2));
36
36
  try {
37
- const Adapter = listener.getAdapter(path);
37
+ const Adapter = karin.getAdapter(path);
38
38
  if (!Adapter) {
39
39
  logger.error(`[反向WS] 适配器不存在:${path}`);
40
40
  return socket.close();
@@ -73,7 +73,7 @@ export const server = new (class Server {
73
73
  if (req.hostname === 'localhost' || req.hostname === '127.0.0.1') {
74
74
  logger.mark('[服务器][HTTP] 收到退出请求,即将退出');
75
75
  /** 关闭服务器 */
76
- listener.emit('exit.grpc');
76
+ karin.emit('exit.grpc');
77
77
  this.server.close();
78
78
  /** 如果是pm2 获取当前pm2ID 使用 */
79
79
  if (process.env.pm_id)
@@ -113,7 +113,7 @@ export const server = new (class Server {
113
113
  this.server.listen(port, host, () => {
114
114
  logger.mark('[服务器][启动成功][HTTP]: ' + logger.green(`http://${host}:${port}`));
115
115
  });
116
- listener.once('restart.http', () => {
116
+ karin.once('restart.http', () => {
117
117
  logger.mark('[服务器][重启][HTTP] 正在重启HTTP服务器...');
118
118
  this.#restartServer();
119
119
  });
@@ -34,3 +34,4 @@ export default class LevelDB extends Level {
34
34
  }
35
35
  }
36
36
  export const level = new LevelDB();
37
+ level.open();
@@ -1,5 +1,6 @@
1
1
  export default class RedisLevel {
2
2
  #private;
3
+ /** 唯一标识符 用于区分不同的数据库 */
3
4
  id: string;
4
5
  constructor();
5
6
  /**
@@ -1,23 +1,16 @@
1
1
  import { Level } from 'level';
2
2
  export default class RedisLevel {
3
3
  #level;
4
+ /** 过期时间映射表 */
4
5
  #expireMap;
6
+ /** 唯一标识符 用于区分不同的数据库 */
5
7
  id;
6
8
  constructor() {
7
9
  const path = process.cwd() + '/data/db/RedisLevel';
8
10
  this.#level = new Level(path, { valueEncoding: 'json' });
9
- /**
10
- * @type {'RedisLevel'} 唯一标识符 用于区分不同的数据库
11
- */
11
+ this.#level.open();
12
12
  this.id = 'RedisLevel';
13
- /**
14
- * 过期时间映射表
15
- * @type {Map<string, number>} 键: 值 (过期时间)
16
- */
17
13
  this.#expireMap = new Map();
18
- /**
19
- * 开启过期时间处理
20
- */
21
14
  this.#expireHandle();
22
15
  }
23
16
  /**
@@ -2,7 +2,7 @@ import lodash from 'lodash';
2
2
  import { review } from './review.js';
3
3
  import { EventBaseHandler } from './base.js';
4
4
  import { logger, config } from '../../utils/index.js';
5
- import { listener, stateArr, pluginLoader } from '../../core/index.js';
5
+ import { karin, stateArr, pluginLoader } from '../../core/index.js';
6
6
  /**
7
7
  * 消息事件
8
8
  */
@@ -12,7 +12,6 @@ export class MessageHandler extends EventBaseHandler {
12
12
  super(e);
13
13
  this.e = e;
14
14
  this.init();
15
- // todo: emit event
16
15
  if (this.e.group_id) {
17
16
  if (!this.getCd())
18
17
  return;
@@ -34,7 +33,7 @@ export class MessageHandler extends EventBaseHandler {
34
33
  * 先对消息事件进行初始化
35
34
  */
36
35
  init() {
37
- listener.emit('karin:count:recv', 1);
36
+ karin.emit('karin:count:recv', 1);
38
37
  const logs = [];
39
38
  for (const val of this.e.elements) {
40
39
  switch (val.type) {
@@ -176,6 +175,7 @@ export class MessageHandler extends EventBaseHandler {
176
175
  }
177
176
  logs.length = 0;
178
177
  this.reply();
178
+ karin.emit('message', this.e);
179
179
  }
180
180
  /**
181
181
  * 响应模式检查 返回false表示未通过
@@ -242,7 +242,7 @@ export class MessageHandler extends EventBaseHandler {
242
242
  continue;
243
243
  /** 计算插件处理时间 */
244
244
  const start = Date.now();
245
- listener.emit('karin:count:fnc', this.e.logFnc);
245
+ karin.emit('karin:count:fnc', this.e.logFnc);
246
246
  try {
247
247
  let res;
248
248
  if (info.type === 'function') {
@@ -276,7 +276,7 @@ export class MessageHandler extends EventBaseHandler {
276
276
  if (App) {
277
277
  switch (App.type) {
278
278
  case 'ctx': {
279
- listener.emit(`ctx:${key}`, this.e);
279
+ karin.emit(`ctx:${key}`, this.e);
280
280
  delete stateArr[key];
281
281
  return true;
282
282
  }
@@ -1,7 +1,7 @@
1
1
  import { review } from './review.js';
2
2
  import { EventBaseHandler } from './base.js';
3
- import { pluginLoader } from '../../core/index.js';
4
3
  import { logger, config } from '../../utils/index.js';
4
+ import { karin, pluginLoader } from '../../core/index.js';
5
5
  /**
6
6
  * 通知事件
7
7
  */
@@ -57,6 +57,7 @@ export class NoticeHandler extends EventBaseHandler {
57
57
  logger.bot('info', this.e.self_id, `未知来源通知事件:${JSON.stringify(this.e)}`);
58
58
  }
59
59
  this.reply();
60
+ karin.emit('notice', this.e);
60
61
  }
61
62
  /**
62
63
  * 处理事件
@@ -131,7 +132,7 @@ export class NoticeHandler extends EventBaseHandler {
131
132
  /** 群成员增加 */
132
133
  case "group_member_increase" /* NoticeSubType.GroupMemberIncrease */: {
133
134
  const { operator_uid, operator_uin, target_uid, target_uin, type } = this.e.content;
134
- this.e.raw_message = `[群成员新增]: ${operator_uid || operator_uin} ${type === 'invite' ? '邀请' : '同意'} ${target_uid || target_uin} 加入群聊`;
135
+ this.e.raw_message = `[群成员新增]: ${operator_uid || operator_uin} ${type === 'invite' ? '邀请' : '同意'} ${target_uid || target_uin} 加入群聊`;
135
136
  break;
136
137
  }
137
138
  /** 群成员减少 */
@@ -1,6 +1,6 @@
1
1
  import { review } from './review.js';
2
2
  import { EventBaseHandler } from './base.js';
3
- import { pluginLoader } from '../../core/index.js';
3
+ import { karin, pluginLoader } from '../../core/index.js';
4
4
  import { logger, config } from '../../utils/index.js';
5
5
  /**
6
6
  * 请求事件
@@ -52,6 +52,7 @@ export class RequestHandler extends EventBaseHandler {
52
52
  logger.bot('info', this.e.self_id, `未知来源请求事件:${JSON.stringify(this.e)}`);
53
53
  }
54
54
  this.reply();
55
+ karin.emit('request', this.e);
55
56
  }
56
57
  /**
57
58
  * 处理事件
@@ -25,31 +25,31 @@ export declare const review: {
25
25
  /**
26
26
  * 群聊黑白名单 允许哪个群触发事件
27
27
  */
28
- "__#11@#GroupEnable"(): true | undefined;
28
+ "__#1@#GroupEnable"(): true | undefined;
29
29
  /**
30
30
  * 用户黑白名单 允许那个用户触发事件
31
31
  */
32
- "__#11@#UserEnable"(): true | undefined;
32
+ "__#1@#UserEnable"(): true | undefined;
33
33
  /**
34
34
  * 群聊事件日志 是否打印
35
35
  */
36
- "__#11@#GroupMsgPrint"(): true | undefined;
36
+ "__#1@#GroupMsgPrint"(): true | undefined;
37
37
  /**
38
38
  * 黑白名单插件 哪个插件可以被触发
39
39
  */
40
- "__#11@#PluginEnable"(): boolean;
40
+ "__#1@#PluginEnable"(): boolean;
41
41
  /** 群聊cd */
42
- "__#11@#CD"(): true | undefined;
42
+ "__#1@#CD"(): true | undefined;
43
43
  /**
44
44
  * 响应模式
45
45
  */
46
- "__#11@#mode"(): true | undefined;
46
+ "__#1@#mode"(): true | undefined;
47
47
  /**
48
48
  * 前缀、别名
49
49
  */
50
- "__#11@#alias"(): true | undefined;
50
+ "__#1@#alias"(): true | undefined;
51
51
  /**
52
52
  * 私聊功能
53
53
  */
54
- "__#11@#Private"(): true | undefined;
54
+ "__#1@#Private"(): true | undefined;
55
55
  };
@@ -11,7 +11,7 @@ export interface NpmInfo {
11
11
  /**
12
12
  * 常用方法
13
13
  */
14
- declare class Common {
14
+ export declare class Common {
15
15
  streamPipeline: (stream1: Readable, stream2: fs.WriteStream) => Promise<void>;
16
16
  constructor();
17
17
  /**
@@ -85,6 +85,15 @@ declare class Common {
85
85
  * - 写入yaml文件
86
86
  */
87
87
  writeYaml(file: string, data: any): boolean;
88
+ /**
89
+ * 传入插件名称 返回解析后的package.json的内容
90
+ * @param name - 插件名称
91
+ */
92
+ pkgJson(name: string): {
93
+ name: string;
94
+ version: string;
95
+ [key: string]: any;
96
+ } | null;
88
97
  /**
89
98
  * 输入包名 返回包根目录的绝对路径 仅简单查找
90
99
  * @param name - 包名
@@ -178,6 +187,16 @@ declare class Common {
178
187
  * 获取运行时间
179
188
  */
180
189
  uptime(): string;
190
+ /**
191
+ * 传入一个时间戳
192
+ * 返回距今已过去的时间
193
+ * @param time - 时间戳
194
+ *
195
+ * @example
196
+ * common.formatTime(1620000000)
197
+ * // -> '18天'
198
+ */
199
+ formatTime(time: number): string;
181
200
  /**
182
201
  * 构建消息体日志
183
202
  * @param - 消息体
@@ -208,4 +227,3 @@ declare class Common {
208
227
  * 常用方法
209
228
  */
210
229
  export declare const common: Common;
211
- export {};
@@ -11,7 +11,7 @@ import { logger, segment, YamlEditor } from '../../utils/index.js';
11
11
  /**
12
12
  * 常用方法
13
13
  */
14
- class Common {
14
+ export class Common {
15
15
  streamPipeline;
16
16
  constructor() {
17
17
  this.streamPipeline = promisify(pipeline);
@@ -191,6 +191,24 @@ class Common {
191
191
  return false;
192
192
  }
193
193
  }
194
+ /**
195
+ * 传入插件名称 返回解析后的package.json的内容
196
+ * @param name - 插件名称
197
+ */
198
+ pkgJson(name) {
199
+ try {
200
+ /** 先查git插件 */
201
+ const gitPath = `./plugins/${name}`;
202
+ if (fs.existsSync(gitPath))
203
+ return this.readJson(`${gitPath}/package.json`);
204
+ /** 查npm插件 */
205
+ const require = createRequire(import.meta.url);
206
+ return require(`${name}/package.json`);
207
+ }
208
+ catch {
209
+ return null;
210
+ }
211
+ }
194
212
  /**
195
213
  * 输入包名 返回包根目录的绝对路径 仅简单查找
196
214
  * @param name - 包名
@@ -471,6 +489,27 @@ class Common {
471
489
  const parts = [day ? `${day}天` : '', hour ? `${hour}小时` : '', min ? `${min}分钟` : '', !day && sec ? `${sec}秒` : ''];
472
490
  return parts.filter(Boolean).join('');
473
491
  }
492
+ /**
493
+ * 传入一个时间戳
494
+ * 返回距今已过去的时间
495
+ * @param time - 时间戳
496
+ *
497
+ * @example
498
+ * common.formatTime(1620000000)
499
+ * // -> '18天'
500
+ */
501
+ formatTime(time) {
502
+ /** 判断是几位时间戳 进行对应处理 */
503
+ time = time.toString().length === 10 ? time * 1000 : time;
504
+ /** 减去当前时间 */
505
+ time = Math.floor((Date.now() - time) / 1000);
506
+ const day = Math.floor(time / 86400);
507
+ const hour = Math.floor((time % 86400) / 3600);
508
+ const min = Math.floor((time % 3600) / 60);
509
+ const sec = Math.floor(time % 60);
510
+ const parts = [day ? `${day}天` : '', hour ? `${hour}小时` : '', min ? `${min}分钟` : '', !day && sec ? `${sec}秒` : ''];
511
+ return parts.filter(Boolean).join('');
512
+ }
474
513
  /**
475
514
  * 构建消息体日志
476
515
  * @param - 消息体
@@ -95,7 +95,7 @@ export declare const config: {
95
95
  changeApp(): Promise<void>;
96
96
  changeCfg(): Promise<void>;
97
97
  changeGroup(): Promise<void>;
98
- "__#15@#review"(): Promise<void>;
98
+ "__#2@#review"(): Promise<void>;
99
99
  };
100
100
  export declare const Cfg: {
101
101
  /**
@@ -189,5 +189,5 @@ export declare const Cfg: {
189
189
  changeApp(): Promise<void>;
190
190
  changeCfg(): Promise<void>;
191
191
  changeGroup(): Promise<void>;
192
- "__#15@#review"(): Promise<void>;
192
+ "__#2@#review"(): Promise<void>;
193
193
  };
@@ -2,7 +2,7 @@ import fs from 'fs';
2
2
  import Yaml from 'yaml';
3
3
  import path from 'path';
4
4
  import chokidar from 'chokidar';
5
- import { karinDir } from '../../core/index.js';
5
+ import { karinDir } from '../../core/init/dir.js';
6
6
  import { common } from '../../utils/common/common.js';
7
7
  /**
8
8
  * 配置文件
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-karin",
3
- "version": "0.11.10",
3
+ "version": "0.11.12",
4
4
  "private": false,
5
5
  "description": "基于 Kritor 进行开发的nodejs机器人框架",
6
6
  "homepage": "https://github.com/KarinJS/Karin",