node-karin 0.12.8 → 0.12.10

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.
@@ -1,21 +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, PermissionType, RenderResult, KarinNoticeType, KarinRequestType, AllMessageSubType, CommandInfo, TaskInfo, HandlerInfo, AcceptInfo, UseInfo, AllNoticeSubType, AllRequestSubType, UseMapType } 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
- /**
9
- * 中间件类型
10
- */
11
- export declare const enum MiddlewareType {
12
- /** 收到消息后 */
13
- ReceiveMsg = "recvMsg",
14
- /** 回复消息前 */
15
- ReplyMsg = "replyMsg",
16
- /** 发送主动消息前 */
17
- SendMsg = "sendMsg"
18
- }
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];
19
6
  export interface Options {
20
7
  /**
21
8
  * - 插件名称 不传则使用 插件名称:函数名
@@ -178,9 +165,13 @@ export declare class Karin extends Listeners {
178
165
  * @param fn - 实现函数
179
166
  */
180
167
  accept(event: AllNoticeSubType | AllRequestSubType, fn: (e: KarinNoticeType | KarinRequestType) => Promise<boolean>, options?: Options): AcceptInfo;
181
- use(type: `${MiddlewareType.ReceiveMsg}`, fn: UseReceive, options?: Omit<Options, 'log'>): UseInfo;
182
- use(type: `${MiddlewareType.ReplyMsg}`, fn: UseReply, options?: Omit<Options, 'log'>): UseInfo;
183
- use(type: `${MiddlewareType.SendMsg}`, fn: UseRecord, options?: Omit<Options, 'log'>): UseInfo;
168
+ /**
169
+ * 中间件
170
+ * @param type 中间件类型
171
+ * @param fn 中间件函数
172
+ * @param options 选项配置
173
+ */
174
+ use<T extends keyof UseMapType>(type: `${T}`, fn: UseMapType[T][number]['fn'], options?: Omit<Options, 'log'>): UseInfo;
184
175
  /**
185
176
  * - 启动
186
177
  */
@@ -191,6 +191,7 @@ export class Karin extends Listeners {
191
191
  * 中间件
192
192
  * @param type 中间件类型
193
193
  * @param fn 中间件函数
194
+ * @param options 选项配置
194
195
  */
195
196
  use(type, fn, options) {
196
197
  return {
@@ -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, PluginInfoType, PluginTaskInfoType, PluginButtonInfoType, PluginAcceptInfoType, PluginHandlerInfoType, PluginCommandInfoType, UseValueType } 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: UseValueType;
63
63
  /** 加载的文件数组 .js .ts */
64
64
  ext: string[];
65
65
  constructor();
@@ -56,7 +56,7 @@ class PluginLoader {
56
56
  replyMsg: [],
57
57
  sendMsg: [],
58
58
  forwardMsg: [],
59
- notFound: [],
59
+ notFoundMsg: [],
60
60
  };
61
61
  this.ext = process.env.karin_app_lang === 'ts' ? ['.js', '.ts'] : ['.js'];
62
62
  this.index = 0;
@@ -292,7 +292,7 @@ class PluginLoader {
292
292
  this.use.replyMsg = lodash.orderBy(this.use.replyMsg, ['rank'], ['asc']);
293
293
  this.use.sendMsg = lodash.orderBy(this.use.sendMsg, ['rank'], ['asc']);
294
294
  this.use.forwardMsg = lodash.orderBy(this.use.forwardMsg, ['rank'], ['asc']);
295
- this.use.notFound = lodash.orderBy(this.use.notFound, ['rank'], ['asc']);
295
+ this.use.notFoundMsg = lodash.orderBy(this.use.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']);
@@ -556,7 +556,7 @@ class PluginLoader {
556
556
  this.use.replyMsg = this.use.replyMsg.filter(val => val.key !== key);
557
557
  this.use.sendMsg = this.use.sendMsg.filter(val => val.key !== key);
558
558
  this.use.forwardMsg = this.use.forwardMsg.filter(val => val.key !== key);
559
- this.use.notFound = this.use.notFound.filter(val => val.key !== key);
559
+ this.use.notFoundMsg = this.use.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;
@@ -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, KarinMessageType, KarinNoticeType, KarinRequestType, NewMessagePlugin, Permission, UseMapType } from '../../types/index.js';
3
2
  export declare const enum AppType {
4
3
  Command = "command",
5
4
  Task = "task",
@@ -66,6 +65,6 @@ export interface AcceptInfo extends AppInfo {
66
65
  export interface UseInfo extends Omit<AppInfo, 'log'> {
67
66
  type: `${AppType.Use}`;
68
67
  /** 中间件类型key */
69
- key: `${MiddlewareType}`;
68
+ key: `${keyof UseMapType}`;
70
69
  }
71
70
  export {};
@@ -3,6 +3,7 @@ import { Plugin } from '../../core/index.js';
3
3
  import { Reply, replyCallback, replyForward } from '../event/reply.js';
4
4
  import { KarinNoticeType, KarinRequestType, AllListenEvent, KarinMessageType, PermissionType, AllMessageSubType, Contact, AllNoticeSubType, AllRequestSubType } from '../event/index.js';
5
5
  import { KarinElement, NodeElement } from '../element/element.js';
6
+ import { KarinAdapter } from '../adapter/base.js';
6
7
  /**
7
8
  * - 插件根目录名称
8
9
  * - 例如: karin-plugin-example
@@ -145,7 +146,7 @@ export interface PluginHandlerInfoType {
145
146
  /**
146
147
  * 中间件规则集信息
147
148
  */
148
- export interface PluginMiddlewareInfoType {
149
+ export interface UseValueType {
149
150
  /** 初始化消息前 */
150
151
  recvMsg: Array<{
151
152
  /** 插件基本信息的映射key */
@@ -210,7 +211,7 @@ export interface PluginMiddlewareInfoType {
210
211
  /** 插件包名称 */
211
212
  name: string;
212
213
  /** 插件执行方法 */
213
- fn: (
214
+ fn: (bot: KarinAdapter,
214
215
  /** 发送的目标信息 */
215
216
  contact: Contact,
216
217
  /** 发送的消息体 */
@@ -223,7 +224,7 @@ export interface PluginMiddlewareInfoType {
223
224
  rank: number;
224
225
  }>;
225
226
  /** 消息事件没有找到任何匹配的插件触发 */
226
- notFound: Array<{
227
+ notFoundMsg: Array<{
227
228
  /** 插件基本信息的映射key */
228
229
  key: number;
229
230
  /** 插件包名称 */
@@ -240,6 +241,31 @@ export interface PluginMiddlewareInfoType {
240
241
  rank: number;
241
242
  }>;
242
243
  }
244
+ /**
245
+ * 中间件类型
246
+ */
247
+ export declare const enum UseKeyType {
248
+ /** 收到消息后 */
249
+ ReceiveMsg = "recvMsg",
250
+ /** 回复消息前 */
251
+ ReplyMsg = "replyMsg",
252
+ /** 发送主动消息前 */
253
+ SendMsg = "sendMsg",
254
+ /** 发送合并转发前 */
255
+ ForwardMsg = "forwardMsg",
256
+ /** 消息事件没有找到任何匹配的插件触发 */
257
+ NotFoundMsg = "notFoundMsg"
258
+ }
259
+ /**
260
+ * 中间件映射
261
+ */
262
+ 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'];
268
+ }
243
269
  /**
244
270
  * 上下文状态
245
271
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-karin",
3
- "version": "0.12.8",
3
+ "version": "0.12.10",
4
4
  "private": false,
5
5
  "description": "基于 Kritor 进行开发的nodejs机器人框架",
6
6
  "homepage": "https://github.com/KarinJS/Karin",