node-karin 0.12.10 → 0.12.11

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.
@@ -94,7 +94,7 @@ export class KarinCfgInit {
94
94
  modifyPackage() {
95
95
  const pkg = this.readJson(path.join(karinDir, 'package.json'));
96
96
  const projPkg = this.readJson('./package.json');
97
- const main = 'node_modules/node-karin/lib/index.js';
97
+ const main = 'node_modules/node-karin/lib/cli/start.js';
98
98
  delete pkg.bin;
99
99
  pkg.main = `./${main}`;
100
100
  pkg.types = `./${main.replace('.js', '.d.ts')}`;
@@ -105,13 +105,12 @@ export class KarinCfgInit {
105
105
  projPkg.main = `./${main}`;
106
106
  projPkg.dependencies = { ...projPkg.dependencies, ...pkg.dependencies };
107
107
  projPkg.dependencies['node-karin'] = 'latest';
108
- projPkg.dependencies['kritor-proto'] = 'latest';
109
108
  if (!projPkg.scripts)
110
109
  projPkg.scripts = {};
111
110
  if (!projPkg.devDependencies)
112
111
  projPkg.devDependencies = {};
113
112
  projPkg.scripts.debug = `node --watch ${main}`;
114
- projPkg.scripts.dev = `tsx --watch ${main}`;
113
+ // projPkg.scripts.dev = `tsx --watch ${main}`
115
114
  fs.writeFileSync('./package.json', JSON.stringify(projPkg, null, 2));
116
115
  }
117
116
  /**
@@ -1,4 +1,4 @@
1
- import { Contact, KarinElement, KarinMessage, KarinRenderType, PermissionType, RenderResult, KarinNoticeType, KarinRequestType, AllMessageSubType, CommandInfo, TaskInfo, HandlerInfo, AcceptInfo, UseInfo, AllNoticeSubType, AllRequestSubType, UseMapType } from '../../types/index.js';
1
+ import { Contact, KarinElement, KarinMessage, KarinRenderType, RenderResult, AllMessageSubType, CommandInfo, TaskInfo, HandlerInfo, AcceptInfo, UseInfo, AllNoticeSubType, AllRequestSubType, UseMapType, ButtonInfo, Permission } from '../../types/index.js';
2
2
  import { Listeners } from '../listener/listener.js';
3
3
  export type FncFunction = (e: KarinMessage) => Promise<boolean>;
4
4
  export type FncElement = string | KarinElement | Array<KarinElement>;
@@ -28,7 +28,7 @@ export interface OptionsCommand extends Options {
28
28
  * - 权限
29
29
  * @default 'all'
30
30
  */
31
- permission?: PermissionType;
31
+ permission?: `${Permission}`;
32
32
  }
33
33
  export interface OptionsElement extends OptionsCommand {
34
34
  /**
@@ -87,17 +87,7 @@ export declare class Karin extends Listeners {
87
87
  * @param fn - 函数实现
88
88
  * @param options - 选项
89
89
  */
90
- handler(key: string, fn: (
91
- /**
92
- * - 自定义参数 由调用方传递
93
- */
94
- args: {
95
- [key: string]: any;
96
- },
97
- /**
98
- * - 停止循环函数 调用后则不再继续执行下一个处理器
99
- */
100
- reject: (msg?: string) => void) => Promise<any>, options?: Omit<Options, 'log'>): HandlerInfo;
90
+ handler(key: string, fn: HandlerInfo['fn'], options?: Omit<Options, 'log'>): HandlerInfo;
101
91
  /**
102
92
  * 构建contact
103
93
  * @param scene - 场景
@@ -164,7 +154,7 @@ export declare class Karin extends Listeners {
164
154
  * @param event - 监听事件
165
155
  * @param fn - 实现函数
166
156
  */
167
- accept(event: AllNoticeSubType | AllRequestSubType, fn: (e: KarinNoticeType | KarinRequestType) => Promise<boolean>, options?: Options): AcceptInfo;
157
+ accept(event: AllNoticeSubType | AllRequestSubType, fn: AcceptInfo['fn'], options?: Options): AcceptInfo;
168
158
  /**
169
159
  * 中间件
170
160
  * @param type 中间件类型
@@ -172,6 +162,12 @@ export declare class Karin extends Listeners {
172
162
  * @param options 选项配置
173
163
  */
174
164
  use<T extends keyof UseMapType>(type: `${T}`, fn: UseMapType[T][number]['fn'], options?: Omit<Options, 'log'>): UseInfo;
165
+ /**
166
+ * 按钮
167
+ * @param reg - 正则表达式
168
+ * @param fn - 函数
169
+ */
170
+ button(reg: RegExp | string, fn: ButtonInfo['fn'], options?: Omit<Options, 'log'>): ButtonInfo;
175
171
  /**
176
172
  * - 启动
177
173
  */
@@ -48,11 +48,11 @@ export class Karin extends Listeners {
48
48
  fn,
49
49
  fnname: 'fnc',
50
50
  log,
51
- name: options.name || 'command',
51
+ name: options.name || "command" /* AppType.Command */,
52
52
  perm: options.permission || 'all',
53
53
  rank: options.priority || 10000,
54
54
  reg,
55
- type: 'command',
55
+ type: "command" /* AppType.Command */,
56
56
  };
57
57
  }
58
58
  /**
@@ -74,9 +74,9 @@ export class Karin extends Listeners {
74
74
  cron,
75
75
  fn,
76
76
  log,
77
- name: options?.name || 'task',
77
+ name: options?.name || "task" /* AppType.Task */,
78
78
  fnname: name,
79
- type: 'task',
79
+ type: "task" /* AppType.Task */,
80
80
  };
81
81
  }
82
82
  /**
@@ -93,9 +93,9 @@ export class Karin extends Listeners {
93
93
  return {
94
94
  fn,
95
95
  key,
96
- name: options?.name || 'handler',
96
+ name: options?.name || "handler" /* AppType.Handler */,
97
97
  rank: options?.priority || 10000,
98
- type: 'handler',
98
+ type: "handler" /* AppType.Handler */,
99
99
  };
100
100
  }
101
101
  /**
@@ -182,9 +182,9 @@ export class Karin extends Listeners {
182
182
  event,
183
183
  fn,
184
184
  log,
185
- name: options?.name || 'accept',
185
+ name: options?.name || "accept" /* AppType.Accept */,
186
186
  rank: options?.priority || 10000,
187
- type: 'accept',
187
+ type: "accept" /* AppType.Accept */,
188
188
  };
189
189
  }
190
190
  /**
@@ -197,9 +197,23 @@ export class Karin extends Listeners {
197
197
  return {
198
198
  fn,
199
199
  key: type,
200
- name: options?.name || 'use',
200
+ name: options?.name || "use" /* AppType.Use */,
201
201
  rank: options?.priority || 10000,
202
- type: 'use',
202
+ type: "use" /* AppType.Use */,
203
+ };
204
+ }
205
+ /**
206
+ * 按钮
207
+ * @param reg - 正则表达式
208
+ * @param fn - 函数
209
+ */
210
+ button(reg, fn, options) {
211
+ return {
212
+ fn,
213
+ reg: reg instanceof RegExp ? reg : new RegExp(reg),
214
+ name: options?.name || "button" /* AppType.Button */,
215
+ rank: options?.priority || 10000,
216
+ type: "button" /* AppType.Button */,
203
217
  };
204
218
  }
205
219
  /**
@@ -1,5 +1,5 @@
1
1
  import chokidar from 'chokidar';
2
- import { UseInfo, AppsType, TaskInfo, ButtonInfo, AcceptInfo, CommandInfo, HandlerInfo, PluginInfoType, PluginTaskInfoType, PluginButtonInfoType, PluginAcceptInfoType, PluginHandlerInfoType, PluginCommandInfoType, UseValueType } from '../../types/index.js';
2
+ import { UseInfo, AppsType, TaskInfo, ButtonInfo, AcceptInfo, CommandInfo, HandlerInfo, UseMapType, PluginInfoType, PluginTaskInfoType, PluginButtonInfoType, PluginAcceptInfoType, PluginHandlerInfoType, PluginCommandInfoType } from '../../types/index.js';
3
3
  type AppType = CommandInfo | TaskInfo | HandlerInfo | ButtonInfo | AcceptInfo | UseInfo;
4
4
  export interface AppFile {
5
5
  /** 插件包名称 */
@@ -59,7 +59,7 @@ declare class PluginLoader {
59
59
  /** task定时任务信息 */
60
60
  task: PluginTaskInfoType[];
61
61
  /** 中间件 */
62
- use: UseValueType;
62
+ use: UseMapType;
63
63
  /** 加载的文件数组 .js .ts */
64
64
  ext: string[];
65
65
  constructor();
@@ -52,11 +52,11 @@ class PluginLoader {
52
52
  this.plugin = new Map();
53
53
  this.task = [];
54
54
  this.use = {
55
- recvMsg: [],
56
- replyMsg: [],
57
- sendMsg: [],
58
- forwardMsg: [],
59
- notFoundMsg: [],
55
+ ["recvMsg" /* UseKeyType['ReceiveMsg'] */]: [],
56
+ ["replyMsg" /* UseKeyType['ReplyMsg'] */]: [],
57
+ ["sendMsg" /* UseKeyType['SendMsg'] */]: [],
58
+ ["forwardMsg" /* UseKeyType['ForwardMsg'] */]: [],
59
+ ["notFoundMsg" /* UseKeyType['NotFoundMsg'] */]: [],
60
60
  };
61
61
  this.ext = process.env.karin_app_lang === 'ts' ? ['.js', '.ts'] : ['.js'];
62
62
  this.index = 0;
@@ -288,11 +288,11 @@ class PluginLoader {
288
288
  this.button = lodash.orderBy(this.button, ['rank'], ['asc']);
289
289
  this.command = lodash.orderBy(this.command, ['rank'], ['asc']);
290
290
  this.task = lodash.orderBy(this.task, ['rank'], ['asc']);
291
- this.use.recvMsg = lodash.orderBy(this.use.recvMsg, ['rank'], ['asc']);
292
- this.use.replyMsg = lodash.orderBy(this.use.replyMsg, ['rank'], ['asc']);
293
- this.use.sendMsg = lodash.orderBy(this.use.sendMsg, ['rank'], ['asc']);
294
- this.use.forwardMsg = lodash.orderBy(this.use.forwardMsg, ['rank'], ['asc']);
295
- this.use.notFoundMsg = lodash.orderBy(this.use.notFoundMsg, ['rank'], ['asc']);
291
+ this.use["recvMsg" /* UseKeyType['ReceiveMsg'] */] = lodash.orderBy(this.use["recvMsg" /* UseKeyType['ReceiveMsg'] */], ['rank'], ['asc']);
292
+ this.use["replyMsg" /* UseKeyType['ReplyMsg'] */] = lodash.orderBy(this.use["replyMsg" /* UseKeyType['ReplyMsg'] */], ['rank'], ['asc']);
293
+ this.use["sendMsg" /* UseKeyType['SendMsg'] */] = lodash.orderBy(this.use["sendMsg" /* UseKeyType['SendMsg'] */], ['rank'], ['asc']);
294
+ this.use["forwardMsg" /* UseKeyType['ForwardMsg'] */] = lodash.orderBy(this.use["forwardMsg" /* UseKeyType['ForwardMsg'] */], ['rank'], ['asc']);
295
+ this.use["notFoundMsg" /* UseKeyType['NotFoundMsg'] */] = lodash.orderBy(this.use["notFoundMsg" /* UseKeyType['NotFoundMsg'] */], ['rank'], ['asc']);
296
296
  const handler = Object.keys(this.handler);
297
297
  handler.forEach(key => {
298
298
  this.handler[key] = lodash.orderBy(this.handler[key], ['rank'], ['asc']);
@@ -552,11 +552,11 @@ class PluginLoader {
552
552
  this.accept = this.accept.filter(val => val.key !== key);
553
553
  this.button = this.button.filter(val => val.key !== key);
554
554
  this.command = this.command.filter(val => val.key !== key);
555
- this.use.recvMsg = this.use.recvMsg.filter(val => val.key !== key);
556
- this.use.replyMsg = this.use.replyMsg.filter(val => val.key !== key);
557
- this.use.sendMsg = this.use.sendMsg.filter(val => val.key !== key);
558
- this.use.forwardMsg = this.use.forwardMsg.filter(val => val.key !== key);
559
- this.use.notFoundMsg = this.use.notFoundMsg.filter(val => val.key !== key);
555
+ this.use["recvMsg" /* UseKeyType['ReceiveMsg'] */] = this.use["recvMsg" /* UseKeyType['ReceiveMsg'] */].filter(val => val.key !== key);
556
+ this.use["replyMsg" /* UseKeyType['ReplyMsg'] */] = this.use["replyMsg" /* UseKeyType['ReplyMsg'] */].filter(val => val.key !== key);
557
+ this.use["sendMsg" /* UseKeyType['SendMsg'] */] = this.use["sendMsg" /* UseKeyType['SendMsg'] */].filter(val => val.key !== key);
558
+ this.use["forwardMsg" /* UseKeyType['ForwardMsg'] */] = this.use["forwardMsg" /* UseKeyType['ForwardMsg'] */].filter(val => val.key !== key);
559
+ this.use["notFoundMsg" /* UseKeyType['NotFoundMsg'] */] = this.use["notFoundMsg" /* UseKeyType['NotFoundMsg'] */].filter(val => val.key !== key);
560
560
  /** 定时任务需要先停止 */
561
561
  this.task = this.task.filter(val => {
562
562
  if (val.key === key) {
@@ -17,10 +17,6 @@ export declare const enum Permission {
17
17
  GroupOwner = "group.owner",
18
18
  GroupAdmin = "group.admin"
19
19
  }
20
- /**
21
- * - 权限类型 纯文本
22
- */
23
- export type PermissionType = 'all' | 'master' | 'admin' | 'group.owner' | 'group.admin';
24
20
  /**
25
21
  * - 事件发送者信息
26
22
  */
@@ -1,4 +1,4 @@
1
- import { AllMessageSubType, AllNoticeSubType, AllRequestSubType, KarinMessageType, KarinNoticeType, KarinRequestType, NewMessagePlugin, Permission, UseMapType } from '../../types/index.js';
1
+ import { AllMessageSubType, AllNoticeSubType, AllRequestSubType, ButtonElement, Contact, KarinAdapter, KarinElement, KarinMessage, KarinMessageType, KarinNoticeType, KarinRequestType, KeyBoardElement, NewMessagePlugin, NodeElement, Permission, UseMapType } from '../../types/index.js';
2
2
  export declare const enum AppType {
3
3
  Command = "command",
4
4
  Task = "task",
@@ -7,7 +7,7 @@ export declare const enum AppType {
7
7
  Accept = "accept",
8
8
  Use = "use"
9
9
  }
10
- interface AppInfo {
10
+ export interface AppBase {
11
11
  /** 实现方法 */
12
12
  fn: Function;
13
13
  /** 插件名称 */
@@ -20,7 +20,7 @@ interface AppInfo {
20
20
  log: Function;
21
21
  }
22
22
  /** command规则集类型 */
23
- export interface CommandInfo extends AppInfo {
23
+ export interface CommandInfo extends AppBase {
24
24
  fn: (e: KarinMessageType) => Promise<boolean>;
25
25
  /** 插件执行方法名称 */
26
26
  fnname: string;
@@ -35,7 +35,7 @@ export interface CommandInfo extends AppInfo {
35
35
  data: NewMessagePlugin | '';
36
36
  }
37
37
  /** task规则集类型 */
38
- export interface TaskInfo extends Omit<AppInfo, 'rank'> {
38
+ export interface TaskInfo extends Omit<AppBase, 'rank'> {
39
39
  type: `${AppType.Task}`;
40
40
  /** 任务名称 */
41
41
  fnname: string;
@@ -43,28 +43,106 @@ export interface TaskInfo extends Omit<AppInfo, 'rank'> {
43
43
  cron: string;
44
44
  }
45
45
  /** handler规则集类型 */
46
- export interface HandlerInfo extends Omit<AppInfo, 'log'> {
46
+ export interface HandlerInfo extends Omit<AppBase, 'log'> {
47
47
  type: `${AppType.Handler}`;
48
48
  /** 入口秘钥 */
49
49
  key: string;
50
+ /** 实现方法 */
51
+ fn: (
52
+ /**
53
+ * - 自定义参数 由调用方传递
54
+ */
55
+ args: {
56
+ [key: string]: any;
57
+ },
58
+ /**
59
+ * - 停止循环函数 调用后则不再继续执行下一个处理器
60
+ */
61
+ next: (msg?: string) => void) => Promise<any>;
50
62
  }
51
63
  /** button规则集类型 */
52
- export interface ButtonInfo extends AppInfo {
64
+ export interface ButtonInfo extends Omit<AppBase, 'log'> {
53
65
  type: `${AppType.Button}`;
54
66
  /** 正则 */
55
67
  reg: RegExp;
68
+ fn: (
69
+ /** 是否继续匹配下一个按钮 默认否 调用后则继续 */
70
+ next: () => void,
71
+ /** 消息事件 可能不存在~ */
72
+ e?: KarinMessage,
73
+ /** 自定义参数 */
74
+ ...args: any[]) => Promise<ButtonElement | KeyBoardElement | Array<ButtonElement | KeyBoardElement>>;
56
75
  }
57
76
  /** accept规则集类型 */
58
- export interface AcceptInfo extends AppInfo {
77
+ export interface AcceptInfo extends AppBase {
59
78
  fn: (e: KarinNoticeType | KarinRequestType) => Promise<boolean>;
60
79
  type: `${AppType.Accept}`;
61
80
  /** 监听事件 */
62
81
  event: `${AllNoticeSubType}` | `${AllRequestSubType}`;
63
82
  }
83
+ /**
84
+ * 初始化消息前 中间件实现方法
85
+ */
86
+ export type UseRecvMsgFn = (
87
+ /** 消息事件方法 */
88
+ e: KarinMessageType,
89
+ /** 是否继续执行下一个中间件 */
90
+ next: () => void,
91
+ /** 是否退出此条消息 不再执行匹配插件 */
92
+ exit: () => void) => Promise<boolean>;
93
+ /**
94
+ * 回复消息前 中间件实现方法
95
+ */
96
+ export type UseSendMsgFn = (
97
+ /** 消息事件方法 */
98
+ e: KarinMessageType,
99
+ /** 回复的消息体 */
100
+ element: KarinElement[],
101
+ /** 是否继续执行下一个中间件 */
102
+ next: () => void,
103
+ /** 是否不发送此条消息 */
104
+ exit: () => void) => Promise<boolean>;
105
+ /**
106
+ * 发送主动消息前 中间件实现方法
107
+ */
108
+ export type UseSendNoticeFn = (
109
+ /** 发送的bot */
110
+ uid: string,
111
+ /** 发送目标 */
112
+ contact: Contact,
113
+ /** 发送的消息体 */
114
+ element: KarinElement[],
115
+ /** 是否继续执行下一个中间件 */
116
+ next: () => void,
117
+ /** 是否不发送此条消息 */
118
+ exit: () => void) => Promise<boolean>;
119
+ /**
120
+ * 发送合并转发前 中间件实现方法
121
+ */
122
+ export type UseForwardMsgFn = (bot: KarinAdapter,
123
+ /** 发送的目标信息 */
124
+ contact: Contact,
125
+ /** 发送的消息体 */
126
+ elements: Array<NodeElement>,
127
+ /** 是否继续执行下一个中间件 */
128
+ next: () => void,
129
+ /** 是否不发送此条消息 */
130
+ exit: () => void) => Promise<boolean>;
131
+ /**
132
+ * 消息事件没有找到任何匹配的插件触发 中间件实现方法
133
+ */
134
+ export type UseNotFoundMsgFn = (
135
+ /** 消息事件方法 */
136
+ e: KarinMessageType,
137
+ /** 是否继续执行下一个中间件 */
138
+ next: () => void,
139
+ /** 是否退出此条消息 不再执行匹配插件 */
140
+ exit: () => void) => Promise<boolean>;
64
141
  /** use规则集类型 */
65
- export interface UseInfo extends Omit<AppInfo, 'log'> {
142
+ export interface UseInfo<T extends keyof UseMapType = keyof UseMapType> extends Omit<AppBase, 'log'> {
66
143
  type: `${AppType.Use}`;
67
144
  /** 中间件类型key */
68
- key: `${keyof UseMapType}`;
145
+ key: `${T}`;
146
+ /** 中间件实现方法 */
147
+ fn: UseMapType[T][number]['fn'];
69
148
  }
70
- export {};
@@ -1,9 +1,7 @@
1
1
  import schedule from 'node-schedule';
2
- import { Plugin } from '../../core/index.js';
3
2
  import { Reply, replyCallback, replyForward } from '../event/reply.js';
4
- import { KarinNoticeType, KarinRequestType, AllListenEvent, KarinMessageType, PermissionType, AllMessageSubType, Contact, AllNoticeSubType, AllRequestSubType } from '../event/index.js';
5
- import { KarinElement, NodeElement } from '../element/element.js';
6
- import { KarinAdapter } from '../adapter/base.js';
3
+ import { KarinNoticeType, KarinRequestType, AllListenEvent, KarinMessageType, AllMessageSubType, Permission } from '../event/index.js';
4
+ import { AcceptInfo, ButtonInfo, CommandInfo, HandlerInfo, Plugin, UseForwardMsgFn, UseInfo, UseNotFoundMsgFn, UseRecvMsgFn, UseSendMsgFn, UseSendNoticeFn } from '../../core/index.js';
7
5
  /**
8
6
  * - 插件根目录名称
9
7
  * - 例如: karin-plugin-example
@@ -57,25 +55,25 @@ export interface PluginCommandInfoType {
57
55
  /** 插件基本信息的映射key */
58
56
  key: number;
59
57
  /** 插件包名称 */
60
- name: string;
58
+ name: CommandInfo['name'];
61
59
  /** 插件正则 */
62
- reg: RegExp;
60
+ reg: CommandInfo['reg'];
63
61
  /** 插件执行方法 */
64
- fn: (e: KarinMessageType) => Promise<boolean>;
62
+ fn: CommandInfo['fn'];
65
63
  /** 插件执行方法名称 */
66
- fnname: string;
64
+ fnname: CommandInfo['fnname'];
67
65
  /** 插件类型 */
68
66
  type: `${MethodType}`;
69
67
  /** 在type为class的时候 data为class 否则为空字符串 */
70
68
  data: (() => PluginType) | undefined;
71
69
  /** 插件执行权限 */
72
- perm: `${PermissionType}`;
70
+ perm: CommandInfo['perm'];
73
71
  /** 执行打印日志方法 */
74
- log: Function;
72
+ log: CommandInfo['log'];
75
73
  /** 监听的子事件 高于父事件 */
76
- event: AllMessageSubType;
74
+ event: CommandInfo['event'];
77
75
  /** 优先级 */
78
- rank: number;
76
+ rank: CommandInfo['rank'];
79
77
  }
80
78
  /**
81
79
  * accept规则集信息
@@ -84,15 +82,15 @@ export interface PluginAcceptInfoType {
84
82
  /** 插件基本信息的映射key */
85
83
  key: number;
86
84
  /** 插件包名称 */
87
- name: string;
85
+ name: AcceptInfo['name'];
88
86
  /** 插件执行方法 */
89
- fn: (e: KarinNoticeType | KarinRequestType) => Promise<boolean>;
87
+ fn: AcceptInfo['fn'];
90
88
  /** 优先级 */
91
- rank: number;
89
+ rank: AcceptInfo['rank'];
92
90
  /** 监听事件 */
93
- event: AllNoticeSubType | AllRequestSubType;
91
+ event: AcceptInfo['event'];
94
92
  /** 执行打印日志方法 */
95
- log: Function;
93
+ log: AcceptInfo['log'];
96
94
  }
97
95
  /**
98
96
  * task规则集信息
@@ -122,13 +120,13 @@ export interface PluginButtonInfoType {
122
120
  /** 插件基本信息的映射key */
123
121
  key: number;
124
122
  /** 插件包名称 */
125
- name: string;
123
+ name: ButtonInfo['name'];
126
124
  /** 插件正则 */
127
- reg: RegExp;
125
+ reg: ButtonInfo['reg'];
128
126
  /** 插件执行方法 */
129
- fn: (reject: Function, e?: KarinMessageType) => Promise<any>;
127
+ fn: ButtonInfo['fn'];
130
128
  /** 优先级 */
131
- rank: number;
129
+ rank: ButtonInfo['rank'];
132
130
  }
133
131
  /**
134
132
  * handler规则集信息
@@ -137,109 +135,21 @@ export interface PluginHandlerInfoType {
137
135
  /** 插件基本信息的映射key */
138
136
  key: number;
139
137
  /** 插件包名称 */
140
- name: string;
138
+ name: HandlerInfo['name'];
141
139
  /** handler的处理方法 */
142
- fn: (args: any, reject: (msg?: string) => void) => Promise<any>;
140
+ fn: HandlerInfo['fn'];
143
141
  /** 优先级 */
144
- rank: number;
142
+ rank: HandlerInfo['rank'];
145
143
  }
146
- /**
147
- * 中间件规则集信息
148
- */
149
- export interface UseValueType {
150
- /** 初始化消息前 */
151
- recvMsg: Array<{
152
- /** 插件基本信息的映射key */
153
- key: number;
154
- /** 插件包名称 */
155
- name: string;
156
- /** 插件执行方法 */
157
- fn: (
158
- /** 消息事件方法 */
159
- e: KarinMessageType,
160
- /** 是否继续执行下一个中间件 */
161
- next: Function,
162
- /** 是否退出此条消息 不再执行匹配插件 */
163
- exit: Function) => Promise<boolean>;
164
- /** 优先级 */
165
- rank: number;
166
- }>;
167
- /** 回复消息前 */
168
- replyMsg: Array<{
169
- /** 插件基本信息的映射key */
170
- key: number;
171
- /** 插件包名称 */
172
- name: string;
173
- /** 插件执行方法 */
174
- fn: (
175
- /** 消息事件方法 */
176
- e: KarinMessageType,
177
- /** 回复的消息体 */
178
- element: KarinElement[],
179
- /** 是否继续执行下一个中间件 */
180
- next: Function,
181
- /** 是否不发送此条消息 */
182
- exit: Function) => Promise<boolean>;
183
- /** 优先级 */
184
- rank: number;
185
- }>;
186
- /** 发送主动消息前 */
187
- sendMsg: Array<{
188
- /** 插件基本信息的映射key */
189
- key: number;
190
- /** 插件包名称 */
191
- name: string;
192
- /** 插件执行方法 */
193
- fn: (
194
- /** 发送的bot */
195
- uid: string,
196
- /** 发送目标 */
197
- contact: Contact,
198
- /** 发送的消息体 */
199
- element: KarinElement[],
200
- /** 是否继续执行下一个中间件 */
201
- next: Function,
202
- /** 是否不发送此条消息 */
203
- exit: Function) => Promise<boolean>;
204
- /** 优先级 */
205
- rank: number;
206
- }>;
207
- /** 发送合并转发前 */
208
- forwardMsg: Array<{
209
- /** 插件基本信息的映射key */
210
- key: number;
211
- /** 插件包名称 */
212
- name: string;
213
- /** 插件执行方法 */
214
- fn: (bot: KarinAdapter,
215
- /** 发送的目标信息 */
216
- contact: Contact,
217
- /** 发送的消息体 */
218
- elements: Array<NodeElement>,
219
- /** 是否继续执行下一个中间件 */
220
- next: Function,
221
- /** 是否不发送此条消息 */
222
- exit: Function) => Promise<boolean>;
223
- /** 优先级 */
224
- rank: number;
225
- }>;
226
- /** 消息事件没有找到任何匹配的插件触发 */
227
- notFoundMsg: Array<{
228
- /** 插件基本信息的映射key */
229
- key: number;
230
- /** 插件包名称 */
231
- name: string;
232
- /** 插件执行方法 */
233
- fn: (
234
- /** 消息事件方法 */
235
- e: KarinMessageType,
236
- /** 是否继续执行下一个中间件 */
237
- next: Function,
238
- /** 是否退出此条消息 不再执行匹配插件 */
239
- exit: Function) => Promise<boolean>;
240
- /** 优先级 */
241
- rank: number;
242
- }>;
144
+ export interface UseBase<T> {
145
+ /** 插件基本信息的映射key */
146
+ key: number;
147
+ /** 插件包名称 */
148
+ name: UseInfo['name'];
149
+ /** 插件执行方法 */
150
+ fn: T;
151
+ /** 优先级 */
152
+ rank: UseInfo['rank'];
243
153
  }
244
154
  /**
245
155
  * 中间件类型
@@ -260,11 +170,11 @@ export declare const enum UseKeyType {
260
170
  * 中间件映射
261
171
  */
262
172
  export interface UseMapType {
263
- [UseKeyType.ReceiveMsg]: UseValueType['recvMsg'];
264
- [UseKeyType.ReplyMsg]: UseValueType['replyMsg'];
265
- [UseKeyType.SendMsg]: UseValueType['sendMsg'];
266
- [UseKeyType.ForwardMsg]: UseValueType['forwardMsg'];
267
- [UseKeyType.NotFoundMsg]: UseValueType['notFoundMsg'];
173
+ [UseKeyType.ReceiveMsg]: Array<UseBase<UseRecvMsgFn>>;
174
+ [UseKeyType.ReplyMsg]: Array<UseBase<UseSendMsgFn>>;
175
+ [UseKeyType.SendMsg]: Array<UseBase<UseSendNoticeFn>>;
176
+ [UseKeyType.ForwardMsg]: Array<UseBase<UseForwardMsgFn>>;
177
+ [UseKeyType.NotFoundMsg]: Array<UseBase<UseNotFoundMsgFn>>;
268
178
  }
269
179
  /**
270
180
  * 上下文状态
@@ -304,7 +214,7 @@ export interface PluginRule {
304
214
  /**
305
215
  * 权限
306
216
  */
307
- permission?: PermissionType;
217
+ permission?: `${Permission}`;
308
218
  /**
309
219
  * - 打印日志 默认为true
310
220
  */
@@ -612,9 +612,9 @@ export class Common {
612
612
  const data = {
613
613
  id: String(id),
614
614
  render_data: {
615
- label: i.text,
615
+ label: i.text || i.link || '',
616
616
  style: i.style ?? 0,
617
- visited_label: i.show || i.text,
617
+ visited_label: i.show || i.text || i.link || '',
618
618
  },
619
619
  action: {
620
620
  type,
@@ -19,12 +19,8 @@ export const handler = new (class EventHandler {
19
19
  * 拒绝处理器 调用后则不再继续执行下一个处理器
20
20
  * @param msg 错误信息
21
21
  */
22
- const reject = (msg = '') => {
23
- if (msg)
24
- logger.mark(`[Handler][Reject]: [${plugin.plugin}][${info.name}][${key}] ${msg}`);
25
- done = false;
26
- };
27
- res = await info.fn(args, reject);
22
+ const next = () => { done = false; };
23
+ res = await info.fn(args, next);
28
24
  if (done) {
29
25
  logger.mark(`[Handler][Done]: [${plugin.plugin}][${info.name}][${key}]`);
30
26
  return res;
@@ -258,12 +258,9 @@ export declare const segment: {
258
258
  /**
259
259
  * 按钮 构建单行单个(obj)、多个按钮(obj[])
260
260
  * @param data - 按钮数据
261
- * @returns {ButtonElement} 按钮元素
261
+ * @returns 按钮元素
262
262
  */
263
- button(data: Button | Array<Button>): {
264
- type: ButtonElement["type"];
265
- data: Array<Button>;
266
- };
263
+ button(data: Button | Array<Button>): ButtonElement;
267
264
  /**
268
265
  * 多维按钮
269
266
  * @param data - 按钮数据
@@ -402,7 +402,7 @@ export const segment = new (class Segment {
402
402
  /**
403
403
  * 按钮 构建单行单个(obj)、多个按钮(obj[])
404
404
  * @param data - 按钮数据
405
- * @returns {ButtonElement} 按钮元素
405
+ * @returns 按钮元素
406
406
  */
407
407
  button(data) {
408
408
  return {
@@ -1,2 +1,9 @@
1
- import { KarinMessageType } from '../../types/index.js';
2
- export declare const button: (msg: string, e?: KarinMessageType) => Promise<any>;
1
+ import { ButtonElement, KarinMessageType, KeyBoardElement } from '../../types/index.js';
2
+ /**
3
+ * 按钮处理器类
4
+ * @param msg 传e.msg就行
5
+ * @param e 消息事件 可不传
6
+ * @param arg 自定义参数 可不传
7
+ * @returns 返回按钮元素
8
+ */
9
+ export declare const button: (msg: string, e?: KarinMessageType, ...arg: any[]) => Promise<(ButtonElement | KeyBoardElement)[]>;
@@ -1,23 +1,35 @@
1
1
  import logger from '../core/logger.js';
2
2
  import { pluginLoader as loader } from '../../core/index.js';
3
- export const button = async (msg, e) => {
3
+ /**
4
+ * 按钮处理器类
5
+ * @param msg 传e.msg就行
6
+ * @param e 消息事件 可不传
7
+ * @param arg 自定义参数 可不传
8
+ * @returns 返回按钮元素
9
+ */
10
+ export const button = async (msg, e, ...arg) => {
4
11
  const button = [];
5
12
  for (const info of loader.button) {
6
13
  const reg = info.reg;
7
14
  if (reg.test(msg)) {
8
15
  try {
9
- let done = true;
10
- /**
11
- * 标记函数 如果调用则继续执行 循环下一个按钮插件处理
12
- */
13
- const reject = () => { done = false; };
14
- const res = await info.fn(reject, e);
15
- if (res)
16
- button.push(res);
17
- if (done)
18
- return res;
16
+ let isnext = false;
17
+ /** 如果调用则继续执行 循环下一个按钮插件处理 */
18
+ const next = () => { isnext = true; };
19
+ const list = await info.fn(next, e, arg);
20
+ if (list) {
21
+ if (Array.isArray(list)) {
22
+ button.push(...list);
23
+ }
24
+ else {
25
+ button.push(list);
26
+ }
27
+ }
28
+ if (!isnext)
29
+ return button;
19
30
  }
20
31
  catch (error) {
32
+ logger.error(`[Button][Error]: [${info.name}]:`);
21
33
  logger.error(error);
22
34
  }
23
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-karin",
3
- "version": "0.12.10",
3
+ "version": "0.12.11",
4
4
  "private": false,
5
5
  "description": "基于 Kritor 进行开发的nodejs机器人框架",
6
6
  "homepage": "https://github.com/KarinJS/Karin",