onebots 0.0.17 → 0.0.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.
package/lib/onebot.d.ts CHANGED
@@ -21,7 +21,7 @@ export declare class OneBot<V extends OneBot.Version> extends EventEmitter {
21
21
  start(): Promise<void>;
22
22
  startListen(): void;
23
23
  stop(force?: boolean): Promise<void>;
24
- dispatch(data: any): void;
24
+ dispatch(event: any, data: any): void;
25
25
  }
26
26
  export declare enum OneBotStatus {
27
27
  Good = 0,
package/lib/onebot.js CHANGED
@@ -101,10 +101,10 @@ class OneBot extends events_1.EventEmitter {
101
101
  await this.client.login(this.password);
102
102
  }
103
103
  startListen() {
104
- this.client.on('system', this.dispatch.bind(this));
105
- this.client.on('notice', this.dispatch.bind(this));
106
- this.client.on('request', this.dispatch.bind(this));
107
- this.client.on('message', this.dispatch.bind(this));
104
+ this.client.on('system', this.dispatch.bind(this, 'system'));
105
+ this.client.on('notice', this.dispatch.bind(this, 'notice'));
106
+ this.client.on('request', this.dispatch.bind(this, 'request'));
107
+ this.client.on('message', this.dispatch.bind(this, 'message'));
108
108
  for (const instance of this.instances) {
109
109
  instance.start(this.instances.length > 1 ? '/' + instance.version : undefined);
110
110
  }
@@ -113,14 +113,12 @@ class OneBot extends events_1.EventEmitter {
113
113
  for (const instance of this.instances) {
114
114
  await instance.stop(force);
115
115
  }
116
- this.client.off('system', this.dispatch.bind(this));
117
- this.client.off('notice', this.dispatch.bind(this));
118
- this.client.off('request', this.dispatch.bind(this));
119
- this.client.off('message', this.dispatch.bind(this));
116
+ this.client.removeAllListeners();
120
117
  }
121
- dispatch(data) {
118
+ dispatch(event, data) {
122
119
  for (const instance of this.instances) {
123
- instance.dispatch(data);
120
+ const result = instance.format(event, data);
121
+ instance.dispatch(result);
124
122
  }
125
123
  }
126
124
  }
@@ -33,6 +33,7 @@ export declare class V11 extends EventEmitter implements OneBot.Base {
33
33
  private startWs;
34
34
  private startWsReverse;
35
35
  stop(force?: boolean): Promise<void>;
36
+ format(_: any, data: any): any;
36
37
  dispatch(data: any): void;
37
38
  private _httpRequestHandler;
38
39
  /**
@@ -181,6 +181,9 @@ class V11 extends events_1.EventEmitter {
181
181
  (0, fs_1.rmSync)(this.client.dir, { force: true, recursive: true });
182
182
  }
183
183
  }
184
+ format(_, data) {
185
+ return data;
186
+ }
184
187
  dispatch(data) {
185
188
  if (!data.post_type)
186
189
  data.post_type = 'system';
@@ -10,6 +10,7 @@ export declare class CommonAction {
10
10
  deleteMsg(this: V12, message_id: string): Promise<boolean>;
11
11
  getSelfInfo(this: V12): {
12
12
  user_id: number;
13
+ platform: string;
13
14
  nickname: string;
14
15
  user_displayname: string;
15
16
  };
@@ -16,6 +16,7 @@ class CommonAction {
16
16
  getSelfInfo() {
17
17
  return {
18
18
  user_id: this.client.uin,
19
+ platform: 'qq',
19
20
  nickname: this.client.nickname,
20
21
  user_displayname: ''
21
22
  };
@@ -1,6 +1,6 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="node" />
3
- import { Client } from "oicq";
3
+ import { Client, EventMap, MessageElem, Sendable } from "oicq";
4
4
  import { Config } from './config';
5
5
  import { OneBot } from "../../onebot";
6
6
  import { Action } from "./action";
@@ -29,6 +29,12 @@ export declare class V12 extends EventEmitter implements OneBot.Base {
29
29
  private startWs;
30
30
  private startWsReverse;
31
31
  stop(force?: boolean): Promise<void>;
32
+ format<E extends keyof V12.BotEventMap>(event: E, ...args: [V12.BotEventMap[E]]): {
33
+ self_id: number;
34
+ time: number;
35
+ detail_type: E;
36
+ sub_type: string;
37
+ } & Omit<V12.BotEventMap[E], E>;
32
38
  dispatch(data: Record<string, any>): void;
33
39
  apply(req: any): Promise<string>;
34
40
  private _httpRequestHandler;
@@ -46,6 +52,47 @@ export declare class V12 extends EventEmitter implements OneBot.Base {
46
52
  protected _webSocketHandler(ws: WebSocket): void;
47
53
  }
48
54
  export declare namespace V12 {
55
+ function fromSegment(msgList: SegmentElem | string | number | (SegmentElem | string | number)[]): MessageElem[];
56
+ function toSegment(msgList: Sendable): SegmentElem<keyof SegmentMap>[];
57
+ interface SegmentMap {
58
+ face: {
59
+ id: number;
60
+ text: string;
61
+ };
62
+ text: {
63
+ text: string;
64
+ };
65
+ mention: {
66
+ user_id: string;
67
+ };
68
+ mention_all: null;
69
+ image: {
70
+ file_id: string;
71
+ };
72
+ voice: {
73
+ file_id: string;
74
+ };
75
+ audio: {
76
+ file_id: string;
77
+ };
78
+ file: {
79
+ file_id: string;
80
+ };
81
+ location: {
82
+ latitude: number;
83
+ longitude: number;
84
+ title: string;
85
+ content: string;
86
+ };
87
+ reply: {
88
+ message_id: string;
89
+ user_id: string;
90
+ };
91
+ }
92
+ type SegmentElem<K extends keyof SegmentMap = keyof SegmentMap> = {
93
+ type: K;
94
+ data: SegmentMap[K];
95
+ };
49
96
  interface Config {
50
97
  heartbeat?: number;
51
98
  access_token?: string;
@@ -94,7 +141,8 @@ export declare namespace V12 {
94
141
  params: any;
95
142
  echo?: string;
96
143
  }
97
- type MetaEventMap = {
144
+ type BotEventMap = {
145
+ system: Record<string, any>;
98
146
  connect: {
99
147
  detail_type: 'connect';
100
148
  version: ReturnType<Action['getVersion']>;
@@ -107,15 +155,23 @@ export declare namespace V12 {
107
155
  detail_type: 'status_update';
108
156
  status: ReturnType<Action['getStatus']>;
109
157
  };
158
+ } & TransformEventMap;
159
+ type TransformEventMap = {
160
+ [P in keyof EventMap]: TransformEventParams<Parameters<EventMap[P]>>;
161
+ };
162
+ type TransformEventParams<T extends any[]> = T extends [infer L, ...infer R] ? L extends object ? L & {
163
+ args: R;
164
+ } : {
165
+ args: [L, ...R];
166
+ } : {
167
+ args: T;
110
168
  };
111
169
  function success<T extends any>(data: T, retcode?: Result<T>['retcode'], echo?: string): Result<T>;
112
170
  function error(message: string, retcode?: Result<null>['retcode'], echo?: string): Result<null>;
113
- function genMetaEvent<K extends keyof MetaEventMap>(uin: number, type: K, data: Omit<MetaEventMap[K], K>): {
171
+ function formatPayload<K extends keyof BotEventMap>(uin: number, type: K, data: Omit<BotEventMap[K], K>): {
114
172
  self_id: number;
115
173
  time: number;
116
- type: string;
117
- status: string;
118
174
  detail_type: K;
119
175
  sub_type: string;
120
- } & Omit<MetaEventMap[K], K>;
176
+ } & Omit<BotEventMap[K], K>;
121
177
  }
@@ -13,7 +13,6 @@ const url_1 = require("url");
13
13
  const http_1 = __importDefault(require("http"));
14
14
  const https_1 = __importDefault(require("https"));
15
15
  const ws_1 = require("ws");
16
- const oicq2_cq_enable_1 = require("oicq2-cq-enable");
17
16
  const utils_1 = require("../../utils");
18
17
  const db_1 = require("../../db");
19
18
  const app_1 = require("../../server/app");
@@ -88,7 +87,7 @@ class V12 extends events_1.EventEmitter {
88
87
  });
89
88
  if (this.config.heartbeat) {
90
89
  this.heartbeat = setInterval(() => {
91
- this.dispatch(V12.genMetaEvent(this.client.uin, 'heartbeat', {
90
+ this.dispatch(V12.formatPayload(this.client.uin, 'heartbeat', {
92
91
  detail_type: "heartbeat",
93
92
  interval: new Date().getTime() + this.config.heartbeat * 1000
94
93
  }));
@@ -209,27 +208,50 @@ class V12 extends events_1.EventEmitter {
209
208
  (0, fs_1.rmSync)(this.client.dir, { force: true, recursive: true });
210
209
  }
211
210
  }
212
- dispatch(data) {
213
- if (!data || typeof data !== "object")
214
- data = { args: data || [] };
215
- if (!data['post_type']) {
216
- data['sub_type'] = 'online';
217
- if (data['image']) {
218
- data['system_type'] = 'login';
219
- data['sub_type'] = 'qrcode';
220
- }
221
- else if (data['url']) {
222
- data['system_type'] = 'login';
223
- data.sub_type = 'slider';
211
+ format(event, ...args) {
212
+ const data = (typeof args[0]) === 'object' ? args.shift() || {} : {};
213
+ data.type = data.post_type;
214
+ if (!data.type) {
215
+ data.type = 'meta';
216
+ data.detail_type = 'online';
217
+ if (data.image) {
218
+ data.type = 'login';
219
+ data.detail_type = 'qrcode';
220
+ }
221
+ else if (data.url) {
222
+ data.type = 'login';
223
+ data.detail_type = 'slider';
224
224
  if (data.phone) {
225
- data.sub_type = 'device';
225
+ data.detail_type = 'device';
226
226
  }
227
227
  }
228
228
  else if (data.message) {
229
- data.system_type = 'login';
230
- data.sub_type = 'error';
229
+ data.type = 'login';
230
+ data.detial_type = 'error';
231
+ }
232
+ }
233
+ if (data.type === 'notice') {
234
+ switch (data.detail_type) {
235
+ case 'friend':
236
+ data.detail_type += data.sub_type;
237
+ break;
238
+ case 'group':
239
+ if (['increase', 'decrease'].includes(data.sub_type))
240
+ data.detail_type = 'group_member_' + data.sub_type;
241
+ else if (data.sub_type === 'recall')
242
+ data.detail_type = 'group_message_delete';
231
243
  }
232
244
  }
245
+ if (data.type === 'system')
246
+ data.type = 'meta';
247
+ data.alt_message = data.raw_message;
248
+ data.self = this.action.getSelfInfo.apply(this);
249
+ if (!data.detail_type)
250
+ data.detail_type = data.message_type || data.notice_type || data.request_type || data.system_type;
251
+ data.message = data.type === 'message' ? V12.toSegment(data.message) : data.message;
252
+ return V12.formatPayload(this.client.uin, event, data);
253
+ }
254
+ dispatch(data) {
233
255
  const payload = {
234
256
  id: (0, utils_1.uuid)(),
235
257
  impl: 'oicq_onebot',
@@ -239,23 +261,8 @@ class V12 extends events_1.EventEmitter {
239
261
  platform: 'qq',
240
262
  user_id: `${this.client.uin}`
241
263
  },
242
- type: data.post_type || 'meta',
243
- alt_message: data.raw_message,
244
- detail_type: data.message_type || data.notice_type || data.request_type || data.system_type,
245
264
  ...data,
246
265
  };
247
- if (payload.type === 'notice') {
248
- switch (payload.detail_type) {
249
- case 'friend':
250
- payload.detail_type += payload.sub_type;
251
- break;
252
- case 'group':
253
- if (['increase', 'decrease'].includes(payload.sub_type))
254
- payload.detail_type = 'group_member_' + payload.sub_type;
255
- else if (payload.sub_type === 'recall')
256
- payload.detail_type = 'group_message_delete';
257
- }
258
- }
259
266
  this.emit('dispatch', payload);
260
267
  }
261
268
  async apply(req) {
@@ -264,7 +271,7 @@ class V12 extends events_1.EventEmitter {
264
271
  let is_async = action.includes("_async");
265
272
  if (is_async)
266
273
  action = action.replace("_async", "");
267
- if (action === 'send_msg') {
274
+ if (action === 'send_message') {
268
275
  if (["private", "group", "discuss", 'channel'].includes(params.detail_type)) {
269
276
  action = "send_" + params.detail_type + "_msg";
270
277
  }
@@ -292,12 +299,7 @@ class V12 extends events_1.EventEmitter {
292
299
  if (onebot_1.BOOLS.includes(k))
293
300
  params[k] = (0, utils_1.toBool)(params[k]);
294
301
  if (k === 'message') {
295
- if (typeof params[k] === 'string') {
296
- params[k] = (0, oicq2_cq_enable_1.fromCqcode)(params[k]);
297
- }
298
- else {
299
- params[k] = (0, oicq2_cq_enable_1.fromSegment)(params[k]);
300
- }
302
+ params[k] = V12.fromSegment(params[k]);
301
303
  }
302
304
  args.push(params[k]);
303
305
  }
@@ -487,12 +489,45 @@ class V12 extends events_1.EventEmitter {
487
489
  }));
488
490
  }
489
491
  });
490
- this.dispatch(V12.genMetaEvent(this.client.uin, "connect", this.action.getVersion.apply(this)));
491
- this.dispatch(V12.genMetaEvent(this.client.uin, "status_update", this.action.getStatus.apply(this)));
492
+ this.dispatch(V12.formatPayload(this.client.uin, "connect", this.action.getVersion.apply(this)));
493
+ this.dispatch(V12.formatPayload(this.client.uin, "status_update", this.action.getStatus.apply(this)));
492
494
  }
493
495
  }
494
496
  exports.V12 = V12;
495
497
  (function (V12) {
498
+ function fromSegment(msgList) {
499
+ msgList = [].concat(msgList);
500
+ return msgList.map((msg) => {
501
+ if (typeof msg !== 'object')
502
+ msg = String(msg);
503
+ if (typeof msg === 'string') {
504
+ return { type: 'text', text: msg };
505
+ }
506
+ const { type, data, ...other } = msg;
507
+ return {
508
+ type: type.replace('mention', 'at').replace('at_all', 'at'),
509
+ ...other,
510
+ ...data
511
+ };
512
+ });
513
+ }
514
+ V12.fromSegment = fromSegment;
515
+ function toSegment(msgList) {
516
+ msgList = [].concat(msgList);
517
+ return msgList.map((msg) => {
518
+ if (typeof msg === 'string')
519
+ return { type: 'text', data: { text: msg } };
520
+ let { type, ...other } = msg;
521
+ return {
522
+ type: type === 'at' ? other['qq'] ? 'mention' : "mention_all" : type,
523
+ data: {
524
+ ...other,
525
+ user_id: other['qq']
526
+ }
527
+ };
528
+ });
529
+ }
530
+ V12.toSegment = toSegment;
496
531
  V12.defaultConfig = {
497
532
  heartbeat: 3,
498
533
  access_token: '',
@@ -524,16 +559,15 @@ exports.V12 = V12;
524
559
  };
525
560
  }
526
561
  V12.error = error;
527
- function genMetaEvent(uin, type, data) {
528
- return {
562
+ function formatPayload(uin, type, data) {
563
+ const result = {
529
564
  self_id: uin,
530
565
  time: Math.floor(Date.now() / 1000),
531
- type: "meta",
532
- status: 'ok',
533
566
  detail_type: type,
534
567
  sub_type: '',
535
568
  ...data
536
569
  };
570
+ return result;
537
571
  }
538
- V12.genMetaEvent = genMetaEvent;
572
+ V12.formatPayload = formatPayload;
539
573
  })(V12 = exports.V12 || (exports.V12 = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onebots",
3
- "version": "0.0.17",
3
+ "version": "0.0.19",
4
4
  "description": "基于oicq的多例oneBot实现",
5
5
  "main": "lib/index.js",
6
6
  "bin": {