node-karin 0.8.12 → 0.8.14

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.
@@ -48,10 +48,14 @@ export class AdapterInput {
48
48
  fs.readdirSync('./temp/input').forEach((file) => {
49
49
  fs.unlinkSync(`./temp/input/${file}`);
50
50
  });
51
- /** 注册bot */
52
- const index = listener.addBot({ bot: this, type: this.adapter.type });
53
- if (index)
54
- this.adapter.index = index;
51
+ // 等1秒
52
+ common.sleep(1000)
53
+ .then(() => {
54
+ /** 注册bot */
55
+ const index = listener.addBot({ bot: this, type: this.adapter.type });
56
+ if (index)
57
+ this.adapter.index = index;
58
+ });
55
59
  return this;
56
60
  }
57
61
  init() {
package/lib/cli/karin.js CHANGED
@@ -77,7 +77,7 @@ class KarinCli {
77
77
  }
78
78
  }
79
79
  /** 启动 */
80
- this.child = spawn(runner, cmd, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], cwd: process.cwd(), env: process.env, shell: true })
80
+ this.child = spawn(runner, cmd, { stdio: ['inherit', 'inherit', 'inherit', 'ipc'], cwd: process.cwd(), env: process.env })
81
81
  /** 监听退出 */
82
82
  this.child.once('exit', (code) => process.exit(code))
83
83
  /** 监听子进程消息 */
@@ -272,4 +272,5 @@ program.command('dev').description('JavaScript开发模式').action(() => cli.st
272
272
  program.command('ts').description('TypeScript开发模式').action(() => cli.start('dev' /* Mode.Dev */, 'ts' /* Lang.Ts */, 'tsx' /* Runner.Tsx */))
273
273
  program.command('log').description('查看日志').action(() => cli.log())
274
274
  program.command('up').description('更新依赖').action(() => cli.update())
275
+ program.command('init').description('初始化karin').action(() => { import('./init.js') })
275
276
  program.parse(process.argv)
@@ -1,4 +1,4 @@
1
- import { KarinMessage, PluginApps, KarinElement, Contact, KarinRenderType, PermissionType } from '../types/index.js';
1
+ import { Contact, PluginApps, KarinElement, KarinMessage, KarinRenderType, PermissionType, RenderResult, NoticeListenEvent, RequestListenEvent, KarinNoticeType, KarinRequestType } from '../types/index.js';
2
2
  type FncFunction = (e: KarinMessage) => Promise<boolean>;
3
3
  type FncElement = string | KarinElement | Array<KarinElement>;
4
4
  export interface OptionsCommand {
@@ -116,13 +116,24 @@ export declare class Karin {
116
116
  */
117
117
  contact(peer: string, isGroup?: boolean, sub_peer?: string): Contact;
118
118
  /**
119
- * - 渲染
119
+ * 快速渲染
120
+ * @param file - 文件路径、http地址
121
+ */
122
+ render(file: string): Promise<string>;
123
+ /**
124
+ * 分片渲染
125
+ * @param options - 渲染参数
126
+ */
127
+ render(file: string, multiPage: number | true): Promise<Array<string>>;
128
+ /**
129
+ * 正常渲染
120
130
  * @param options - 渲染参数
121
131
  */
122
- render(options: KarinRenderType): Promise<string>;
132
+ render<T extends KarinRenderType>(options: T): Promise<RenderResult<T>>;
123
133
  /**
124
- * - 上下文
134
+ * 上下文
125
135
  * @param e - 消息事件
136
+ * @param options - 上下文选项
126
137
  */
127
138
  ctx(e: KarinMessage, options?: {
128
139
  /**
@@ -142,6 +153,28 @@ export declare class Karin {
142
153
  */
143
154
  replyMsg?: string;
144
155
  }): Promise<KarinMessage>;
156
+ /**
157
+ * accept
158
+ * @param event - 监听事件
159
+ * @param fnc - 实现函数
160
+ */
161
+ accept(event: NoticeListenEvent | RequestListenEvent, fnc: (e: KarinNoticeType | KarinRequestType) => boolean, options?: {
162
+ /**
163
+ * - 插件名称
164
+ */
165
+ name?: string;
166
+ /**
167
+ * - 优先级
168
+ */
169
+ priority?: number;
170
+ /**
171
+ * - 触发函数是否打印日志
172
+ */
173
+ log?: boolean;
174
+ }): PluginApps;
175
+ /**
176
+ * - 启动
177
+ */
145
178
  run(): void;
146
179
  }
147
180
  export declare const karin: Karin;
package/lib/core/karin.js CHANGED
@@ -121,15 +121,27 @@ export class Karin {
121
121
 
122
122
  /**
123
123
  * - 渲染
124
- * @param options - 渲染参数
124
+ * @param data - 渲染参数
125
+ * @param multiPage - 分片高度
126
+ * @returns - 返回图片base64或数组
125
127
  */
126
- render (options) {
127
- return render.render(options)
128
+ render (data, multiPage) {
129
+ if (typeof data === 'string') {
130
+ /** 分片 */
131
+ if (typeof multiPage === 'number' || multiPage === true) {
132
+ return render.renderMultiHtml(data, multiPage)
133
+ }
134
+ /** 快速渲染 */
135
+ return render.renderHtml(data)
136
+ }
137
+ /** 正常渲染 */
138
+ return render.render(data)
128
139
  }
129
140
 
130
141
  /**
131
- * - 上下文
142
+ * 上下文
132
143
  * @param e - 消息事件
144
+ * @param options - 上下文选项
133
145
  */
134
146
  async ctx (e, options) {
135
147
  const time = options?.time || 120
@@ -150,6 +162,25 @@ export class Karin {
150
162
  })
151
163
  }
152
164
 
165
+ /**
166
+ * accept
167
+ * @param event - 监听事件
168
+ * @param fnc - 实现函数
169
+ */
170
+ accept (event, fnc, options) {
171
+ const data = {
172
+ name: options?.name || 'accept',
173
+ priority: options?.priority,
174
+ event,
175
+ accept: fnc,
176
+ log: options?.log,
177
+ }
178
+ return PluginApp(data)
179
+ }
180
+
181
+ /**
182
+ * - 启动
183
+ */
153
184
  run () {
154
185
  if (this.start) { return }
155
186
  this.start = true
@@ -4,6 +4,7 @@ import { KarinAdapter, Contact, KarinElement } from '../types/index.js';
4
4
  * 监听器管理
5
5
  */
6
6
  declare class Listeners extends EventEmitter {
7
+ #private;
7
8
  /**
8
9
  * Bot索引
9
10
  * @type - Bot索引
@@ -93,7 +94,7 @@ declare class Listeners extends EventEmitter {
93
94
  * @param options.recallMsg - 发送成功后撤回消息时间
94
95
  * @param options.retry_count - 重试次数
95
96
  */
96
- sendMsg(uid: string, contact: Contact, elements: KarinElement, options?: {
97
+ sendMsg(uid: string, contact: Contact, elements: Array<KarinElement>, options?: {
97
98
  recallMsg?: number;
98
99
  retry_count?: number;
99
100
  }): Promise<{
@@ -1,9 +1,10 @@
1
1
  import { EventEmitter } from 'events'
2
2
  import { pluginLoader } from './plugin.loader.js'
3
- import { common, logger, config } from '../utils/index.js'
3
+ import { common, logger, config, segment } from '../utils/index.js'
4
4
  import NoticeHandler from '../event/notice.handler.js'
5
5
  import RequestHandler from '../event/request.handler.js'
6
6
  import { MessageHandler } from '../event/message.handler.js'
7
+ import { level } from '../db/index.js'
7
8
  /**
8
9
  * 监听器管理
9
10
  */
@@ -51,11 +52,36 @@ class Listeners extends EventEmitter {
51
52
  }
52
53
  this.list.push({ index, type: data.type, bot: data.bot })
53
54
  logger.info(`[机器人][注册][${data.type}] ` + logger.green(`[account:${data.bot.account.uid || data.bot.account.uin}(${data.bot.account.name})]`))
54
- this.emit('karin:online', data.bot.account.uid || data.bot.account.uin)
55
+ this.#online(data.bot.account.uid || data.bot.account.uin)
55
56
  logger.debug('注册', this.list)
56
57
  return index
57
58
  }
58
59
 
60
+ /**
61
+ * 发送上线通知
62
+ */
63
+ async #online (uid) {
64
+ /** 重启 */
65
+ const key = `karin:restart:${uid}`
66
+ const options = await level.get(key)
67
+ if (!options) { return }
68
+ const { id, contact, time, message_id } = options
69
+ /** 重启花费时间 保留2位小数 */
70
+ const restartTime = ((Date.now() - time) / 1000).toFixed(2)
71
+ /** 超过2分钟不发 */
72
+ if (Number(restartTime) > 120) {
73
+ await level.del(key)
74
+ return false
75
+ }
76
+ const element = [
77
+ segment.reply(message_id),
78
+ segment.text(`Karin 重启成功:${restartTime}秒`),
79
+ ]
80
+ await this.sendMsg(id, contact, element)
81
+ await level.del(key)
82
+ return true
83
+ }
84
+
59
85
  /**
60
86
  * 卸载Bot
61
87
  * @param index - Bot的索引id
@@ -10,6 +10,7 @@ export interface PluginAppType {
10
10
  event?: PluginApps['event'];
11
11
  priority?: number;
12
12
  accept?: boolean | Function;
13
+ log?: boolean;
13
14
  rule?: PluginApps['rule'];
14
15
  task?: PluginApps['task'];
15
16
  handler?: PluginApps['handler'];
@@ -10,6 +10,7 @@ export default function PluginApp (options) {
10
10
  event: options.event || 'message' /* EventType.Message */,
11
11
  priority: options.priority || 10000,
12
12
  accept: options.accept ?? false,
13
+ log: options.log ?? true,
13
14
  rule: options.rule || [],
14
15
  task: options.task || [],
15
16
  handler: options.handler || [],
@@ -1,4 +1,4 @@
1
- import { PluginType, KarinElement, NodeElement, stateArrType, KarinNoticeType, KarinRequestType, KarinEventTypes, ReplyReturn } from '../types/index.js';
1
+ import { PluginType, KarinElement, NodeElement, stateArrType, KarinNoticeType, KarinRequestType, ReplyReturn, EType } from '../types/index.js';
2
2
  /**
3
3
  * 插件基类
4
4
  */
@@ -150,7 +150,7 @@ export declare class Plugin implements PluginType {
150
150
  * 清除上下文状态
151
151
  */
152
152
  finish(): void;
153
- e: KarinEventTypes;
153
+ e: EType<this>;
154
154
  replyCallback: PluginType['replyCallback'];
155
155
  }
156
156
  /**
@@ -340,9 +340,7 @@ class PluginLoader {
340
340
  try {
341
341
  const keys = Object.keys(this.dependErr)
342
342
  if (!keys.length) { return }
343
- const msg = [
344
- '-----依赖缺失----',
345
- ]
343
+ const msg = ['-----依赖缺失----']
346
344
  keys.forEach(key => {
347
345
  const { dir, name, depend } = this.dependErr[key]
348
346
  msg.push(`[${dir}][${name}] 缺少依赖:${logger.red(depend)}`)
@@ -21,5 +21,5 @@ export declare const server: {
21
21
  */
22
22
  staticPath(): Promise<void>;
23
23
  /** 重启当前HTTP服务器 */
24
- "__#14@#restartServer"(): Promise<void>;
24
+ "__#15@#restartServer"(): Promise<void>;
25
25
  };
@@ -1,6 +1,6 @@
1
1
  import WebSocket from 'ws';
2
2
  import { RenderBase } from './base.js';
3
- import { KarinRenderType } from '../types/render.js';
3
+ import { KarinRenderType, RenderResult } from '../types/render.js';
4
4
  export declare class RenderClient extends RenderBase {
5
5
  url: string;
6
6
  type: string;
@@ -26,5 +26,5 @@ export declare class RenderClient extends RenderBase {
26
26
  * 渲染标准方法
27
27
  * @param options 渲染参数
28
28
  */
29
- async<T extends KarinRenderType>(options: T): "" | Promise<unknown>;
29
+ render<T extends KarinRenderType>(options: T): Promise<RenderResult<T>>;
30
30
  }
@@ -118,7 +118,7 @@ export class RenderClient extends RenderBase {
118
118
  * 渲染标准方法
119
119
  * @param options 渲染参数
120
120
  */
121
- async (options) {
121
+ async render (options) {
122
122
  /** 渲染模板 */
123
123
  let file = options.file
124
124
  let action = 'renderHtml'
@@ -239,10 +239,22 @@ export declare const enum CombinedEventType {
239
239
  */
240
240
  RequestInvitedGroup = "request.invited_group"
241
241
  }
242
+ /**
243
+ * - 消息监听事件
244
+ */
245
+ export type MessageListenEvent = 'message' | 'message.group_message' | 'message.private_message' | 'message.guild_message' | 'message.nearby' | 'message.stranger';
246
+ /**
247
+ * - 通知监听事件
248
+ */
249
+ export type NoticeListenEvent = 'notice' | 'notice.private_poke' | 'notice.private_recall' | 'notice.private_file_uploaded' | 'notice.group_poke' | 'notice.group_card_changed' | 'notice.group_member_unique_title_changed' | 'notice.group_essence_changed' | 'notice.group_recall' | 'notice.group_member_increase' | 'notice.group_member_decrease' | 'notice.group_admin_changed' | 'notice.group_member_ban' | 'notice.group_sign_in' | 'notice.group_whole_ban' | 'notice.group_file_uploaded' | 'notice.group_message_reaction';
250
+ /**
251
+ * - 请求监听事件
252
+ */
253
+ export type RequestListenEvent = 'request' | 'request.private_apply' | 'request.group_apply' | 'request.invited_group';
242
254
  /**
243
255
  * 所有监听事件
244
256
  */
245
- export type AllListenEvent = 'message' | 'notice' | 'request' | 'message.group_message' | 'message.private_message' | 'message.guild_message' | 'message.nearby' | 'message.stranger' | 'notice.private_poke' | 'notice.private_recall' | 'notice.private_file_uploaded' | 'notice.group_poke' | 'notice.group_card_changed' | 'notice.group_member_unique_title_changed' | 'notice.group_essence_changed' | 'notice.group_recall' | 'notice.group_member_increase' | 'notice.group_member_decrease' | 'notice.group_admin_changed' | 'notice.group_member_ban' | 'notice.group_sign_in' | 'notice.group_whole_ban' | 'notice.group_file_uploaded' | 'notice.group_message_reaction' | 'request.private_apply' | 'request.group_apply' | 'request.invited_group';
257
+ export type AllListenEvent = MessageListenEvent | NoticeListenEvent | RequestListenEvent;
246
258
  /**
247
259
  * 事件基类定义
248
260
  */
@@ -505,7 +505,7 @@ export declare class KarinNotice implements KarinNoticeEventBase {
505
505
  replyCallback: KarinNoticeType['replyCallback'];
506
506
  bot: KarinNoticeType['bot'];
507
507
  content: KarinNoticeType['content'];
508
- constructor({ event_id, self_id, user_id, group_id, time, contact, sender, sub_event, content, }: BaseEventDataType & {
508
+ constructor({ event_id, self_id, user_id, group_id, time, contact, sender, sub_event, content, raw_event, }: BaseEventDataType & {
509
509
  sub_event: KarinNoticeType['sub_event'];
510
510
  content: KarinNoticeType['content'];
511
511
  });
@@ -26,7 +26,8 @@ export class KarinNotice {
26
26
  replyCallback;
27
27
  bot;
28
28
  content;
29
- constructor({ event_id, self_id, user_id, group_id = '', time, contact, sender, sub_event, content, }) {
29
+ constructor({ event_id, self_id, user_id, group_id = '', time, contact, sender, sub_event, content, raw_event, }) {
30
+ this.raw_event = raw_event;
30
31
  this.self_id = self_id;
31
32
  this.user_id = user_id;
32
33
  this.group_id = contact.scene === 'group' ? (contact.peer || group_id) : group_id;
@@ -115,7 +115,7 @@ export declare class KarinRequest implements KarinRequestEventBase {
115
115
  bot: KarinRequestType['bot'];
116
116
  content: RequestType[RequestSubType];
117
117
  raw_event: KarinRequestType['raw_event'];
118
- constructor({ self_id, user_id, group_id, time, contact, sender, sub_event, event_id, content, }: BaseEventDataType & {
118
+ constructor({ self_id, user_id, group_id, time, contact, sender, sub_event, event_id, content, raw_event, }: BaseEventDataType & {
119
119
  sub_event: KarinRequestType['sub_event'];
120
120
  /**
121
121
  * - 事件对应的内容参数
@@ -26,7 +26,8 @@ export class KarinRequest {
26
26
  bot;
27
27
  content;
28
28
  raw_event;
29
- constructor({ self_id, user_id, group_id = '', time, contact, sender, sub_event, event_id, content, }) {
29
+ constructor({ self_id, user_id, group_id = '', time, contact, sender, sub_event, event_id, content, raw_event, }) {
30
+ this.raw_event = raw_event;
30
31
  this.self_id = self_id;
31
32
  this.user_id = user_id;
32
33
  this.group_id = contact.scene === 'group' ? (contact.peer || group_id) : group_id;
@@ -1,7 +1,7 @@
1
1
  import schedule from 'node-schedule';
2
2
  import { Plugin } from '../core/index.js';
3
3
  import { Reply, replyCallback, replyForward } from './event/reply.js';
4
- import { KarinNoticeType, KarinRequestType, AllListenEvent, KarinEventTypes, KarinMessageType, PermissionType } from './event/index.js';
4
+ import { KarinNoticeType, KarinRequestType, AllListenEvent, KarinMessageType, PermissionType } from './event/index.js';
5
5
  /**
6
6
  * - 插件根目录名称
7
7
  * - 例如: karin-plugin-example
@@ -127,6 +127,12 @@ export interface AppInfo {
127
127
  */
128
128
  name: fileName;
129
129
  }
130
+ /**
131
+ * - 定义一个条件类型,根据是否存在accept方法来决定类型
132
+ */
133
+ export type EType<T> = T extends {
134
+ accept: (e: KarinNoticeType | KarinRequestType) => Promise<void>;
135
+ } ? KarinNoticeType | KarinRequestType : KarinMessageType;
130
136
  /**
131
137
  * - 插件基类
132
138
  */
@@ -178,9 +184,8 @@ export interface PluginType {
178
184
  timeout: NodeJS.Timeout | undefined;
179
185
  /**
180
186
  * - 上报事件
181
- * - 根据上报中的event字段来获取e的事件类型
182
187
  */
183
- e: KarinEventTypes;
188
+ e: EType<this>;
184
189
  init?: () => Promise<any>;
185
190
  /**
186
191
  * - 快速回复
@@ -254,9 +259,13 @@ export interface PluginApps {
254
259
  */
255
260
  priority: number;
256
261
  /**
257
- * - accept函数存在
262
+ * - accept函数
258
263
  */
259
264
  accept: boolean | Function;
265
+ /**
266
+ * - accept函数触发是否打印日志
267
+ */
268
+ log: boolean;
260
269
  /**
261
270
  * - 命令规则
262
271
  */
@@ -1,4 +1,4 @@
1
- import { PluginType, PluginApps, KarinEventTypes } from '../types/index.js';
1
+ import { PluginType, PluginApps } from '../types/index.js';
2
2
  /**
3
3
  * 事件处理器类
4
4
  */
@@ -23,7 +23,7 @@ export declare const handler: {
23
23
  */
24
24
  call(key: string, args: {
25
25
  [key: string]: any;
26
- e?: KarinEventTypes;
26
+ e?: any;
27
27
  }): Promise<any>;
28
28
  /**
29
29
  * 检查是否存在指定键的事件处理器
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-karin",
3
- "version": "0.8.12",
3
+ "version": "0.8.14",
4
4
  "private": false,
5
5
  "description": "基于 Kritor 进行开发的nodejs机器人框架",
6
6
  "homepage": "https://github.com/KarinJS/Karin",