node-karin 0.2.1 → 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/lib/core/listener.js +4 -4
- package/lib/core/plugin.d.ts +5 -5
- package/lib/core/plugin.js +3 -2
- package/lib/core/plugin.loader.d.ts +46 -84
- package/lib/core/plugin.loader.js +220 -244
- package/lib/core/server.js +1 -1
- package/lib/db/level.d.ts +1 -1
- package/lib/event/message.handler.js +3 -3
- package/lib/event/review.handler.d.ts +5 -5
- package/lib/index.d.ts +1 -7
- package/lib/index.js +5 -5
- package/lib/tools/init.js +1 -1
- package/lib/types/event.d.ts +13 -2
- package/lib/types/plugin.d.ts +7 -10
- package/lib/utils/button.d.ts +1 -47
- package/lib/utils/button.js +29 -73
- package/lib/utils/config.js +10 -10
- package/lib/utils/handler.d.ts +14 -51
- package/lib/utils/handler.js +60 -55
- package/package.json +4 -6
- /package/lib/{renderer → render}/app.d.ts +0 -0
- /package/lib/{renderer → render}/app.js +0 -0
- /package/lib/{renderer → render}/base.d.ts +0 -0
- /package/lib/{renderer → render}/base.js +0 -0
- /package/lib/{renderer → render}/client.d.ts +0 -0
- /package/lib/{renderer → render}/client.js +0 -0
- /package/lib/{renderer → render}/http.d.ts +0 -0
- /package/lib/{renderer → render}/http.js +0 -0
- /package/lib/{renderer → render}/index.d.ts +0 -0
- /package/lib/{renderer → render}/index.js +0 -0
- /package/lib/{renderer → render}/server.d.ts +0 -0
- /package/lib/{renderer → render}/server.js +0 -0
- /package/lib/{renderer → render}/wormhole.d.ts +0 -0
- /package/lib/{renderer → render}/wormhole.js +0 -0
package/lib/index.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export * from 'kritor-proto';
|
|
|
4
4
|
export * from './core/index.js';
|
|
5
5
|
export * from './event/index.js';
|
|
6
6
|
export * from './db/index.js';
|
|
7
|
-
export * from './
|
|
7
|
+
export * from './render/index.js';
|
|
8
8
|
export * from './utils/index.js';
|
|
9
9
|
export * from './types/index.js';
|
|
10
10
|
import { Karin, Plugin } from './core/index.js';
|
|
@@ -66,9 +66,6 @@ export declare const Bot: {
|
|
|
66
66
|
adapter: {
|
|
67
67
|
type: "internal" | "http" | "grpc" | "ws" | "render";
|
|
68
68
|
adapter: new () => import("./types/index.js").KarinAdapter;
|
|
69
|
-
/**
|
|
70
|
-
* @description 即将废弃,请使用 `Plugin`
|
|
71
|
-
*/
|
|
72
69
|
path: string;
|
|
73
70
|
}[];
|
|
74
71
|
addBot(data: {
|
|
@@ -92,9 +89,6 @@ export declare const Bot: {
|
|
|
92
89
|
getAdapterAll(isType?: boolean): {
|
|
93
90
|
type: "internal" | "http" | "grpc" | "ws" | "render";
|
|
94
91
|
adapter: new () => import("./types/index.js").KarinAdapter;
|
|
95
|
-
/**
|
|
96
|
-
* @description 即将废弃,请使用 `Plugin`
|
|
97
|
-
*/
|
|
98
92
|
path: string;
|
|
99
93
|
}[] | (new () => import("./types/index.js").KarinAdapter)[];
|
|
100
94
|
sendMsg(uid: string, contact: import("./types/index.js").contact, elements: import("./types/index.js").KarinElement, options?: {
|
package/lib/index.js
CHANGED
|
@@ -3,17 +3,17 @@ export * from 'kritor-proto';
|
|
|
3
3
|
export * from './core/index.js';
|
|
4
4
|
export * from './event/index.js';
|
|
5
5
|
export * from './db/index.js';
|
|
6
|
-
export * from './
|
|
6
|
+
export * from './render/index.js';
|
|
7
7
|
export * from './utils/index.js';
|
|
8
8
|
export * from './types/index.js';
|
|
9
9
|
import { config, update } from './utils/index.js';
|
|
10
|
-
import { render } from './
|
|
11
|
-
import { RenderServer } from './
|
|
10
|
+
import { render } from './render/index.js';
|
|
11
|
+
import { RenderServer } from './render/index.js';
|
|
12
12
|
import OneBot11 from './adapter/onebot/onebot11.js';
|
|
13
|
-
import { server, Karin,
|
|
13
|
+
import { server, Karin, listener, Plugin } from './core/index.js';
|
|
14
14
|
// 初始化
|
|
15
15
|
server.init();
|
|
16
|
-
|
|
16
|
+
listener.emit('load.plugin');
|
|
17
17
|
listener.emit('adapter', RenderServer);
|
|
18
18
|
listener.emit('adapter', OneBot11);
|
|
19
19
|
/**
|
package/lib/tools/init.js
CHANGED
|
@@ -24,7 +24,7 @@ for (const dir of delList) {
|
|
|
24
24
|
}
|
|
25
25
|
}
|
|
26
26
|
// 判断是否为第一次使用
|
|
27
|
-
if (!fs.existsSync('./index.js')
|
|
27
|
+
if (!fs.existsSync('./index.js')) {
|
|
28
28
|
// 创建一个index.js文件 以供初次开箱使用,写入默认配置
|
|
29
29
|
fs.writeFileSync('./index.js', `import { Bot } from 'node-karin'
|
|
30
30
|
console.log(Bot.name + '开始初始化~')
|
package/lib/types/event.d.ts
CHANGED
|
@@ -890,7 +890,18 @@ export interface GroupHonorInfo {
|
|
|
890
890
|
*/
|
|
891
891
|
description: string;
|
|
892
892
|
}
|
|
893
|
+
export interface EMap {
|
|
894
|
+
message: KarinMessage;
|
|
895
|
+
notice: KarinNotice;
|
|
896
|
+
request: KarinRequest;
|
|
897
|
+
message_sent: KarinMessage;
|
|
898
|
+
meta_event: any;
|
|
899
|
+
}
|
|
900
|
+
export type E<T extends keyof EMap> = EMap[T];
|
|
901
|
+
export type EType = KarinMessage | KarinNotice | KarinRequest;
|
|
893
902
|
/**
|
|
894
|
-
*
|
|
903
|
+
* 根据accept函数是否存在来决定e的类型
|
|
895
904
|
*/
|
|
896
|
-
export type
|
|
905
|
+
export type EventType<T> = T extends {
|
|
906
|
+
accept: (e: any) => Promise<void>;
|
|
907
|
+
} ? KarinNotice | KarinRequest : KarinMessage;
|
package/lib/types/plugin.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import schedule from 'node-schedule';
|
|
3
3
|
import { KarinNodeElement } from './element.js';
|
|
4
4
|
import { Reply, replyCallback } from './reply.js';
|
|
5
|
-
import {
|
|
5
|
+
import { EventType, Event, Permission, SubEvent } from './event.js';
|
|
6
6
|
/**
|
|
7
7
|
* - 插件根目录名称
|
|
8
8
|
* - 例如: karin-plugin-example
|
|
@@ -71,15 +71,11 @@ export interface PluginButton {
|
|
|
71
71
|
/**
|
|
72
72
|
* - 按钮命令正则
|
|
73
73
|
*/
|
|
74
|
-
reg: string;
|
|
74
|
+
reg: string | RegExp;
|
|
75
75
|
/**
|
|
76
76
|
* - 执行方法名称
|
|
77
77
|
*/
|
|
78
|
-
fnc: string;
|
|
79
|
-
/**
|
|
80
|
-
* - 优先级 不填默认为主优先度
|
|
81
|
-
*/
|
|
82
|
-
priority: number;
|
|
78
|
+
fnc: string | Function;
|
|
83
79
|
}
|
|
84
80
|
/**
|
|
85
81
|
* - handler规则
|
|
@@ -137,7 +133,7 @@ export interface PluginType {
|
|
|
137
133
|
*/
|
|
138
134
|
event: Event | `${Event}.${SubEvent}`;
|
|
139
135
|
/**
|
|
140
|
-
* - 优先级 默认为
|
|
136
|
+
* - 优先级 默认为10000
|
|
141
137
|
*/
|
|
142
138
|
priority: number;
|
|
143
139
|
/**
|
|
@@ -166,8 +162,9 @@ export interface PluginType {
|
|
|
166
162
|
timeout: NodeJS.Timeout | undefined;
|
|
167
163
|
/**
|
|
168
164
|
* - 上报事件
|
|
165
|
+
* - 根据上报中的event字段来获取e的事件类型
|
|
169
166
|
*/
|
|
170
|
-
e:
|
|
167
|
+
e: EventType<this>;
|
|
171
168
|
/**
|
|
172
169
|
* - 快速回复
|
|
173
170
|
*/
|
|
@@ -218,7 +215,7 @@ export interface PluginType {
|
|
|
218
215
|
/**
|
|
219
216
|
* - accept标准方法 给通知、请求事件使用
|
|
220
217
|
*/
|
|
221
|
-
accept?(e:
|
|
218
|
+
accept?(e: EventType<this>): Promise<void>;
|
|
222
219
|
}
|
|
223
220
|
/**
|
|
224
221
|
* 上下文状态
|
package/lib/utils/button.d.ts
CHANGED
|
@@ -1,48 +1,2 @@
|
|
|
1
1
|
import { KarinMessage } from '../event/message.js';
|
|
2
|
-
|
|
3
|
-
export declare const button: {
|
|
4
|
-
Apps: Array<{
|
|
5
|
-
App: new () => PluginType;
|
|
6
|
-
name: string;
|
|
7
|
-
priority: number;
|
|
8
|
-
file: {
|
|
9
|
-
dir: dirName;
|
|
10
|
-
name: fileName;
|
|
11
|
-
};
|
|
12
|
-
rule: Array<{
|
|
13
|
-
reg: RegExp;
|
|
14
|
-
fnc: string;
|
|
15
|
-
}>;
|
|
16
|
-
}>;
|
|
17
|
-
add({ name, dir, App, Class }: {
|
|
18
|
-
dir: dirName;
|
|
19
|
-
name: fileName;
|
|
20
|
-
App: new () => PluginType;
|
|
21
|
-
Class: PluginType;
|
|
22
|
-
}): void;
|
|
23
|
-
/**
|
|
24
|
-
* 卸载按钮
|
|
25
|
-
* @param {string} dir 插件目录
|
|
26
|
-
* @param {string} name 插件文件名称
|
|
27
|
-
*/
|
|
28
|
-
del(dir: dirName, name: fileName): {
|
|
29
|
-
App: new () => PluginType;
|
|
30
|
-
name: string;
|
|
31
|
-
priority: number;
|
|
32
|
-
file: {
|
|
33
|
-
dir: dirName;
|
|
34
|
-
name: fileName;
|
|
35
|
-
};
|
|
36
|
-
rule: Array<{
|
|
37
|
-
reg: RegExp;
|
|
38
|
-
fnc: string;
|
|
39
|
-
}>;
|
|
40
|
-
}[];
|
|
41
|
-
update({ dir, name, App, Class }: {
|
|
42
|
-
dir: dirName;
|
|
43
|
-
name: fileName;
|
|
44
|
-
App: new () => PluginType;
|
|
45
|
-
Class: PluginType;
|
|
46
|
-
}): void;
|
|
47
|
-
get(e: KarinMessage): Promise<any[]>;
|
|
48
|
-
};
|
|
2
|
+
export declare const button: (e: KarinMessage) => Promise<any>;
|
package/lib/utils/button.js
CHANGED
|
@@ -1,79 +1,35 @@
|
|
|
1
|
-
import lodash from 'lodash'
|
|
2
1
|
import logger from './logger.js'
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
file: { name, dir },
|
|
26
|
-
rule,
|
|
27
|
-
})
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* 卸载按钮
|
|
32
|
-
* @param {string} dir 插件目录
|
|
33
|
-
* @param {string} name 插件文件名称
|
|
34
|
-
*/
|
|
35
|
-
del (dir, name) {
|
|
36
|
-
/** 未传入name则删除所有 */
|
|
37
|
-
if (!name) {
|
|
38
|
-
this.Apps = this.Apps.filter(v => v.file.dir !== dir)
|
|
39
|
-
} else {
|
|
40
|
-
/** 传入name则删除指定 */
|
|
41
|
-
this.Apps = this.Apps.filter(v => v.file.dir !== dir || v.file.name !== name)
|
|
42
|
-
}
|
|
43
|
-
/** 排序 */
|
|
44
|
-
this.Apps = lodash.orderBy(this.Apps, ['priority'], ['asc'])
|
|
45
|
-
return this.Apps
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
update ({ dir, name, App, Class }) {
|
|
49
|
-
this.del(dir, name)
|
|
50
|
-
this.add({ name, dir, App, Class })
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
async get (e) {
|
|
54
|
-
const button = []
|
|
55
|
-
for (const app of this.Apps) {
|
|
56
|
-
for (const v of app.rule) {
|
|
57
|
-
/** 这里的lastIndex是为了防止正则无法从头开始匹配 */
|
|
58
|
-
v.reg.lastIndex = 0
|
|
59
|
-
if (v.reg.test(e.msg)) {
|
|
60
|
-
try {
|
|
61
|
-
const App = new app.App()
|
|
62
|
-
App.e = e
|
|
63
|
-
const res = await App[v.fnc](e)
|
|
64
|
-
if (!res) { continue }
|
|
65
|
-
/** 是否继续循环 */
|
|
66
|
-
const cycle = res.cycle ?? true
|
|
67
|
-
delete res.cycle
|
|
68
|
-
button.push(res)
|
|
69
|
-
if (cycle !== false) { return button }
|
|
70
|
-
} catch (error) {
|
|
71
|
-
logger.error(error)
|
|
2
|
+
import { pluginLoader as loader } from '../core/index.js'
|
|
3
|
+
export const button = async (e) => {
|
|
4
|
+
const button = []
|
|
5
|
+
for (const v of loader.buttonIds) {
|
|
6
|
+
const info = loader.PluginList[v]
|
|
7
|
+
for (const v of info.button) {
|
|
8
|
+
const reg = v.reg
|
|
9
|
+
/** 这里的lastIndex是为了防止正则无法从头开始匹配 */
|
|
10
|
+
if (reg.test(e.msg)) {
|
|
11
|
+
try {
|
|
12
|
+
let res
|
|
13
|
+
let done = true
|
|
14
|
+
/**
|
|
15
|
+
* 标记函数 如果调用则继续执行 循环下一个按钮插件处理
|
|
16
|
+
*/
|
|
17
|
+
const reject = () => { done = false }
|
|
18
|
+
if (typeof v.fnc === 'function') {
|
|
19
|
+
res = await v.fnc(e, reject)
|
|
20
|
+
} else {
|
|
21
|
+
const cla = new info.file.Fnc()
|
|
22
|
+
cla.e = e
|
|
23
|
+
res = await cla[v.fnc](e, reject)
|
|
72
24
|
}
|
|
25
|
+
if (res) { button.push(res) }
|
|
26
|
+
if (done) { return res }
|
|
27
|
+
} catch (error) {
|
|
28
|
+
logger.error(error)
|
|
73
29
|
}
|
|
74
30
|
}
|
|
75
31
|
}
|
|
76
|
-
/** 理论上不会走到这里,但是还是要稳一手,不排除有所有插件都false... */
|
|
77
|
-
return button
|
|
78
32
|
}
|
|
79
|
-
|
|
33
|
+
/** 理论上不会走到这里,但是还是要稳一手,不排除有所有插件都false... */
|
|
34
|
+
return button
|
|
35
|
+
}
|
package/lib/utils/config.js
CHANGED
|
@@ -238,11 +238,11 @@ export const config = new (class Cfg {
|
|
|
238
238
|
/** 修改日志等级 */
|
|
239
239
|
this.loggger.level = this.Config.log4jsCfg.level
|
|
240
240
|
await this.#review()
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
241
|
+
if (this.Server.HotUpdate) {
|
|
242
|
+
const { Bot } = await import('../index.js')
|
|
243
|
+
Bot.emit('restart_http', {})
|
|
244
|
+
Bot.emit('restart_grpc', {})
|
|
245
|
+
}
|
|
246
246
|
}
|
|
247
247
|
|
|
248
248
|
async change_group () {
|
|
@@ -250,10 +250,10 @@ export const config = new (class Cfg {
|
|
|
250
250
|
}
|
|
251
251
|
|
|
252
252
|
async #review () {
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
253
|
+
if (this.review) { return }
|
|
254
|
+
this.review = true
|
|
255
|
+
const { review } = await import('../event/index.js')
|
|
256
|
+
review.main()
|
|
257
|
+
this.review = false
|
|
258
258
|
}
|
|
259
259
|
})()
|
package/lib/utils/handler.d.ts
CHANGED
|
@@ -1,64 +1,25 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { dirName, fileName } from '../types/index.js';
|
|
1
|
+
import { EventType, PluginType } from '../types/index.js';
|
|
3
2
|
/**
|
|
4
3
|
* 事件处理器类
|
|
5
4
|
*/
|
|
6
5
|
export declare const handler: {
|
|
7
|
-
events: {
|
|
8
|
-
[key: string]: {
|
|
9
|
-
/**
|
|
10
|
-
* - 文件信息
|
|
11
|
-
*/
|
|
12
|
-
file: {
|
|
13
|
-
/**
|
|
14
|
-
* - 插件包名称
|
|
15
|
-
*/
|
|
16
|
-
dir: dirName;
|
|
17
|
-
/**
|
|
18
|
-
* - 文件名称
|
|
19
|
-
*/
|
|
20
|
-
name: fileName;
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* - 事件class
|
|
24
|
-
*/
|
|
25
|
-
App: new () => Plugin;
|
|
26
|
-
/**
|
|
27
|
-
* - 事件键
|
|
28
|
-
*/
|
|
29
|
-
key: string;
|
|
30
|
-
/**
|
|
31
|
-
* - 事件处理函数名称
|
|
32
|
-
*/
|
|
33
|
-
fnc: string;
|
|
34
|
-
/**
|
|
35
|
-
* - 优先级
|
|
36
|
-
*/
|
|
37
|
-
priority: number;
|
|
38
|
-
}[];
|
|
39
|
-
};
|
|
40
6
|
/**
|
|
41
7
|
* 添加事件处理器
|
|
42
|
-
* @param
|
|
43
|
-
* @param
|
|
44
|
-
* @param {string} config.dir 处理器所在目录
|
|
45
|
-
* @param {Function} config.App 应用构造函数
|
|
46
|
-
* @param {Object} config.Class 类配置
|
|
8
|
+
* @param index 插件索引
|
|
9
|
+
* @param Class 插件类
|
|
47
10
|
*/
|
|
48
|
-
add(
|
|
49
|
-
dir: dirName;
|
|
50
|
-
name: fileName;
|
|
51
|
-
App: new () => Plugin;
|
|
52
|
-
Class: Plugin;
|
|
53
|
-
}): void;
|
|
11
|
+
add(index: string, Class: PluginType): void;
|
|
54
12
|
/**
|
|
55
13
|
* 删除事件处理器
|
|
14
|
+
* 如果不传参数则删除所有处理器
|
|
56
15
|
*/
|
|
57
|
-
del({
|
|
58
|
-
dir: dirName | '';
|
|
59
|
-
name: fileName | '';
|
|
16
|
+
del({ index, key }: {
|
|
60
17
|
/**
|
|
61
|
-
*
|
|
18
|
+
* 插件索引
|
|
19
|
+
*/
|
|
20
|
+
index: string | '';
|
|
21
|
+
/**
|
|
22
|
+
* 事件键
|
|
62
23
|
*/
|
|
63
24
|
key: string | '';
|
|
64
25
|
}): boolean;
|
|
@@ -67,7 +28,9 @@ export declare const handler: {
|
|
|
67
28
|
* @param key 事件键
|
|
68
29
|
* @param args 自定义参数 一般用来传递e之类的
|
|
69
30
|
*/
|
|
70
|
-
call(key: string, args?: {
|
|
31
|
+
call(key: string, args?: {
|
|
32
|
+
e?: EventType<unknown>;
|
|
33
|
+
}): Promise<any>;
|
|
71
34
|
/**
|
|
72
35
|
* 检查是否存在指定键的事件处理器
|
|
73
36
|
*/
|
package/lib/utils/handler.js
CHANGED
|
@@ -1,64 +1,62 @@
|
|
|
1
|
-
import util from 'util'
|
|
2
1
|
import lodash from 'lodash'
|
|
3
2
|
import logger from './logger.js'
|
|
3
|
+
import { pluginLoader as loader } from '../core/index.js'
|
|
4
4
|
/**
|
|
5
5
|
* 事件处理器类
|
|
6
6
|
*/
|
|
7
7
|
export const handler = new (class EventHandler {
|
|
8
|
-
events
|
|
9
|
-
constructor () {
|
|
10
|
-
this.events = {}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
8
|
/**
|
|
14
9
|
* 添加事件处理器
|
|
15
|
-
* @param
|
|
16
|
-
* @param
|
|
17
|
-
* @param {string} config.dir 处理器所在目录
|
|
18
|
-
* @param {Function} config.App 应用构造函数
|
|
19
|
-
* @param {Object} config.Class 类配置
|
|
10
|
+
* @param index 插件索引
|
|
11
|
+
* @param Class 插件类
|
|
20
12
|
*/
|
|
21
|
-
add (
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (!
|
|
25
|
-
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
logger.debug(`[Handler][Reg]: [${name}][${key}]`)
|
|
31
|
-
if (!Array.isArray(this.events[key])) { this.events[key] = [] }
|
|
32
|
-
this.events[key].push({ file: { name, dir }, App, key, fnc, priority })
|
|
33
|
-
this.events[key] = lodash.orderBy(this.events[key], ['priority'], ['asc'])
|
|
34
|
-
}
|
|
13
|
+
add (index, Class) {
|
|
14
|
+
lodash.forEach(Class.handler, val => {
|
|
15
|
+
if (!val.key) { logger.error(`[Handler][Add]: [${Class.name}] 缺少 key`) }
|
|
16
|
+
if (!val.fnc) { logger.error(`[Handler][Add]: [${Class.name}] 缺少 fnc`) }
|
|
17
|
+
logger.debug(`[Handler][Reg]: [${Class.name}][${val.key}]`)
|
|
18
|
+
if (!Array.isArray(loader.handlerIds[val.key])) { loader.handlerIds[val.key] = [] }
|
|
19
|
+
loader.handlerIds[val.key].push({ index, fnc: val.fnc, priority: val.priority || Class.priority })
|
|
20
|
+
loader.handlerIds[val.key] = lodash.orderBy(loader.handlerIds[val.key], ['priority'], ['asc'])
|
|
21
|
+
})
|
|
35
22
|
}
|
|
36
23
|
|
|
37
24
|
/**
|
|
38
25
|
* 删除事件处理器
|
|
26
|
+
* 如果不传参数则删除所有处理器
|
|
39
27
|
*/
|
|
40
|
-
del ({
|
|
41
|
-
/**
|
|
42
|
-
if (!key && !
|
|
43
|
-
|
|
28
|
+
del ({ index = '', key = '' }) {
|
|
29
|
+
/** 无参 */
|
|
30
|
+
if (!key && !index) {
|
|
31
|
+
loader.handlerIds = {}
|
|
44
32
|
return true
|
|
45
33
|
}
|
|
46
|
-
/**
|
|
34
|
+
/** 删除指定索引插件 */
|
|
47
35
|
if (!key) {
|
|
48
|
-
for (const v of Object.keys(
|
|
49
|
-
|
|
36
|
+
for (const v of Object.keys(loader.handlerIds)) {
|
|
37
|
+
loader.handlerIds[v] = loader.handlerIds[v].filter(v => v.index !== index)
|
|
50
38
|
// 如果处理器为空则删除键
|
|
51
|
-
if (!
|
|
52
|
-
delete
|
|
39
|
+
if (!loader.handlerIds[v].length) {
|
|
40
|
+
delete loader.handlerIds[v]
|
|
53
41
|
} else {
|
|
54
|
-
|
|
42
|
+
loader.handlerIds[v] = lodash.orderBy(loader.handlerIds[v], ['priority'], ['asc'])
|
|
55
43
|
}
|
|
56
44
|
}
|
|
57
45
|
return true
|
|
58
46
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
47
|
+
/** 删除指定key */
|
|
48
|
+
if (!index) {
|
|
49
|
+
loader.handlerIds[key] && delete loader.handlerIds[key]
|
|
50
|
+
return true
|
|
51
|
+
}
|
|
52
|
+
/** 删除指定key的index */
|
|
53
|
+
if (!loader.handlerIds[key]) { return false }
|
|
54
|
+
loader.handlerIds[key] = loader.handlerIds[key].filter(v => v.index !== index)
|
|
55
|
+
if (!loader.handlerIds[key].length) {
|
|
56
|
+
delete loader.handlerIds[key]
|
|
57
|
+
return true
|
|
58
|
+
}
|
|
59
|
+
loader.handlerIds[key] = lodash.orderBy(loader.handlerIds[key], ['priority'], ['asc'])
|
|
62
60
|
return true
|
|
63
61
|
}
|
|
64
62
|
|
|
@@ -68,35 +66,42 @@ export const handler = new (class EventHandler {
|
|
|
68
66
|
* @param args 自定义参数 一般用来传递e之类的
|
|
69
67
|
*/
|
|
70
68
|
async call (key, args = {}) {
|
|
71
|
-
let
|
|
72
|
-
for (const v of
|
|
73
|
-
const
|
|
74
|
-
if ('e' in args && args.e) { App.e = args.e }
|
|
75
|
-
let done = true
|
|
76
|
-
// 标记函数,用于标记处理器是否执行成功,由处理器自行调用,如果未调用则认为处理器未执行成功
|
|
77
|
-
const reject = (msg = '') => {
|
|
78
|
-
if (msg) { logger.mark(`[Handler][Reject]: [${v.file.dir}][${v.file.name}][${key}] ${msg}`) }
|
|
79
|
-
done = false
|
|
80
|
-
}
|
|
69
|
+
let res
|
|
70
|
+
for (const v of loader.handlerIds[key] || []) {
|
|
71
|
+
const info = loader.PluginList[v.index]
|
|
81
72
|
try {
|
|
82
|
-
|
|
83
|
-
|
|
73
|
+
let done = true
|
|
74
|
+
/**
|
|
75
|
+
* 拒绝处理器 调用后则不再继续执行下一个处理器
|
|
76
|
+
* @param msg 错误信息
|
|
77
|
+
*/
|
|
78
|
+
const reject = (msg = '') => {
|
|
79
|
+
if (msg) { logger.mark(`[Handler][Reject]: [${info.file.dir}][${info.file.name}][${key}] ${msg}`) }
|
|
80
|
+
done = false
|
|
81
|
+
}
|
|
82
|
+
if (info.file.type === 'function' && typeof v.fnc === 'function') {
|
|
83
|
+
res = await v.fnc(args, reject)
|
|
84
|
+
} else {
|
|
85
|
+
const cla = new info.file.Fnc()
|
|
86
|
+
cla.e = args.e
|
|
87
|
+
res = await cla[v.fnc](args, reject)
|
|
88
|
+
}
|
|
84
89
|
if (done) {
|
|
85
|
-
logger.mark(`[Handler][Done]: [${
|
|
86
|
-
return
|
|
90
|
+
logger.mark(`[Handler][Done]: [${info.file.dir}][${info.file.name}][${key}]`)
|
|
91
|
+
return res
|
|
87
92
|
}
|
|
88
93
|
} catch (e) {
|
|
89
94
|
// 产生错误继续下一个处理器
|
|
90
|
-
logger.error(`[Handler][Error]: [${
|
|
95
|
+
logger.error(`[Handler][Error]: [${info.file.dir}][${info.file.name}][${key}] ${e}`)
|
|
91
96
|
}
|
|
92
97
|
}
|
|
93
|
-
return
|
|
98
|
+
return res
|
|
94
99
|
}
|
|
95
100
|
|
|
96
101
|
/**
|
|
97
102
|
* 检查是否存在指定键的事件处理器
|
|
98
103
|
*/
|
|
99
104
|
has (key) {
|
|
100
|
-
return !!
|
|
105
|
+
return !!loader.handlerIds[key]
|
|
101
106
|
}
|
|
102
107
|
})()
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-karin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
|
+
"description": "基于 Kritor 进行开发的nodejs机器人框架",
|
|
5
6
|
"homepage": "https://github.com/KarinJS/Karin",
|
|
6
7
|
"bugs": {
|
|
7
8
|
"url": "https://github.com/KarinJS/Karin/issues"
|
|
@@ -41,8 +42,7 @@
|
|
|
41
42
|
"init": "node lib/tools/install.js",
|
|
42
43
|
"init:dev": "node lib/tools/install.js",
|
|
43
44
|
"init:pack": "node lib/tools/install.js",
|
|
44
|
-
"
|
|
45
|
-
"js:dev": "node . --dev",
|
|
45
|
+
"dev": "tsx ./lib/index.js --dev",
|
|
46
46
|
"log": "node lib/tools/pm2Log.js",
|
|
47
47
|
"monit": "pm2 monit",
|
|
48
48
|
"pub": "npm publish --access public",
|
|
@@ -50,8 +50,6 @@
|
|
|
50
50
|
"sort:pack": "npx sort-package-json",
|
|
51
51
|
"start": "pm2 start ./config/config/pm2.yaml && pm2 monit",
|
|
52
52
|
"stop": "pm2 delete ./config/config/pm2.yaml",
|
|
53
|
-
"ts": "tsx ./src/index.ts --dev",
|
|
54
|
-
"ts:dev": "tsx ./src/index.ts --dev",
|
|
55
53
|
"uninstall": "node lib/tools/uninstall.js",
|
|
56
54
|
"ver": "node lib/tools/updateVersion.js"
|
|
57
55
|
},
|
|
@@ -80,7 +78,7 @@
|
|
|
80
78
|
"@types/node-schedule": "^2.1.7",
|
|
81
79
|
"@types/ws": "8.5.10",
|
|
82
80
|
"eslint": "9.5.0",
|
|
83
|
-
"neostandard": "^0.
|
|
81
|
+
"neostandard": "^0.9.0",
|
|
84
82
|
"sort-package-json": "^2.10.0",
|
|
85
83
|
"tsc-alias": "^1.8.10",
|
|
86
84
|
"tsx": "^4.15.7",
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|