node-karin 0.6.17 → 0.6.19

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.
Files changed (101) hide show
  1. package/lib/adapter/index.d.ts +1 -1
  2. package/lib/adapter/index.js +1 -1
  3. package/lib/adapter/input/index.d.ts +16 -3
  4. package/lib/adapter/input/index.js +52 -38
  5. package/lib/adapter/kritor/grpc.d.ts +3 -2
  6. package/lib/adapter/kritor/grpc.js +80 -64
  7. package/lib/adapter/kritor/index.d.ts +38 -18
  8. package/lib/adapter/kritor/index.js +30 -11
  9. package/lib/adapter/onebot/11/convert.d.ts +11 -0
  10. package/lib/adapter/onebot/11/convert.js +168 -0
  11. package/lib/adapter/onebot/11/event.d.ts +25 -0
  12. package/lib/adapter/onebot/11/event.js +425 -0
  13. package/lib/adapter/onebot/11/index.d.ts +506 -0
  14. package/lib/adapter/onebot/11/index.js +1047 -0
  15. package/lib/cli/init.js +5 -0
  16. package/lib/core/karin.d.ts +3 -4
  17. package/lib/core/karin.js +3 -2
  18. package/lib/core/listener.d.ts +2 -2
  19. package/lib/core/listener.js +2 -2
  20. package/lib/core/plugin.app.js +2 -1
  21. package/lib/core/plugin.d.ts +4 -4
  22. package/lib/core/plugin.js +2 -1
  23. package/lib/core/plugin.loader.d.ts +3 -3
  24. package/lib/core/plugin.loader.js +2 -1
  25. package/lib/core/server.js +3 -3
  26. package/lib/event/event.handler.d.ts +5 -5
  27. package/lib/event/event.handler.js +6 -5
  28. package/lib/event/index.d.ts +0 -4
  29. package/lib/event/index.js +0 -4
  30. package/lib/event/message.handler.d.ts +3 -3
  31. package/lib/event/notice.handler.d.ts +3 -3
  32. package/lib/event/notice.handler.js +17 -16
  33. package/lib/event/request.handler.d.ts +3 -3
  34. package/lib/event/request.handler.js +4 -3
  35. package/lib/event/review.handler.d.ts +7 -8
  36. package/lib/index.js +1 -1
  37. package/lib/types/adapter/adapter.d.ts +564 -0
  38. package/lib/types/adapter/adapter.js +1 -0
  39. package/lib/types/{api.d.ts → adapter/api.d.ts} +103 -3
  40. package/lib/types/adapter/api.js +1 -0
  41. package/lib/types/element.d.ts +5 -4
  42. package/lib/types/event/contact.d.ts +28 -0
  43. package/lib/types/event/contact.js +13 -0
  44. package/lib/types/event/event.d.ts +390 -0
  45. package/lib/types/event/event.js +234 -0
  46. package/lib/types/event/index.d.ts +12 -0
  47. package/lib/types/event/index.js +7 -0
  48. package/lib/types/event/message.d.ts +104 -0
  49. package/lib/types/event/message.js +72 -0
  50. package/lib/types/event/notice.d.ts +513 -0
  51. package/lib/types/event/notice.js +52 -0
  52. package/lib/types/{reply.d.ts → event/reply.d.ts} +2 -2
  53. package/lib/types/event/reply.js +1 -0
  54. package/lib/types/event/request.d.ts +126 -0
  55. package/lib/types/event/request.js +52 -0
  56. package/lib/types/event/sender.d.ts +44 -0
  57. package/lib/types/event/sender.js +22 -0
  58. package/lib/types/index.d.ts +6 -5
  59. package/lib/types/index.js +6 -5
  60. package/lib/types/onebot11/api.d.ts +53 -0
  61. package/lib/types/onebot11/api.js +54 -0
  62. package/lib/types/onebot11/event.d.ts +619 -0
  63. package/lib/types/onebot11/event.js +56 -0
  64. package/lib/types/onebot11/index.d.ts +6 -0
  65. package/lib/types/onebot11/index.js +6 -0
  66. package/lib/types/onebot11/params.d.ts +609 -0
  67. package/lib/types/onebot11/params.js +7 -0
  68. package/lib/types/onebot11/response.d.ts +516 -0
  69. package/lib/types/onebot11/response.js +1 -0
  70. package/lib/types/onebot11/segment.d.ts +268 -0
  71. package/lib/types/onebot11/segment.js +30 -0
  72. package/lib/types/onebot11/sender.d.ts +59 -0
  73. package/lib/types/onebot11/sender.js +1 -0
  74. package/lib/types/plugin.d.ts +13 -9
  75. package/lib/utils/button.d.ts +2 -2
  76. package/lib/utils/common.d.ts +2 -2
  77. package/lib/utils/config.js +3 -3
  78. package/lib/utils/handler.d.ts +2 -2
  79. package/lib/utils/handler.js +1 -1
  80. package/lib/utils/init.d.ts +1 -1
  81. package/lib/utils/init.js +2 -1
  82. package/lib/utils/segment.d.ts +2 -2
  83. package/package.json +1 -1
  84. package/lib/adapter/onebot/onebot11.d.ts +0 -309
  85. package/lib/adapter/onebot/onebot11.js +0 -1285
  86. package/lib/event/event.d.ts +0 -140
  87. package/lib/event/event.js +0 -125
  88. package/lib/event/message.d.ts +0 -105
  89. package/lib/event/message.js +0 -70
  90. package/lib/event/notice.d.ts +0 -49
  91. package/lib/event/notice.js +0 -15
  92. package/lib/event/request.d.ts +0 -49
  93. package/lib/event/request.js +0 -15
  94. package/lib/types/adapter.d.ts +0 -581
  95. package/lib/types/adapter.js +0 -1
  96. package/lib/types/api.js +0 -1
  97. package/lib/types/event.d.ts +0 -789
  98. package/lib/types/event.js +0 -1
  99. package/lib/types/onebots11.d.ts +0 -1426
  100. package/lib/types/onebots11.js +0 -1
  101. package/lib/types/reply.js +0 -1
@@ -1,1285 +0,0 @@
1
- import WebSocket from 'ws';
2
- import { randomUUID } from 'crypto';
3
- import { listener } from '../../core/listener.js';
4
- import { common, config, logger, segment } from '../../utils/index.js';
5
- import { KarinMessage, KarinNotice, KarinRequest } from '../../event/index.js';
6
- /**
7
- * @class OneBot11
8
- * @extends KarinAdapter
9
- */
10
- export class AdapterOneBot11 {
11
- /**
12
- * 是否初始化
13
- */
14
- #init = false;
15
- /**
16
- * - 重连次数 仅正向ws使用
17
- */
18
- index;
19
- socket;
20
- account;
21
- adapter;
22
- version;
23
- constructor() {
24
- this.index = 0;
25
- this.account = { uid: '', uin: '', name: '' };
26
- this.adapter = { id: 'QQ', name: 'OneBot11', type: 'ws', sub_type: 'internal', start_time: Date.now(), connect: '', index: 0 };
27
- this.version = { name: '', app_name: '', version: '' };
28
- }
29
- /**
30
- * 反向ws初始化
31
- */
32
- async server(socket, request) {
33
- this.socket = socket;
34
- const self_id = String(request.headers['x-self-id']);
35
- const connect = 'ws://' + request.headers.host + request.url;
36
- this.account.uin = self_id;
37
- this.account.uid = self_id;
38
- this.adapter.connect = connect;
39
- this.adapter.sub_type = 'server';
40
- this.logger('info', `[反向WS][onebot11-${request.headers.upgrade}][${self_id}] ` + logger.green(connect));
41
- await this.#initListener(connect);
42
- }
43
- /**
44
- * 正向ws初始化
45
- * @param connect - WebSocket连接地址
46
- */
47
- async client(connect) {
48
- /** 创建连接 */
49
- this.socket = new WebSocket(connect);
50
- this.socket.on('open', async () => {
51
- this.adapter.sub_type = 'client';
52
- this.adapter.connect = connect;
53
- logger.info('[正向WS][连接成功][onebot11] ' + logger.green(connect));
54
- this.index = 0;
55
- this.#initListener(connect);
56
- });
57
- }
58
- /**
59
- * 初始化监听事件
60
- * @param connect - WebSocket连接地址
61
- */
62
- async #initListener(connect) {
63
- /** 监听事件 */
64
- this.socket.on('message', data => {
65
- this.logger('debug', `[收到事件]:${data}`);
66
- const event = data.toString().trim() || '{"post_type":"error","error":"空事件"}';
67
- const json = JSON.parse(event);
68
- if (json.echo) {
69
- return this.socket.emit(json.echo, json);
70
- }
71
- else {
72
- /** 未初始化 */
73
- this.#init && this.#event(json);
74
- }
75
- });
76
- /** 监听错误 */
77
- this.socket.on('error', error => {
78
- this.logger('debug', '[正向WS] 发生错误', error);
79
- });
80
- /** 监听断开 */
81
- this.socket.once('close', async () => {
82
- const type = this.adapter.sub_type === 'server' ? '反向WS' : '正向WS';
83
- this.logger('warn', `[${type}] 连接断开:${connect}`);
84
- /** 停止全部监听 */
85
- this.socket.removeAllListeners();
86
- /** 注销bot */
87
- this.adapter.index && listener.delBot(this.adapter.index);
88
- /** 正向ws需要重连 */
89
- if (this.adapter.sub_type === 'client') {
90
- this.index++;
91
- this.logger('warn', `[正向WS][重连次数:${this.index}] 连接断开,将在5秒后重连:${connect}`);
92
- await common.sleep(5000);
93
- this.client(connect);
94
- }
95
- });
96
- await this.getSelf();
97
- this.#init = true;
98
- }
99
- get self_id() {
100
- return this.account.uid || this.account.uin;
101
- }
102
- /**
103
- * 获取当前登录号信息
104
- */
105
- async getSelf() {
106
- const data = await this.GetCurrentAccount();
107
- try {
108
- const { app_name, app_version: version } = await this.GetVersion();
109
- this.version.name = app_name;
110
- this.version.app_name = app_name;
111
- this.version.version = version;
112
- }
113
- catch (e) {
114
- /** 兼容onebots */
115
- const { app_name, app_version: version } = await this.SendApi('get_version');
116
- this.version.name = app_name;
117
- this.version.app_name = app_name;
118
- this.version.version = version;
119
- }
120
- this.account.uid = data.account_uid;
121
- this.account.uin = data.account_uin;
122
- this.account.name = data.account_name;
123
- this.logger('info', `[加载完成][app_name:${this.version.name}][version:${this.version.version}] ` + logger.green(this.adapter.connect));
124
- /** 注册bot */
125
- const index = listener.addBot({ type: this.adapter.type, bot: this });
126
- if (index)
127
- this.adapter.index = index;
128
- }
129
- /**
130
- * 处理事件
131
- * - @param data ob11相关标准数据
132
- */
133
- #event(data) {
134
- switch (data.post_type) {
135
- case 'meta_event': {
136
- if (data.meta_event_type === 'heartbeat') {
137
- this.logger('trace', `[心跳]:${JSON.stringify(data.status)}`);
138
- }
139
- else {
140
- const typeMap = {
141
- enable: 'OneBot启用',
142
- disable: 'OneBot停用',
143
- connect: 'WebSocket连接成功',
144
- };
145
- const sub_type = data.sub_type;
146
- this.logger('debug', `[生命周期]:${typeMap[sub_type]}`);
147
- }
148
- listener.emit('meta_event', data);
149
- return;
150
- }
151
- case 'message':
152
- case 'message_sent': {
153
- const message = {
154
- event: (data.post_type + ''),
155
- event_id: data.message_id + '',
156
- self_id: data.self_id + '',
157
- user_id: data.sender.user_id + '',
158
- time: data.time,
159
- message_id: data.message_id + '',
160
- message_seq: data.message_id,
161
- sender: {
162
- ...data.sender,
163
- uid: data.sender.user_id + '',
164
- uin: data.sender.user_id + '',
165
- nick: data.sender.nickname || '',
166
- role: ('role' in data.sender ? data.sender.role || '' : ''),
167
- },
168
- elements: this.AdapterConvertKarin(data.message),
169
- contact: {
170
- scene: (data.message_type === 'private' ? 'friend' : 'group'),
171
- peer: data.message_type === 'private' ? data.sender.user_id : data.group_id,
172
- sub_peer: '',
173
- },
174
- group_id: data.message_type === 'group' ? data.group_id : '',
175
- raw_message: '',
176
- };
177
- const e = new KarinMessage(message);
178
- e.bot = this;
179
- /**
180
- * 快速回复 开发者不应该使用这个方法,应该使用由karin封装过后的reply方法
181
- */
182
- e.replyCallback = async (elements) => {
183
- if (data.message_type === 'private') {
184
- return this.send_private_msg(data.user_id, elements);
185
- }
186
- else {
187
- return this.send_group_msg(data.group_id, elements);
188
- }
189
- };
190
- listener.emit('message', e);
191
- return;
192
- }
193
- case 'notice':
194
- this.#notice_event(data);
195
- return;
196
- case 'request':
197
- this.#request_event(data);
198
- return;
199
- default:
200
- this.logger('info', `未知事件:${JSON.stringify(data)}`);
201
- }
202
- }
203
- /**
204
- * 通知事件
205
- */
206
- #notice_event(data) {
207
- const time = data.time;
208
- const self_id = data.self_id + '';
209
- let notice = {};
210
- const user_id = data.user_id + '';
211
- const event_id = `notice.${time}`;
212
- const sender = {
213
- uid: data.user_id + '',
214
- uin: data.user_id + '',
215
- nick: '',
216
- role: 'unknown',
217
- };
218
- const contact = {
219
- scene: ('group_id' in data ? 'group' : 'friend'),
220
- peer: 'group_id' in data ? data.group_id : data.user_id,
221
- sub_peer: '',
222
- };
223
- switch (data.notice_type) {
224
- // 群文件上传
225
- case 'group_upload': {
226
- const group_id = data.group_id + '';
227
- const content = {
228
- group_id,
229
- operator_uid: data.user_id + '',
230
- operator_uin: data.user_id + '',
231
- file_id: data.file.id,
232
- file_sub_id: 0,
233
- file_name: data.file.name,
234
- file_size: data.file.size,
235
- expire_time: 0,
236
- file_url: '',
237
- };
238
- const options = {
239
- time,
240
- self_id,
241
- user_id,
242
- event_id,
243
- content,
244
- sender,
245
- contact,
246
- group_id,
247
- sub_event: 'group_file_uploaded',
248
- };
249
- notice = new KarinNotice(options);
250
- break;
251
- }
252
- // 群管理员变动
253
- case 'group_admin': {
254
- const group_id = data.group_id + '';
255
- const content = {
256
- group_id,
257
- target_uid: data.user_id + '',
258
- target_uin: data.user_id + '',
259
- is_admin: data.sub_type === 'set',
260
- };
261
- const options = {
262
- time,
263
- self_id,
264
- user_id,
265
- event_id,
266
- sender,
267
- contact,
268
- content,
269
- group_id,
270
- sub_event: 'group_admin_changed',
271
- };
272
- notice = new KarinNotice(options);
273
- break;
274
- }
275
- // 群成员减少
276
- case 'group_decrease': {
277
- const group_id = data.group_id + '';
278
- const content = {
279
- group_id,
280
- operator_uid: (data.operator_id + '') || '',
281
- operator_uin: (data.operator_id + '') || '',
282
- target_uid: data.user_id || '',
283
- target_uin: data.user_id || '',
284
- type: data.sub_type,
285
- };
286
- const options = {
287
- time,
288
- self_id,
289
- user_id,
290
- event_id,
291
- sender,
292
- contact,
293
- content,
294
- group_id,
295
- sub_event: 'group_member_decrease',
296
- };
297
- notice = new KarinNotice(options);
298
- break;
299
- }
300
- // 群成员增加
301
- case 'group_increase': {
302
- const group_id = data.group_id + '';
303
- const content = {
304
- group_id,
305
- operator_uid: (data.operator_id || '') + '',
306
- operator_uin: (data.operator_id || '') + '',
307
- target_uid: (data.user_id || '') + '',
308
- target_uin: (data.user_id || '') + '',
309
- type: data.sub_type,
310
- };
311
- const options = {
312
- time,
313
- self_id,
314
- user_id,
315
- event_id,
316
- sender,
317
- contact,
318
- content,
319
- group_id,
320
- sub_event: 'group_member_increase',
321
- };
322
- notice = new KarinNotice(options);
323
- break;
324
- }
325
- // 群禁言事件
326
- case 'group_ban': {
327
- const group_id = data.group_id + '';
328
- const content = {
329
- group_id,
330
- operator_uid: (data.operator_id + '') || '',
331
- operator_uin: (data.operator_id + '') || '',
332
- target_uid: data.user_id || '',
333
- target_uin: data.user_id || '',
334
- duration: data.duration,
335
- type: data.sub_type,
336
- };
337
- const options = {
338
- time,
339
- self_id,
340
- user_id,
341
- event_id,
342
- sender,
343
- contact,
344
- content,
345
- group_id,
346
- sub_event: 'group_member_ban',
347
- };
348
- notice = new KarinNotice(options);
349
- break;
350
- }
351
- case 'friend_add':
352
- // todo kritor没有这个事件
353
- this.logger('info', `[好友添加]:${JSON.stringify(data)}`);
354
- break;
355
- case 'group_recall': {
356
- const group_id = data.group_id + '';
357
- const content = {
358
- group_id,
359
- operator_uid: (data.operator_id + '') || '',
360
- operator_uin: (data.operator_id + '') || '',
361
- target_uid: data.user_id || '',
362
- target_uin: data.user_id || '',
363
- message_id: data.message_id,
364
- tip_text: '撤回了一条消息',
365
- };
366
- const options = {
367
- time,
368
- self_id,
369
- user_id,
370
- event_id,
371
- sender,
372
- contact,
373
- content,
374
- group_id,
375
- sub_event: 'group_recall',
376
- };
377
- notice = new KarinNotice(options);
378
- break;
379
- }
380
- case 'friend_recall': {
381
- const content = {
382
- operator_uid: data.user_id || '',
383
- operator_uin: data.user_id || '',
384
- message_id: data.message_id,
385
- tip_text: '撤回了一条消息',
386
- };
387
- const options = {
388
- time,
389
- self_id,
390
- user_id,
391
- event_id,
392
- sender,
393
- contact,
394
- content,
395
- sub_event: 'private_recall',
396
- };
397
- notice = new KarinNotice(options);
398
- break;
399
- }
400
- case 'notify':
401
- switch (data.sub_type) {
402
- case 'poke': {
403
- const group_id = 'group_id' in data ? data.group_id + '' : '';
404
- const content = {
405
- group_id,
406
- operator_uid: data.user_id + '',
407
- operator_uin: data.user_id + '',
408
- target_uid: data.target_id + '',
409
- target_uin: data.target_id + '',
410
- action: '戳了戳',
411
- suffix: '',
412
- action_image: '',
413
- };
414
- const options = {
415
- time,
416
- self_id,
417
- user_id,
418
- event_id,
419
- sender,
420
- contact,
421
- content,
422
- group_id,
423
- sub_event: data.group_id ? 'group_poke' : 'private_poke',
424
- };
425
- notice = new KarinNotice(options);
426
- break;
427
- }
428
- case 'lucky_king':
429
- // todo kritor没有这个事件
430
- this.logger('info', `[运气王]:${JSON.stringify(data)}`);
431
- break;
432
- case 'honor':
433
- // todo kritor没有这个事件
434
- this.logger('info', `[群荣誉变更]:${JSON.stringify(data)}`);
435
- break;
436
- }
437
- break;
438
- case 'group_msg_emoji_like': {
439
- const group_id = data.group_id + '';
440
- const content = {
441
- group_id,
442
- message_id: data.message_id,
443
- face_id: data.likes[0].emoji_id,
444
- is_set: true,
445
- };
446
- const options = {
447
- time,
448
- self_id,
449
- user_id,
450
- event_id,
451
- sender,
452
- contact,
453
- content,
454
- group_id,
455
- sub_event: 'group_message_reaction',
456
- };
457
- notice = new KarinNotice(options);
458
- break;
459
- }
460
- default: {
461
- return this.logger('error', '未知通知事件:', JSON.stringify(data));
462
- }
463
- }
464
- listener.emit('notice', notice);
465
- }
466
- /** 请求事件 */
467
- #request_event(data) {
468
- switch (data.request_type) {
469
- case 'friend': {
470
- const request = new KarinRequest({
471
- event_id: `request.${data.time}`,
472
- self_id: data.self_id + '',
473
- user_id: data.user_id + '',
474
- time: data.time,
475
- contact: {
476
- scene: 'friend',
477
- peer: data.user_id + '',
478
- sub_peer: '',
479
- },
480
- sender: {
481
- uid: data.user_id + '',
482
- uin: data.user_id + '',
483
- nick: '',
484
- role: 'unknown',
485
- },
486
- sub_event: 'private_apply',
487
- content: {
488
- applier_uid: data.user_id + '',
489
- applier_uin: data.user_id + '',
490
- message: data.comment,
491
- },
492
- });
493
- listener.emit('request', request);
494
- return;
495
- }
496
- case 'group': {
497
- const request = new KarinRequest({
498
- event_id: `request.${data.time}`,
499
- self_id: data.self_id + '',
500
- user_id: data.user_id + '',
501
- time: data.time,
502
- contact: {
503
- scene: 'group',
504
- peer: data.group_id + '',
505
- sub_peer: '',
506
- },
507
- sender: {
508
- uid: data.user_id + '',
509
- uin: data.user_id + '',
510
- nick: '',
511
- role: 'unknown',
512
- },
513
- sub_event: data.sub_type === 'add' ? 'group_apply' : 'invited_group',
514
- content: {
515
- group_id: data.group_id + '',
516
- applier_uid: data.user_id + '',
517
- applier_uin: data.user_id + '',
518
- inviter_uid: data.user_id + '',
519
- inviter_uin: data.user_id + '',
520
- message: data.comment,
521
- },
522
- });
523
- listener.emit('request', request);
524
- return;
525
- }
526
- default: {
527
- this.logger('info', `未知请求事件:${JSON.stringify(data)}`);
528
- }
529
- }
530
- }
531
- /**
532
- * onebot11转karin
533
- * @return karin格式消息
534
- * */
535
- AdapterConvertKarin(data) {
536
- const elements = [];
537
- for (const i of data) {
538
- switch (i.type) {
539
- case 'text':
540
- elements.push(segment.text(i.data.text));
541
- break;
542
- case 'face':
543
- elements.push(segment.face(Number(i.data.id)));
544
- break;
545
- case 'image':
546
- elements.push(segment.image(i.data.url || i.data.file, { file_type: i.data.type }));
547
- break;
548
- case 'record':
549
- elements.push(segment.voice(i.data.url || i.data.file, i.data.magic === 1));
550
- break;
551
- case 'video':
552
- elements.push(segment.video(i.data.url || i.data.file));
553
- break;
554
- case 'at':
555
- elements.push(segment.at(i.data.qq, i.data.qq));
556
- break;
557
- case 'poke':
558
- elements.push(segment.poke(Number(i.data.id), Number(i.data.type)));
559
- break;
560
- case 'contact':
561
- elements.push(segment.contact(i.data.type === 'qq' ? 'friend' : 'group', i.data.id));
562
- break;
563
- case 'location':
564
- elements.push(segment.location(Number(i.data.lat), Number(i.data.lon), i.data.title || '', i.data.content || ''));
565
- break;
566
- case 'reply':
567
- elements.push(segment.reply(i.data.id));
568
- break;
569
- case 'forward':
570
- elements.push(segment.forward(i.data.id));
571
- break;
572
- case 'json':
573
- elements.push(segment.json(i.data.data));
574
- break;
575
- case 'xml':
576
- elements.push(segment.xml(i.data.data));
577
- break;
578
- default: {
579
- elements.push(segment.text(JSON.stringify(i)));
580
- }
581
- }
582
- }
583
- return elements;
584
- }
585
- /**
586
- * karin转onebot11
587
- * @param data karin格式消息
588
- * */
589
- KarinConvertAdapter(data) {
590
- const elements = [];
591
- for (const i of data) {
592
- switch (i.type) {
593
- case 'text':
594
- elements.push({ type: 'text', data: { text: i.text } });
595
- break;
596
- case 'face':
597
- elements.push({ type: 'face', data: { id: i.id } });
598
- break;
599
- case 'at':
600
- elements.push({ type: 'at', data: { qq: String(i.uid || i.uin) } });
601
- break;
602
- case 'reply':
603
- elements.push({ type: 'reply', data: { id: i.message_id } });
604
- break;
605
- case 'image':
606
- case 'video':
607
- case 'file': {
608
- elements.push({ type: i.type, data: { file: i.file } });
609
- break;
610
- }
611
- case 'xml': {
612
- elements.push({ type: 'xml', data: { data: i.data } });
613
- break;
614
- }
615
- case 'json': {
616
- elements.push({ type: 'json', data: { data: i.data } });
617
- break;
618
- }
619
- case 'forward': {
620
- elements.push({ type: 'forward', data: { id: i.res_id } });
621
- break;
622
- }
623
- case 'record':
624
- case 'voice': {
625
- elements.push({ type: 'record', data: { file: i.file, magic: i.magic || false } });
626
- break;
627
- }
628
- case 'music': {
629
- if (i.id) {
630
- elements.push({ type: 'music', data: { type: i.platform, id: i.id } });
631
- }
632
- else {
633
- const { url, audio, title, author, pic } = i;
634
- elements.push({ type: 'music', data: { type: 'custom', url, audio, title, content: author, image: pic } });
635
- }
636
- break;
637
- }
638
- case 'button': {
639
- elements.push({ type: 'button', data: i.data });
640
- break;
641
- }
642
- case 'markdown': {
643
- const { type, ...data } = i;
644
- elements.push({ type, data: { ...data } });
645
- break;
646
- }
647
- case 'rows': {
648
- for (const val of i.rows) {
649
- elements.push({ type: 'button', data: val.data });
650
- }
651
- break;
652
- }
653
- case 'poke': {
654
- elements.push({ type: 'poke', data: { type: i.poke_type, id: i.id } });
655
- break;
656
- }
657
- case 'bubble_face': {
658
- elements.push({ type: 'bubble_face', data: { id: i.id, count: i.count } });
659
- break;
660
- }
661
- case 'contact': {
662
- elements.push({ type: 'contact', data: { type: i.scene, id: i.peer } });
663
- break;
664
- }
665
- case 'location': {
666
- elements.push({ type: 'location', data: { lat: i.lat, lon: i.lon, title: i.title, content: i.address } });
667
- break;
668
- }
669
- case 'long_msg':
670
- case 'basketball':
671
- case 'dice':
672
- case 'market_face':
673
- case 'rps': {
674
- elements.push({ type: i.type, data: { id: i.id } });
675
- break;
676
- }
677
- case 'gift': {
678
- elements.push({ type: 'gift', data: { qq: i.qq, id: i.id } });
679
- break;
680
- }
681
- case 'share': {
682
- elements.push({ type: 'share', data: { url: i.url, title: i.title, content: i.content, image: i.image } });
683
- break;
684
- }
685
- case 'weather': {
686
- elements.push({ type: 'weather', data: { city: i.city, type: i.type } });
687
- break;
688
- }
689
- default: {
690
- elements.push(i);
691
- break;
692
- }
693
- }
694
- }
695
- return elements;
696
- }
697
- /**
698
- * 专属当前Bot的日志打印方法
699
- */
700
- logger(level, ...args) {
701
- logger.bot(level, this.account.uid || this.account.uin, ...args);
702
- }
703
- /**
704
- * 获取头像url
705
- * @param 头像大小,默认`0`
706
- * @param 用户qq,默认为机器人QQ
707
- * @returns 头像的url地址
708
- */
709
- getAvatarUrl(uid = this.account.uid || this.account.uin, size = 0) {
710
- return Number(uid) ? `https://q1.qlogo.cn/g?b=qq&s=${size}&nk=${uid}` : `https://q.qlogo.cn/qqapp/${uid}/${uid}/${size}`;
711
- }
712
- /**
713
- * 获取群头像
714
- * @param group_id - 群号
715
- * @param size - 头像大小,默认`0`
716
- * @param history - 历史头像记录,默认`0`,若要获取历史群头像则填写1,2,3...
717
- * @returns - 群头像的url地址
718
- */
719
- getGroupAvatar(group_id, size = 0, history = 0) {
720
- return `https://p.qlogo.cn/gh/${group_id}/${group_id}${history ? '_' + history : ''}/` + size;
721
- }
722
- /**
723
- * 发送私聊消息
724
- * @param user_id - 用户ID
725
- * @param message - 要发送的内容
726
- * @returns - 消息ID
727
- */
728
- async send_private_msg(user_id, message) {
729
- const obMessage = this.KarinConvertAdapter(message);
730
- // this.logger(`${logger.green(`Send private ${user_id}: `)}${this.logSend(message)}`))
731
- return await this.SendApi('send_private_msg', { user_id, message: obMessage });
732
- }
733
- /**
734
- * 发送群消息
735
- * @param group_id - 群号
736
- * @param message - 要发送的内容
737
- * @returns - 消息ID
738
- */
739
- async send_group_msg(group_id, message) {
740
- const obMessages = this.KarinConvertAdapter(message);
741
- return await this.SendApi('send_group_msg', { group_id, message: obMessages });
742
- }
743
- /**
744
- * 发送消息
745
- *
746
- * @param contact
747
- * @param elements
748
- * @returns - 消息ID
749
- */
750
- async SendMessage(contact, elements) {
751
- const { scene, peer } = contact;
752
- const message_type = scene === 'group' ? 'group' : 'private';
753
- const key = scene === 'group' ? 'group_id' : 'user_id';
754
- const message = this.KarinConvertAdapter(elements);
755
- const params = { [key]: peer, message_type, message };
756
- return await this.SendApi('send_msg', params);
757
- }
758
- /**
759
- * 上传合并转发消息
760
- * @param contact - 联系人信息
761
- * @param elements - nodes
762
- * @returns - 资源id
763
- * */
764
- async UploadForwardMessage(contact, elements) {
765
- if (!Array.isArray(elements))
766
- elements = [elements];
767
- if (elements.some((element) => element.type !== 'node')) {
768
- throw new Error('elements should be all node type');
769
- }
770
- const { scene, peer } = contact;
771
- const message_type = scene === 'group' ? 'group_id' : 'user_id';
772
- const messages = [];
773
- const selfUin = this.account.uin;
774
- const selfNick = this.account.name;
775
- for (const i of elements) {
776
- const { type, user_id, nickname, content: contents } = i;
777
- const content = this.KarinConvertAdapter(contents);
778
- messages.push({ type, data: { uin: user_id || selfUin, name: nickname || selfNick, content } });
779
- }
780
- const params = { [message_type]: String(peer), messages };
781
- return await this.SendApi('send_forward_msg', params);
782
- }
783
- /**
784
- * 通过资源id发送转发消息
785
- * @param contact - 联系人信息
786
- * @param id - 资源id
787
- * */
788
- async SendMessageByResId(contact, id) {
789
- const { scene, peer } = contact;
790
- const message_type = scene === 'group' ? 'group' : 'private';
791
- const key = scene === 'group' ? 'group_id' : 'user_id';
792
- const message = [{ type: 'forward', data: { id } }];
793
- const params = { [key]: peer, message_type, message };
794
- const res = await this.SendApi('send_msg', params);
795
- return { message_id: res.message_id, message_time: Date.now() };
796
- }
797
- /**
798
- * 撤回消息
799
- * @param _contact - ob11无需提供contact参数
800
- * @param message_id - 消息ID
801
- */
802
- async RecallMessage(_contact, message_id) {
803
- return await this.SendApi('delete_msg', { message_id });
804
- }
805
- /**
806
- * 获取消息
807
- * @param _contact - ob11无需提供contact参数
808
- * @param message_id - 消息ID
809
- */
810
- async GetMessage(_contact, message_id) {
811
- let res = await this.SendApi('get_msg', { message_id });
812
- res = {
813
- time: res.time,
814
- message_id: res.message_id,
815
- message_seq: res.message_id,
816
- contact: {
817
- scene: res.message_type === 'group' ? 'group' : 'friend',
818
- peer: res.sender.user_id, // 拿不到group_id...
819
- },
820
- sender: {
821
- uid: res.sender.user_id,
822
- uin: res.sender.user_id,
823
- nick: res.sender.nickname,
824
- },
825
- elements: this.AdapterConvertKarin(res.message),
826
- };
827
- return res;
828
- }
829
- /**
830
- * 获取msg_id获取历史消息
831
- * @description 此api各平台实现不同,暂时废弃
832
- */
833
- async GetHistoryMessage(contact, start_message_id, count = 1) {
834
- const type = contact.scene === 'group' ? 'group_id' : 'user_id';
835
- const param = { [type]: contact.peer, message_id: start_message_id, count };
836
- const api = contact.scene === 'group' ? 'get_group_msg_history' : 'get_friend_msg_history';
837
- const res = await this.SendApi(api, param, 120);
838
- const ret = [];
839
- for (const i of res.messages) {
840
- let { time = Date.now(), message_id, message_seq = message_id, sender, message } = i;
841
- const { user_id, nickname } = sender;
842
- sender = { uid: user_id, uin: user_id, nick: nickname };
843
- const elements = this.AdapterConvertKarin(message);
844
- ret.push({ time, message_id, message_seq, contact, sender, elements });
845
- }
846
- return ret;
847
- }
848
- /**
849
- * 获取合并转发消息
850
- */
851
- async get_forward_msg(id) {
852
- return await this.SendApi('get_forward_msg', { id });
853
- }
854
- /**
855
- * 发送好友赞
856
- * @param target_uid_or_uin - 用户ID
857
- * @param vote_count - 赞的次数,默认为`10`
858
- */
859
- async VoteUser(target_uid_or_uin, vote_count = 10) {
860
- const user_id = Number(target_uid_or_uin);
861
- await this.SendApi('send_like', { user_id, times: vote_count });
862
- }
863
- /**
864
- * 群组踢人
865
- */
866
- async KickMember(group_id, target_uid_or_uin, reject_add_request = false, kick_reason = '') {
867
- const user_id = Number(target_uid_or_uin);
868
- await this.SendApi('set_group_kick', { group_id, user_id, reject_add_request });
869
- }
870
- /**
871
- * 禁言用户
872
- * @param group_id - 群号
873
- * @param target_uid_or_uin - 用户ID
874
- * @param duration - 禁言时长,单位秒,0 表示取消禁言
875
- */
876
- async BanMember(group_id, target_uid_or_uin, duration) {
877
- const user_id = Number(target_uid_or_uin);
878
- await this.SendApi('set_group_ban', { group_id, user_id, duration });
879
- }
880
- /**
881
- * 群组全员禁言
882
- * @param group_id - 群号
883
- * @param is_ban - 是否全员禁言
884
- */
885
- async SetGroupWholeBan(group_id, is_ban = true) {
886
- await this.SendApi('set_group_whole_ban', { group_id, enable: is_ban });
887
- }
888
- /**
889
- * 设置群管理员
890
- * @param group_id - 群号
891
- * @param target_uid_or_uin - 目标用户ID
892
- * @param is_admin - 是否设置为管理员
893
- */
894
- async SetGroupAdmin(group_id, target_uid_or_uin, is_admin) {
895
- const user_id = Number(target_uid_or_uin);
896
- await this.SendApi('set_group_admin', { group_id, user_id, enable: is_admin });
897
- }
898
- /**
899
- * 群组匿名
900
- * @param group_id - 群号
901
- * @param enable - 是否允许匿名聊天
902
- */
903
- async set_group_anonymous(group_id, enable = true) {
904
- await this.SendApi('set_group_anonymous', { group_id, enable });
905
- }
906
- /**
907
- * 修改群名片
908
- * @param group_id - 群号
909
- * @param target_uid_or_uin - 目标用户ID
910
- * @param card - 新名片
911
- */
912
- async ModifyMemberCard(group_id, target_uid_or_uin, card) {
913
- const user_id = Number(target_uid_or_uin);
914
- await this.SendApi('set_group_card', { group_id, user_id, card });
915
- }
916
- /**
917
- * 设置群名
918
- * @param group_id - 群号
919
- * @param group_name - 新群名
920
- */
921
- async ModifyGroupName(group_id, group_name) {
922
- await this.SendApi('set_group_name', { group_id, group_name });
923
- }
924
- /**
925
- * 退出群组
926
- * @param group_id - 群号
927
- * @param is_dismiss - 是否解散,如果登录号是群主,则仅在此项为 true 时能够解散
928
- */
929
- async LeaveGroup(group_id, is_dismiss = false) {
930
- await this.SendApi('set_group_leave', { group_id, is_dismiss });
931
- }
932
- /**
933
- * 设置群专属头衔
934
- * @param group_id - 群号
935
- * @param target_uid_or_uin - 目标用户ID
936
- * @param special_title - 专属头衔
937
- */
938
- async SetGroupUniqueTitle(group_id, target_uid_or_uin, unique_title) {
939
- const user_id = Number(target_uid_or_uin);
940
- const special_title = unique_title;
941
- const duration = -1;
942
- await this.SendApi('set_group_special_title', { group_id, user_id, special_title, duration });
943
- }
944
- // /**
945
- // * 处理加好友请求
946
- // * @param flag - 加好友请求的 flag(需从上报的数据中获得)
947
- // * @param {boolean} [approve=true] - 是否同意请求
948
- // * @param [remark=''] - 添加后的好友备注(仅在同意时有效)
949
- // */
950
- // async set_friend_add_request(flag: string, approve = true, remark = '') {
951
- // await this.SendApi('set_friend_add_request', { flag, approve, remark })
952
- // }
953
- // /**
954
- // * 处理加群请求/邀请
955
- // * @param flag - 加群请求的 flag(需从上报的数据中获得)
956
- // * @param sub_type - add 或 invite,请求类型(需要和上报消息中的 sub_type 字段相符)
957
- // * @param {boolean} [approve=true] - 是否同意请求/邀请
958
- // * @param [reason=''] - 拒绝理由(仅在拒绝时有效)
959
- // */
960
- // async set_group_add_request(flag: string, sub_type: string, approve = true, reason = '') {
961
- // await this.SendApi('set_group_add_request', { flag, sub_type, approve, reason })
962
- // }
963
- /**
964
- * 获取登录号信息
965
- */
966
- async GetCurrentAccount() {
967
- const res = await this.SendApi('get_login_info');
968
- return {
969
- account_uid: res.user_id,
970
- account_uin: res.user_id,
971
- account_name: res.nickname,
972
- };
973
- }
974
- /**
975
- * 获取陌生人信息 不支持批量获取 只支持一个
976
- * @param target_uid_or_uin - 目标用户ID
977
- */
978
- async GetStrangerProfileCard(target_uid_or_uin) {
979
- const user_id = Number(target_uid_or_uin[0]) || String(target_uid_or_uin[0]);
980
- const res = await this.SendApi('get_stranger_info', { user_id, no_cache: true });
981
- return [res];
982
- }
983
- /**
984
- * 获取好友列表
985
- */
986
- async GetFriendList() {
987
- /** @type {{
988
- * user_id: number,
989
- * user_name: string?,
990
- * user_remark: string,
991
- * remark: string?,
992
- * nickname: string?,
993
- }[]} **/
994
- const friendList = await this.SendApi('get_friend_list');
995
- return friendList.map((friend) => {
996
- return {
997
- uin: friend.user_id,
998
- uid: friend.user_id,
999
- qid: '',
1000
- nick: friend.nickname || friend.user_name,
1001
- remark: friend.remark || friend.user_remark,
1002
- };
1003
- });
1004
- }
1005
- /**
1006
- * 获取群信息
1007
- * @param group_id - 群号
1008
- * @param no_cache - 是否不使用缓存
1009
- */
1010
- async GetGroupInfo(group_id, no_cache = false) {
1011
- const groupInfo = await this.SendApi('get_group_info', { group_id, no_cache });
1012
- return {
1013
- group_id: groupInfo.group_id,
1014
- group_name: groupInfo.group_name,
1015
- group_remark: groupInfo.group_memo || groupInfo.group_remark,
1016
- max_member_count: groupInfo.max_member_count,
1017
- member_count: groupInfo.member_count,
1018
- group_uin: groupInfo.group_id,
1019
- admins: groupInfo.admins,
1020
- owner: groupInfo.admins[0],
1021
- };
1022
- }
1023
- /**
1024
- * 获取群列表
1025
- */
1026
- async GetGroupList() {
1027
- const groupList = await this.SendApi('get_group_list');
1028
- return groupList?.map((groupInfo) => {
1029
- return {
1030
- group_id: groupInfo.group_id,
1031
- group_name: groupInfo.group_name,
1032
- group_remark: groupInfo.group_memo || groupInfo.group_remark,
1033
- max_member_count: groupInfo.max_member_count,
1034
- member_count: groupInfo.member_count,
1035
- group_uin: groupInfo.group_id,
1036
- admins: groupInfo.admins,
1037
- };
1038
- });
1039
- }
1040
- /**
1041
- * 获取群成员信息
1042
- * @param group_id - 群号
1043
- * @param target_uid_or_uin - 目标用户ID
1044
- * @param refresh - 是否刷新缓存,默认为 false
1045
- */
1046
- async GetGroupMemberInfo(group_id, target_uid_or_uin, refresh = false) {
1047
- const user_id = Number(target_uid_or_uin);
1048
- /**
1049
- * @type {{
1050
- * group_id: number,
1051
- * user_id: number,
1052
- * nickname: string,
1053
- * card: string,
1054
- * sex: string,
1055
- * age: number,
1056
- * area: string,
1057
- * join_time: number,
1058
- * last_sent_time: number,
1059
- * level: string,
1060
- * role: 'owner' | 'admin' | 'member',
1061
- * unfriendly: boolean,
1062
- * title: string,
1063
- * title_expire_time: number,
1064
- * card_changeable: boolean,
1065
- * shut_up_timestamp: number
1066
- * }}
1067
- */
1068
- const groupMemberInfo = await this.SendApi('get_group_member_info', { group_id, user_id, no_cache: refresh });
1069
- let level = 0;
1070
- try {
1071
- level = parseInt(groupMemberInfo.level);
1072
- }
1073
- catch (e) { }
1074
- return {
1075
- uid: groupMemberInfo.user_id,
1076
- uin: groupMemberInfo.user_id,
1077
- nick: groupMemberInfo.nickname,
1078
- age: groupMemberInfo.age,
1079
- unique_title: groupMemberInfo.title,
1080
- unique_title_expire_time: groupMemberInfo.title_expire_time,
1081
- card: groupMemberInfo.card,
1082
- join_time: groupMemberInfo.join_time,
1083
- last_active_time: groupMemberInfo.last_sent_time,
1084
- shut_up_time: 0,
1085
- level,
1086
- shut_up_timestamp: groupMemberInfo.shut_up_timestamp,
1087
- unfriendly: groupMemberInfo.unfriendly,
1088
- card_changeable: groupMemberInfo.card_changeable,
1089
- };
1090
- }
1091
- /**
1092
- * 获取群成员列表
1093
- * @param group_id - 群号
1094
- * @param refresh - 是否刷新缓存,默认为 false
1095
- */
1096
- async GetGroupMemberList(group_id, refresh = false) {
1097
- const gl = await this.SendApi('get_group_member_list', { group_id, refresh });
1098
- return gl.map((groupMemberInfo) => {
1099
- let level = 0;
1100
- try {
1101
- level = parseInt(groupMemberInfo.level);
1102
- }
1103
- catch (e) { }
1104
- return {
1105
- uid: groupMemberInfo.user_id,
1106
- uin: groupMemberInfo.user_id,
1107
- nick: groupMemberInfo.nickname,
1108
- age: groupMemberInfo.age,
1109
- unique_title: groupMemberInfo.title,
1110
- unique_title_expire_time: groupMemberInfo.title_expire_time,
1111
- card: groupMemberInfo.card,
1112
- join_time: groupMemberInfo.join_time,
1113
- last_active_time: groupMemberInfo.last_sent_time,
1114
- level,
1115
- shut_up_timestamp: groupMemberInfo.shut_up_timestamp,
1116
- unfriendly: groupMemberInfo.unfriendly,
1117
- card_changeable: groupMemberInfo.card_changeable,
1118
- };
1119
- });
1120
- }
1121
- /**
1122
- * 获取群荣誉信息
1123
- */
1124
- async GetGroupHonor(group_id, refresh = false) {
1125
- /**
1126
- * @typedef {{user_id: number, nickname: string, avatar: string, description: string}} GroupHonor
1127
- */
1128
- /**
1129
- * @type {{
1130
- * group_id: number,
1131
- * current_talkative: {user_id: number, nickname: string, avatar: string, day_count: number},
1132
- * talkative_list: Array<GroupHonor>,
1133
- * performer_list: Array<GroupHonor>,
1134
- * legend_list: Array<GroupHonor>,
1135
- * strong_newbie_list: Array<GroupHonor>,
1136
- * emotion_list: Array<GroupHonor>,
1137
- * }}
1138
- */
1139
- const groupHonor = await this.SendApi('get_group_honor_info', { group_id, type: 'all' });
1140
- const result = [];
1141
- groupHonor.talkative_list.forEach((honor) => {
1142
- result.push({
1143
- uin: honor.user_id,
1144
- uid: honor.user_id,
1145
- nick: honor.nickname,
1146
- honor_name: '历史龙王',
1147
- id: 0,
1148
- avatar: honor.avatar,
1149
- description: honor.description,
1150
- });
1151
- });
1152
- groupHonor.performer_list.forEach((honor) => {
1153
- result.push({
1154
- uin: honor.user_id,
1155
- uid: honor.user_id,
1156
- nick: honor.nickname,
1157
- honor_name: '群聊之火',
1158
- avatar: honor.avatar,
1159
- id: 0,
1160
- description: honor.description,
1161
- });
1162
- });
1163
- groupHonor.legend_list.forEach((honor) => {
1164
- result.push({
1165
- uin: honor.user_id,
1166
- uid: honor.user_id,
1167
- nick: honor.nickname,
1168
- honor_name: '群聊炽焰',
1169
- avatar: honor.avatar,
1170
- id: 0,
1171
- description: honor.description,
1172
- });
1173
- });
1174
- groupHonor.strong_newbie_list.forEach((honor) => {
1175
- result.push({
1176
- uin: honor.user_id,
1177
- uid: honor.user_id,
1178
- nick: honor.nickname,
1179
- honor_name: '冒尖小春笋',
1180
- avatar: honor.avatar,
1181
- id: 0,
1182
- description: honor.description,
1183
- });
1184
- });
1185
- groupHonor.emotion_list.forEach((honor) => {
1186
- result.push({
1187
- uin: honor.user_id,
1188
- uid: honor.user_id,
1189
- nick: honor.nickname,
1190
- honor_name: '快乐之源',
1191
- avatar: honor.avatar,
1192
- id: 0,
1193
- description: honor.description,
1194
- });
1195
- });
1196
- return result;
1197
- }
1198
- /**
1199
- * 对消息进行表情回应
1200
- * @param contact - 联系人信息
1201
- * @param message_id - 消息ID
1202
- * @param face_id - 表情ID
1203
- */
1204
- async ReactMessageWithEmoji(contact, message_id, face_id, is_set = true) {
1205
- return await this.SendApi('set_msg_emoji_like', { message_id, emoji_id: face_id, is_set });
1206
- }
1207
- /**
1208
- * 获取版本信息
1209
- */
1210
- async GetVersion() {
1211
- return await this.SendApi('get_version_info');
1212
- }
1213
- async DownloadForwardMessage() {
1214
- throw new Error('Method not implemented.');
1215
- }
1216
- async GetEssenceMessageList() {
1217
- throw new Error('Method not implemented.');
1218
- }
1219
- /**
1220
- * 上传群文件
1221
- * @param group_id - 群号
1222
- * @param file - 本地文件绝对路径
1223
- * @param name - 文件名称 必须提供
1224
- * @param folder - 父目录ID 不提供则上传到根目录
1225
- */
1226
- async UploadGroupFile(group_id, file, name, folder) {
1227
- return await this.SendApi('upload_group_file', { group_id, file, name, folder });
1228
- }
1229
- /**
1230
- * 上传私聊文件
1231
- * @param user_id - 用户ID
1232
- * @param file - 本地文件绝对路径
1233
- * @param name - 文件名称 必须提供
1234
- */
1235
- async UploadPrivateFile(user_id, file, name) {
1236
- return await this.SendApi('upload_private_file', { user_id, file, name });
1237
- }
1238
- async SetEssenceMessage() { }
1239
- async DeleteEssenceMessage() { }
1240
- async SetFriendApplyResult() { }
1241
- async SetGroupApplyResult() { }
1242
- async SetInvitedJoinGroupResult() { }
1243
- async sendForwardMessage(contact, elements) {
1244
- let message_id = await this.UploadForwardMessage(contact, elements);
1245
- if (this.version.name === 'Lagrange.OneBot') {
1246
- message_id = await this.SendMessage(contact, [segment.forward(message_id)]);
1247
- }
1248
- return { message_id };
1249
- }
1250
- /**
1251
- * 发送API请求
1252
- * @param action - API断点
1253
- * @param {object} params - API参数
1254
- * @returns {Promise<any>} - API返回
1255
- */
1256
- async SendApi(action, params = {}, time = 0) {
1257
- if (!time)
1258
- time = config.timeout('ws');
1259
- const echo = randomUUID();
1260
- const request = JSON.stringify({ echo, action, params });
1261
- logger.debug(`[API请求] ${action}: ${request}`);
1262
- return new Promise((resolve, reject) => {
1263
- const timeoutId = setTimeout(() => {
1264
- reject(new Error('API请求超时'));
1265
- }, time * 1000);
1266
- this.socket.send(request);
1267
- this.socket.once(echo, data => {
1268
- /** 停止监听器 */
1269
- clearTimeout(timeoutId);
1270
- if (data.status === 'ok') {
1271
- resolve(data.data);
1272
- }
1273
- else {
1274
- this.logger('error', `[Api请求错误] ${action}: ${JSON.stringify(data, null, 2)}`);
1275
- reject(data);
1276
- }
1277
- });
1278
- });
1279
- }
1280
- }
1281
- export default {
1282
- type: 'websocket',
1283
- path: '/onebot/v11/ws',
1284
- adapter: AdapterOneBot11,
1285
- };