node-karin 0.12.9 → 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,27 +1,8 @@
1
- import { Contact, KarinElement, KarinMessage, KarinRenderType, PermissionType, RenderResult, KarinNoticeType, KarinRequestType, KarinMessageType, AllMessageSubType, CommandInfo, TaskInfo, HandlerInfo, AcceptInfo, UseInfo, AllNoticeSubType, AllRequestSubType } 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
- type FncFunction = (e: KarinMessage) => Promise<boolean>;
4
- type FncElement = string | KarinElement | Array<KarinElement>;
5
- type UseReceive = (e: KarinMessageType, next: Function, exit: Function) => Promise<void>;
6
- type UseReply = (e: KarinMessageType, element: KarinElement[], next: Function, exit: Function) => Promise<void>;
7
- type UseRecord = (uid: string, contact: Contact, elements: KarinElement[], next: Function, exit: Function) => Promise<void>;
8
- type ForwardRecord = (contact: Contact, elements: KarinElement[], next: Function, exit: Function) => Promise<void>;
9
- type NotFoundRecord = (e: KarinMessageType, next: Function, exit: Function) => Promise<void>;
10
- /**
11
- * 中间件类型
12
- */
13
- export declare const enum MiddlewareType {
14
- /** 收到消息后 */
15
- ReceiveMsg = "recvMsg",
16
- /** 回复消息前 */
17
- ReplyMsg = "replyMsg",
18
- /** 发送主动消息前 */
19
- SendMsg = "sendMsg",
20
- /** 发送合并转发前 */
21
- ForwardMsg = "forwardMsg",
22
- /** 消息事件没有找到任何匹配的插件触发 */
23
- NotFoundMsg = "notFound"
24
- }
3
+ export type FncFunction = (e: KarinMessage) => Promise<boolean>;
4
+ export type FncElement = string | KarinElement | Array<KarinElement>;
5
+ export type UseFnType<K extends keyof UseMapType> = UseMapType[K];
25
6
  export interface Options {
26
7
  /**
27
8
  * - 插件名称 不传则使用 插件名称:函数名
@@ -47,7 +28,7 @@ export interface OptionsCommand extends Options {
47
28
  * - 权限
48
29
  * @default 'all'
49
30
  */
50
- permission?: PermissionType;
31
+ permission?: `${Permission}`;
51
32
  }
52
33
  export interface OptionsElement extends OptionsCommand {
53
34
  /**
@@ -106,17 +87,7 @@ export declare class Karin extends Listeners {
106
87
  * @param fn - 函数实现
107
88
  * @param options - 选项
108
89
  */
109
- handler(key: string, fn: (
110
- /**
111
- * - 自定义参数 由调用方传递
112
- */
113
- args: {
114
- [key: string]: any;
115
- },
116
- /**
117
- * - 停止循环函数 调用后则不再继续执行下一个处理器
118
- */
119
- reject: (msg?: string) => void) => Promise<any>, options?: Omit<Options, 'log'>): HandlerInfo;
90
+ handler(key: string, fn: HandlerInfo['fn'], options?: Omit<Options, 'log'>): HandlerInfo;
120
91
  /**
121
92
  * 构建contact
122
93
  * @param scene - 场景
@@ -183,12 +154,20 @@ export declare class Karin extends Listeners {
183
154
  * @param event - 监听事件
184
155
  * @param fn - 实现函数
185
156
  */
186
- accept(event: AllNoticeSubType | AllRequestSubType, fn: (e: KarinNoticeType | KarinRequestType) => Promise<boolean>, options?: Options): AcceptInfo;
187
- use(type: `${MiddlewareType.ReceiveMsg}`, fn: UseReceive, options?: Omit<Options, 'log'>): UseInfo;
188
- use(type: `${MiddlewareType.ReplyMsg}`, fn: UseReply, options?: Omit<Options, 'log'>): UseInfo;
189
- use(type: `${MiddlewareType.SendMsg}`, fn: UseRecord, options?: Omit<Options, 'log'>): UseInfo;
190
- use(type: `${MiddlewareType.ForwardMsg}`, fn: ForwardRecord, options?: Omit<Options, 'log'>): UseInfo;
191
- use(type: `${MiddlewareType.NotFoundMsg}`, fn: NotFoundRecord, options?: Omit<Options, 'log'>): UseInfo;
157
+ accept(event: AllNoticeSubType | AllRequestSubType, fn: AcceptInfo['fn'], options?: Options): AcceptInfo;
158
+ /**
159
+ * 中间件
160
+ * @param type 中间件类型
161
+ * @param fn 中间件函数
162
+ * @param options 选项配置
163
+ */
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;
192
171
  /**
193
172
  * - 启动
194
173
  */
@@ -1,4 +1,3 @@
1
- /* eslint-disable @stylistic/indent */
2
1
  import { stateArr } from '../plugin/base.js';
3
2
  import onebot11 from '../../adapter/onebot/11/index.js';
4
3
  import { render } from '../../render/app.js';
@@ -49,11 +48,11 @@ export class Karin extends Listeners {
49
48
  fn,
50
49
  fnname: 'fnc',
51
50
  log,
52
- name: options.name || 'command',
51
+ name: options.name || "command" /* AppType.Command */,
53
52
  perm: options.permission || 'all',
54
53
  rank: options.priority || 10000,
55
54
  reg,
56
- type: 'command',
55
+ type: "command" /* AppType.Command */,
57
56
  };
58
57
  }
59
58
  /**
@@ -75,9 +74,9 @@ export class Karin extends Listeners {
75
74
  cron,
76
75
  fn,
77
76
  log,
78
- name: options?.name || 'task',
77
+ name: options?.name || "task" /* AppType.Task */,
79
78
  fnname: name,
80
- type: 'task',
79
+ type: "task" /* AppType.Task */,
81
80
  };
82
81
  }
83
82
  /**
@@ -94,9 +93,9 @@ export class Karin extends Listeners {
94
93
  return {
95
94
  fn,
96
95
  key,
97
- name: options?.name || 'handler',
96
+ name: options?.name || "handler" /* AppType.Handler */,
98
97
  rank: options?.priority || 10000,
99
- type: 'handler',
98
+ type: "handler" /* AppType.Handler */,
100
99
  };
101
100
  }
102
101
  /**
@@ -183,23 +182,38 @@ export class Karin extends Listeners {
183
182
  event,
184
183
  fn,
185
184
  log,
186
- name: options?.name || 'accept',
185
+ name: options?.name || "accept" /* AppType.Accept */,
187
186
  rank: options?.priority || 10000,
188
- type: 'accept',
187
+ type: "accept" /* AppType.Accept */,
189
188
  };
190
189
  }
191
190
  /**
192
191
  * 中间件
193
192
  * @param type 中间件类型
194
193
  * @param fn 中间件函数
194
+ * @param options 选项配置
195
195
  */
196
196
  use(type, fn, options) {
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
  /**
@@ -58,7 +58,7 @@ export class Listeners extends EventEmitter {
58
58
  let exit = false;
59
59
  const nextFn = () => { next = true; };
60
60
  const exitFn = () => { exit = true; };
61
- await info.fn(contact, elements, nextFn, exitFn);
61
+ await info.fn(data.bot, contact, elements, nextFn, exitFn);
62
62
  if (exit) {
63
63
  const plugin = pluginLoader.plugin.get(info.key);
64
64
  logger.debug(`[消息中间件][${plugin.plugin}][${plugin.file}] 主动操作退出`);
@@ -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, PluginMiddlewareInfoType } 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: PluginMiddlewareInfoType;
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
- notFound: [],
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.notFound = lodash.orderBy(this.use.notFound, ['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.notFound = this.use.notFound.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) {
@@ -217,7 +217,7 @@ export class MessageHandler extends EventBaseHandler {
217
217
  * 结束中间件
218
218
  */
219
219
  async endUse() {
220
- for (const info of pluginLoader.use.notFound) {
220
+ for (const info of pluginLoader.use.notFoundMsg) {
221
221
  try {
222
222
  let next = false;
223
223
  let exit = false;
@@ -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,5 +1,4 @@
1
- import { MiddlewareType } from '../../core/karin/karin.js';
2
- import { AllMessageSubType, AllNoticeSubType, AllRequestSubType, KarinMessageType, KarinNoticeType, KarinRequestType, NewMessagePlugin, Permission } 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';
3
2
  export declare const enum AppType {
4
3
  Command = "command",
5
4
  Task = "task",
@@ -8,7 +7,7 @@ export declare const enum AppType {
8
7
  Accept = "accept",
9
8
  Use = "use"
10
9
  }
11
- interface AppInfo {
10
+ export interface AppBase {
12
11
  /** 实现方法 */
13
12
  fn: Function;
14
13
  /** 插件名称 */
@@ -21,7 +20,7 @@ interface AppInfo {
21
20
  log: Function;
22
21
  }
23
22
  /** command规则集类型 */
24
- export interface CommandInfo extends AppInfo {
23
+ export interface CommandInfo extends AppBase {
25
24
  fn: (e: KarinMessageType) => Promise<boolean>;
26
25
  /** 插件执行方法名称 */
27
26
  fnname: string;
@@ -36,7 +35,7 @@ export interface CommandInfo extends AppInfo {
36
35
  data: NewMessagePlugin | '';
37
36
  }
38
37
  /** task规则集类型 */
39
- export interface TaskInfo extends Omit<AppInfo, 'rank'> {
38
+ export interface TaskInfo extends Omit<AppBase, 'rank'> {
40
39
  type: `${AppType.Task}`;
41
40
  /** 任务名称 */
42
41
  fnname: string;
@@ -44,28 +43,106 @@ export interface TaskInfo extends Omit<AppInfo, 'rank'> {
44
43
  cron: string;
45
44
  }
46
45
  /** handler规则集类型 */
47
- export interface HandlerInfo extends Omit<AppInfo, 'log'> {
46
+ export interface HandlerInfo extends Omit<AppBase, 'log'> {
48
47
  type: `${AppType.Handler}`;
49
48
  /** 入口秘钥 */
50
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>;
51
62
  }
52
63
  /** button规则集类型 */
53
- export interface ButtonInfo extends AppInfo {
64
+ export interface ButtonInfo extends Omit<AppBase, 'log'> {
54
65
  type: `${AppType.Button}`;
55
66
  /** 正则 */
56
67
  reg: RegExp;
68
+ fn: (
69
+ /** 是否继续匹配下一个按钮 默认否 调用后则继续 */
70
+ next: () => void,
71
+ /** 消息事件 可能不存在~ */
72
+ e?: KarinMessage,
73
+ /** 自定义参数 */
74
+ ...args: any[]) => Promise<ButtonElement | KeyBoardElement | Array<ButtonElement | KeyBoardElement>>;
57
75
  }
58
76
  /** accept规则集类型 */
59
- export interface AcceptInfo extends AppInfo {
77
+ export interface AcceptInfo extends AppBase {
60
78
  fn: (e: KarinNoticeType | KarinRequestType) => Promise<boolean>;
61
79
  type: `${AppType.Accept}`;
62
80
  /** 监听事件 */
63
81
  event: `${AllNoticeSubType}` | `${AllRequestSubType}`;
64
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>;
65
141
  /** use规则集类型 */
66
- export interface UseInfo extends Omit<AppInfo, 'log'> {
142
+ export interface UseInfo<T extends keyof UseMapType = keyof UseMapType> extends Omit<AppBase, 'log'> {
67
143
  type: `${AppType.Use}`;
68
144
  /** 中间件类型key */
69
- key: `${MiddlewareType}`;
145
+ key: `${T}`;
146
+ /** 中间件实现方法 */
147
+ fn: UseMapType[T][number]['fn'];
70
148
  }
71
- export {};
@@ -1,8 +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';
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';
6
5
  /**
7
6
  * - 插件根目录名称
8
7
  * - 例如: karin-plugin-example
@@ -56,25 +55,25 @@ export interface PluginCommandInfoType {
56
55
  /** 插件基本信息的映射key */
57
56
  key: number;
58
57
  /** 插件包名称 */
59
- name: string;
58
+ name: CommandInfo['name'];
60
59
  /** 插件正则 */
61
- reg: RegExp;
60
+ reg: CommandInfo['reg'];
62
61
  /** 插件执行方法 */
63
- fn: (e: KarinMessageType) => Promise<boolean>;
62
+ fn: CommandInfo['fn'];
64
63
  /** 插件执行方法名称 */
65
- fnname: string;
64
+ fnname: CommandInfo['fnname'];
66
65
  /** 插件类型 */
67
66
  type: `${MethodType}`;
68
67
  /** 在type为class的时候 data为class 否则为空字符串 */
69
68
  data: (() => PluginType) | undefined;
70
69
  /** 插件执行权限 */
71
- perm: `${PermissionType}`;
70
+ perm: CommandInfo['perm'];
72
71
  /** 执行打印日志方法 */
73
- log: Function;
72
+ log: CommandInfo['log'];
74
73
  /** 监听的子事件 高于父事件 */
75
- event: AllMessageSubType;
74
+ event: CommandInfo['event'];
76
75
  /** 优先级 */
77
- rank: number;
76
+ rank: CommandInfo['rank'];
78
77
  }
79
78
  /**
80
79
  * accept规则集信息
@@ -83,15 +82,15 @@ export interface PluginAcceptInfoType {
83
82
  /** 插件基本信息的映射key */
84
83
  key: number;
85
84
  /** 插件包名称 */
86
- name: string;
85
+ name: AcceptInfo['name'];
87
86
  /** 插件执行方法 */
88
- fn: (e: KarinNoticeType | KarinRequestType) => Promise<boolean>;
87
+ fn: AcceptInfo['fn'];
89
88
  /** 优先级 */
90
- rank: number;
89
+ rank: AcceptInfo['rank'];
91
90
  /** 监听事件 */
92
- event: AllNoticeSubType | AllRequestSubType;
91
+ event: AcceptInfo['event'];
93
92
  /** 执行打印日志方法 */
94
- log: Function;
93
+ log: AcceptInfo['log'];
95
94
  }
96
95
  /**
97
96
  * task规则集信息
@@ -121,13 +120,13 @@ export interface PluginButtonInfoType {
121
120
  /** 插件基本信息的映射key */
122
121
  key: number;
123
122
  /** 插件包名称 */
124
- name: string;
123
+ name: ButtonInfo['name'];
125
124
  /** 插件正则 */
126
- reg: RegExp;
125
+ reg: ButtonInfo['reg'];
127
126
  /** 插件执行方法 */
128
- fn: (reject: Function, e?: KarinMessageType) => Promise<any>;
127
+ fn: ButtonInfo['fn'];
129
128
  /** 优先级 */
130
- rank: number;
129
+ rank: ButtonInfo['rank'];
131
130
  }
132
131
  /**
133
132
  * handler规则集信息
@@ -136,109 +135,46 @@ export interface PluginHandlerInfoType {
136
135
  /** 插件基本信息的映射key */
137
136
  key: number;
138
137
  /** 插件包名称 */
139
- name: string;
138
+ name: HandlerInfo['name'];
140
139
  /** handler的处理方法 */
141
- fn: (args: any, reject: (msg?: string) => void) => Promise<any>;
140
+ fn: HandlerInfo['fn'];
142
141
  /** 优先级 */
143
- rank: number;
142
+ rank: HandlerInfo['rank'];
143
+ }
144
+ export interface UseBase<T> {
145
+ /** 插件基本信息的映射key */
146
+ key: number;
147
+ /** 插件包名称 */
148
+ name: UseInfo['name'];
149
+ /** 插件执行方法 */
150
+ fn: T;
151
+ /** 优先级 */
152
+ rank: UseInfo['rank'];
144
153
  }
145
154
  /**
146
- * 中间件规则集信息
155
+ * 中间件类型
147
156
  */
148
- export interface PluginMiddlewareInfoType {
149
- /** 初始化消息前 */
150
- recvMsg: Array<{
151
- /** 插件基本信息的映射key */
152
- key: number;
153
- /** 插件包名称 */
154
- name: string;
155
- /** 插件执行方法 */
156
- fn: (
157
- /** 消息事件方法 */
158
- e: KarinMessageType,
159
- /** 是否继续执行下一个中间件 */
160
- next: Function,
161
- /** 是否退出此条消息 不再执行匹配插件 */
162
- exit: Function) => Promise<boolean>;
163
- /** 优先级 */
164
- rank: number;
165
- }>;
157
+ export declare const enum UseKeyType {
158
+ /** 收到消息后 */
159
+ ReceiveMsg = "recvMsg",
166
160
  /** 回复消息前 */
167
- replyMsg: Array<{
168
- /** 插件基本信息的映射key */
169
- key: number;
170
- /** 插件包名称 */
171
- name: string;
172
- /** 插件执行方法 */
173
- fn: (
174
- /** 消息事件方法 */
175
- e: KarinMessageType,
176
- /** 回复的消息体 */
177
- element: KarinElement[],
178
- /** 是否继续执行下一个中间件 */
179
- next: Function,
180
- /** 是否不发送此条消息 */
181
- exit: Function) => Promise<boolean>;
182
- /** 优先级 */
183
- rank: number;
184
- }>;
161
+ ReplyMsg = "replyMsg",
185
162
  /** 发送主动消息前 */
186
- sendMsg: Array<{
187
- /** 插件基本信息的映射key */
188
- key: number;
189
- /** 插件包名称 */
190
- name: string;
191
- /** 插件执行方法 */
192
- fn: (
193
- /** 发送的bot */
194
- uid: string,
195
- /** 发送目标 */
196
- contact: Contact,
197
- /** 发送的消息体 */
198
- element: KarinElement[],
199
- /** 是否继续执行下一个中间件 */
200
- next: Function,
201
- /** 是否不发送此条消息 */
202
- exit: Function) => Promise<boolean>;
203
- /** 优先级 */
204
- rank: number;
205
- }>;
163
+ SendMsg = "sendMsg",
206
164
  /** 发送合并转发前 */
207
- forwardMsg: Array<{
208
- /** 插件基本信息的映射key */
209
- key: number;
210
- /** 插件包名称 */
211
- name: string;
212
- /** 插件执行方法 */
213
- fn: (
214
- /** 发送的目标信息 */
215
- contact: Contact,
216
- /** 发送的消息体 */
217
- elements: Array<NodeElement>,
218
- /** 是否继续执行下一个中间件 */
219
- next: Function,
220
- /** 是否不发送此条消息 */
221
- exit: Function) => Promise<boolean>;
222
- /** 优先级 */
223
- rank: number;
224
- }>;
165
+ ForwardMsg = "forwardMsg",
225
166
  /** 消息事件没有找到任何匹配的插件触发 */
226
- notFound: Array<{
227
- /** 插件基本信息的映射key */
228
- key: number;
229
- /** 插件包名称 */
230
- name: string;
231
- /** 插件执行方法 */
232
- fn: (
233
- /** 消息事件方法 */
234
- e: KarinMessageType,
235
- /** 是否继续执行下一个中间件 */
236
- next: Function,
237
- /** 是否退出此条消息 不再执行匹配插件 */
238
- exit: Function) => Promise<boolean>;
239
- /** 优先级 */
240
- rank: number;
241
- }>;
167
+ NotFoundMsg = "notFoundMsg"
168
+ }
169
+ /**
170
+ * 中间件映射
171
+ */
172
+ export interface UseMapType {
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>>;
242
178
  }
243
179
  /**
244
180
  * 上下文状态
@@ -278,7 +214,7 @@ export interface PluginRule {
278
214
  /**
279
215
  * 权限
280
216
  */
281
- permission?: PermissionType;
217
+ permission?: `${Permission}`;
282
218
  /**
283
219
  * - 打印日志 默认为true
284
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.9",
3
+ "version": "0.12.11",
4
4
  "private": false,
5
5
  "description": "基于 Kritor 进行开发的nodejs机器人框架",
6
6
  "homepage": "https://github.com/KarinJS/Karin",