onebots 0.0.18 → 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
  };
@@ -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;
@@ -162,11 +168,9 @@ export declare namespace V12 {
162
168
  };
163
169
  function success<T extends any>(data: T, retcode?: Result<T>['retcode'], echo?: string): Result<T>;
164
170
  function error(message: string, retcode?: Result<null>['retcode'], echo?: string): Result<null>;
165
- function genMetaEvent<K extends keyof BotEventMap>(uin: number, type: K, data: Omit<BotEventMap[K], K>): {
171
+ function formatPayload<K extends keyof BotEventMap>(uin: number, type: K, data: Omit<BotEventMap[K], K>): {
166
172
  self_id: number;
167
173
  time: number;
168
- type: string;
169
- status: string;
170
174
  detail_type: K;
171
175
  sub_type: string;
172
176
  } & Omit<BotEventMap[K], K>;
@@ -87,7 +87,7 @@ class V12 extends events_1.EventEmitter {
87
87
  });
88
88
  if (this.config.heartbeat) {
89
89
  this.heartbeat = setInterval(() => {
90
- this.dispatch(V12.genMetaEvent(this.client.uin, 'heartbeat', {
90
+ this.dispatch(V12.formatPayload(this.client.uin, 'heartbeat', {
91
91
  detail_type: "heartbeat",
92
92
  interval: new Date().getTime() + this.config.heartbeat * 1000
93
93
  }));
@@ -208,11 +208,11 @@ class V12 extends events_1.EventEmitter {
208
208
  (0, fs_1.rmSync)(this.client.dir, { force: true, recursive: true });
209
209
  }
210
210
  }
211
- dispatch(data) {
212
- if (!data || typeof data !== "object")
213
- data = { args: data || [] };
211
+ format(event, ...args) {
212
+ const data = (typeof args[0]) === 'object' ? args.shift() || {} : {};
214
213
  data.type = data.post_type;
215
214
  if (!data.type) {
215
+ data.type = 'meta';
216
216
  data.detail_type = 'online';
217
217
  if (data.image) {
218
218
  data.type = 'login';
@@ -230,6 +230,28 @@ class V12 extends events_1.EventEmitter {
230
230
  data.detial_type = 'error';
231
231
  }
232
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';
243
+ }
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,24 +261,8 @@ class V12 extends events_1.EventEmitter {
239
261
  platform: 'qq',
240
262
  user_id: `${this.client.uin}`
241
263
  },
242
- alt_message: data.raw_message,
243
- detail_type: data.message_type || data.notice_type || data.request_type || data.system_type,
244
- ...V12.genMetaEvent(this.client.uin, data.type, data),
245
- message: data.type === 'message' ? V12.toSegment(data.message) : data.message,
246
- type: data.type || 'meta',
264
+ ...data,
247
265
  };
248
- if (payload.type === 'notice') {
249
- switch (payload.detail_type) {
250
- case 'friend':
251
- payload.detail_type += payload.sub_type;
252
- break;
253
- case 'group':
254
- if (['increase', 'decrease'].includes(payload.sub_type))
255
- payload.detail_type = 'group_member_' + payload.sub_type;
256
- else if (payload.sub_type === 'recall')
257
- payload.detail_type = 'group_message_delete';
258
- }
259
- }
260
266
  this.emit('dispatch', payload);
261
267
  }
262
268
  async apply(req) {
@@ -265,7 +271,7 @@ class V12 extends events_1.EventEmitter {
265
271
  let is_async = action.includes("_async");
266
272
  if (is_async)
267
273
  action = action.replace("_async", "");
268
- if (action === 'send_msg') {
274
+ if (action === 'send_message') {
269
275
  if (["private", "group", "discuss", 'channel'].includes(params.detail_type)) {
270
276
  action = "send_" + params.detail_type + "_msg";
271
277
  }
@@ -483,8 +489,8 @@ class V12 extends events_1.EventEmitter {
483
489
  }));
484
490
  }
485
491
  });
486
- this.dispatch(V12.genMetaEvent(this.client.uin, "connect", this.action.getVersion.apply(this)));
487
- 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)));
488
494
  }
489
495
  }
490
496
  exports.V12 = V12;
@@ -553,16 +559,15 @@ exports.V12 = V12;
553
559
  };
554
560
  }
555
561
  V12.error = error;
556
- function genMetaEvent(uin, type, data) {
557
- return {
562
+ function formatPayload(uin, type, data) {
563
+ const result = {
558
564
  self_id: uin,
559
565
  time: Math.floor(Date.now() / 1000),
560
- type: "meta",
561
- status: 'ok',
562
566
  detail_type: type,
563
567
  sub_type: '',
564
568
  ...data
565
569
  };
570
+ return result;
566
571
  }
567
- V12.genMetaEvent = genMetaEvent;
572
+ V12.formatPayload = formatPayload;
568
573
  })(V12 = exports.V12 || (exports.V12 = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onebots",
3
- "version": "0.0.18",
3
+ "version": "0.0.19",
4
4
  "description": "基于oicq的多例oneBot实现",
5
5
  "main": "lib/index.js",
6
6
  "bin": {