node-karin 0.6.11 → 0.6.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.
@@ -119,5 +119,27 @@ export declare class Karin {
119
119
  * @param options - 渲染参数
120
120
  */
121
121
  render(options: KarinRenderType): Promise<string | string[]>;
122
+ /**
123
+ * - 上下文
124
+ * @param e - 消息事件
125
+ */
126
+ ctx(e: KarinMessage, options?: {
127
+ /**
128
+ * - 指定用户id触发下文 不指定则使用默认e.user_id
129
+ */
130
+ userId?: string;
131
+ /**
132
+ * - 超时时间 默认120秒
133
+ */
134
+ time?: number;
135
+ /**
136
+ * - 超时后是否回复
137
+ */
138
+ reply?: boolean;
139
+ /**
140
+ * - 超时回复文本 默认为'操作超时已取消'
141
+ */
142
+ replyMsg?: string;
143
+ }): Promise<KarinMessage>;
122
144
  }
123
145
  export {};
package/lib/core/karin.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import PluginApp from './plugin.app.js'
2
2
  import { common } from '../utils/index.js'
3
3
  import { render } from '../render/index.js'
4
+ import { stateArr } from './plugin.js'
5
+ import { listener } from './listener.js'
4
6
  export class Karin {
5
7
  /**
6
8
  * - 快速构建命令
@@ -117,4 +119,27 @@ export class Karin {
117
119
  render (options) {
118
120
  return render.render(options)
119
121
  }
122
+
123
+ /**
124
+ * - 上下文
125
+ * @param e - 消息事件
126
+ */
127
+ async ctx (e, options) {
128
+ const time = options?.time || 120
129
+ const userId = options?.userId || e.user_id
130
+ const key = e.group_id ? `${e.group_id}.${userId}` : userId
131
+ stateArr[key] = { type: 'ctx' }
132
+ // 返回promise 设置超时时间
133
+ return new Promise((resolve, reject) => {
134
+ setTimeout(() => {
135
+ if (stateArr[key]) {
136
+ delete stateArr[key]
137
+ if (options?.reply) { e.reply(options.replyMsg || '操作超时已取消') }
138
+ reject(new Error('操作超时已取消'))
139
+ return true
140
+ }
141
+ }, time * 1000)
142
+ listener.once(`ctx:${key}`, (e) => resolve(e))
143
+ })
144
+ }
120
145
  }
@@ -1,4 +1,4 @@
1
- import { PluginType, KarinElement, KarinNodeElement, EventType, KarinNoticeEvent, KarinRequestEvent } from '../types/index.js';
1
+ import { PluginType, KarinElement, KarinNodeElement, EventType, KarinNoticeEvent, KarinRequestEvent, stateArrType } from '../types/index.js';
2
2
  /**
3
3
  * 插件基类
4
4
  */
@@ -142,7 +142,7 @@ export declare class Plugin implements PluginType {
142
142
  /**
143
143
  * @param fnc - 执行方法
144
144
  */
145
- fnc: string,
145
+ fnc: string | Function,
146
146
  /**
147
147
  * @param reply - 超时后是否回复
148
148
  */
@@ -154,10 +154,7 @@ export declare class Plugin implements PluginType {
154
154
  /**
155
155
  * 获取上下文状态
156
156
  */
157
- getContext(): {
158
- plugin: Plugin;
159
- fnc: string;
160
- };
157
+ getContext(): stateArrType[string];
161
158
  /**
162
159
  * 清除上下文状态
163
160
  */
@@ -166,18 +163,7 @@ export declare class Plugin implements PluginType {
166
163
  /**
167
164
  * 上下文状态
168
165
  */
169
- export declare const stateArr: {
170
- [key: string]: {
171
- /**
172
- * @param plugin - 插件实例
173
- */
174
- plugin: Plugin;
175
- /**
176
- * @param fnc - 执行方法名称
177
- */
178
- fnc: string;
179
- };
180
- };
166
+ export declare const stateArr: stateArrType;
181
167
  /**
182
168
  * 通知事件 插件类型
183
169
  */
@@ -103,7 +103,11 @@ export class Plugin {
103
103
  */
104
104
  time = 120) {
105
105
  const key = this.conKey()
106
- stateArr[key] = { plugin: this, fnc }
106
+ if (typeof fnc === 'string') {
107
+ stateArr[key] = { type: 'class', fnc: this, name: fnc }
108
+ } else {
109
+ stateArr[key] = { type: 'fnc', fnc }
110
+ }
107
111
  /** 操作时间 */
108
112
  this.timeout = setTimeout(() => {
109
113
  if (stateArr[key]) {
@@ -219,14 +219,33 @@ export class MessageHandler extends EventHandler {
219
219
  const key = this.e.isGroup ? `${this.e.group_id}.${this.e.user_id}` : this.e.user_id
220
220
  const App = stateArr[key]
221
221
  if (App) {
222
- const { plugin, fnc } = App
223
- this.e.logFnc = `[${plugin.name}][${fnc}]`
224
- /** 计算插件处理时间 */
225
- const start = Date.now()
226
- plugin.e = this.e
227
- await plugin[fnc]()
228
- logger.bot('mark', this.e.self_id, `${this.e.logFnc} 上下文处理完成 ${Date.now() - start}ms`)
229
- return true
222
+ switch (App.type) {
223
+ case 'ctx': {
224
+ listener.emit(`ctx:${key}`, this.e)
225
+ delete stateArr[key]
226
+ return true
227
+ }
228
+ case 'class': {
229
+ const { fnc, name } = App
230
+ this.e.logFnc = `[${fnc.name}][${name}]`
231
+ /** 计算插件处理时间 */
232
+ const start = Date.now()
233
+ fnc.e = this.e
234
+ await fnc[name]()
235
+ logger.bot('mark', this.e.self_id, `${this.e.logFnc} 上下文处理完成 ${Date.now() - start}ms`)
236
+ return true
237
+ }
238
+ case 'fnc': {
239
+ const { fnc } = App
240
+ this.e.logFnc = `[${fnc.name}]`
241
+ /** 计算插件处理时间 */
242
+ const start = Date.now()
243
+ await fnc(this.e)
244
+ logger.bot('mark', this.e.self_id, `${this.e.logFnc} 上下文处理完成 ${Date.now() - start}ms`)
245
+ delete stateArr[key]
246
+ return true
247
+ }
248
+ }
230
249
  }
231
250
  return false
232
251
  }
@@ -1,6 +1,7 @@
1
1
  import schedule from 'node-schedule';
2
2
  import { Reply, replyCallback, replyForward } from './reply.js';
3
3
  import { EventType, Event, Permission, SubEvent, KarinMessageEvent, KarinNoticeEvent, KarinRequestEvent } from './event.js';
4
+ import { Plugin } from '../core/index.js';
4
5
  /**
5
6
  * - 插件根目录名称
6
7
  * - 例如: karin-plugin-example
@@ -12,6 +13,21 @@ export type dirName = `karin-plugin-${string}`;
12
13
  * - 例如: index.js index.ts
13
14
  */
14
15
  export type fileName = `${string}.js` | `${string}.ts`;
16
+ /**
17
+ * 上下文状态
18
+ */
19
+ export interface stateArrType {
20
+ [key: string]: {
21
+ type: 'fnc';
22
+ fnc: Function;
23
+ } | {
24
+ type: 'class';
25
+ fnc: Plugin;
26
+ name: string;
27
+ } | {
28
+ type: 'ctx';
29
+ };
30
+ }
15
31
  /**
16
32
  * - 插件规则
17
33
  */
@@ -198,30 +214,12 @@ export interface PluginType {
198
214
  /**
199
215
  * - 获取上下文状态
200
216
  */
201
- getContext: () => {
202
- /**
203
- * - 插件实例
204
- */
205
- plugin: PluginType;
206
- /**
207
- * - 执行方法名称
208
- */
209
- fnc: string;
210
- };
217
+ getContext: () => stateArrType[string];
211
218
  /**
212
219
  * - accept标准方法 给通知、请求事件使用
213
220
  */
214
221
  accept?(e: EventType<this>): Promise<void>;
215
222
  }
216
- /**
217
- * 上下文状态
218
- */
219
- export interface stateArrType {
220
- [key: string]: {
221
- plugin: PluginType;
222
- fnc: string;
223
- };
224
- }
225
223
  /**
226
224
  * - Apps
227
225
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-karin",
3
- "version": "0.6.11",
3
+ "version": "0.6.12",
4
4
  "private": false,
5
5
  "description": "基于 Kritor 进行开发的nodejs机器人框架",
6
6
  "homepage": "https://github.com/KarinJS/Karin",