onebots 0.0.16 → 0.0.18

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.
@@ -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";
@@ -46,6 +46,47 @@ export declare class V12 extends EventEmitter implements OneBot.Base {
46
46
  protected _webSocketHandler(ws: WebSocket): void;
47
47
  }
48
48
  export declare namespace V12 {
49
+ function fromSegment(msgList: SegmentElem | string | number | (SegmentElem | string | number)[]): MessageElem[];
50
+ function toSegment(msgList: Sendable): SegmentElem<keyof SegmentMap>[];
51
+ interface SegmentMap {
52
+ face: {
53
+ id: number;
54
+ text: string;
55
+ };
56
+ text: {
57
+ text: string;
58
+ };
59
+ mention: {
60
+ user_id: string;
61
+ };
62
+ mention_all: null;
63
+ image: {
64
+ file_id: string;
65
+ };
66
+ voice: {
67
+ file_id: string;
68
+ };
69
+ audio: {
70
+ file_id: string;
71
+ };
72
+ file: {
73
+ file_id: string;
74
+ };
75
+ location: {
76
+ latitude: number;
77
+ longitude: number;
78
+ title: string;
79
+ content: string;
80
+ };
81
+ reply: {
82
+ message_id: string;
83
+ user_id: string;
84
+ };
85
+ }
86
+ type SegmentElem<K extends keyof SegmentMap = keyof SegmentMap> = {
87
+ type: K;
88
+ data: SegmentMap[K];
89
+ };
49
90
  interface Config {
50
91
  heartbeat?: number;
51
92
  access_token?: string;
@@ -94,7 +135,8 @@ export declare namespace V12 {
94
135
  params: any;
95
136
  echo?: string;
96
137
  }
97
- type MetaEventMap = {
138
+ type BotEventMap = {
139
+ system: Record<string, any>;
98
140
  connect: {
99
141
  detail_type: 'connect';
100
142
  version: ReturnType<Action['getVersion']>;
@@ -107,15 +149,25 @@ export declare namespace V12 {
107
149
  detail_type: 'status_update';
108
150
  status: ReturnType<Action['getStatus']>;
109
151
  };
152
+ } & TransformEventMap;
153
+ type TransformEventMap = {
154
+ [P in keyof EventMap]: TransformEventParams<Parameters<EventMap[P]>>;
155
+ };
156
+ type TransformEventParams<T extends any[]> = T extends [infer L, ...infer R] ? L extends object ? L & {
157
+ args: R;
158
+ } : {
159
+ args: [L, ...R];
160
+ } : {
161
+ args: T;
110
162
  };
111
163
  function success<T extends any>(data: T, retcode?: Result<T>['retcode'], echo?: string): Result<T>;
112
164
  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>): {
165
+ function genMetaEvent<K extends keyof BotEventMap>(uin: number, type: K, data: Omit<BotEventMap[K], K>): {
114
166
  self_id: number;
115
167
  time: number;
116
168
  type: string;
117
169
  status: string;
118
170
  detail_type: K;
119
171
  sub_type: string;
120
- } & Omit<MetaEventMap[K], K>;
172
+ } & Omit<BotEventMap[K], K>;
121
173
  }
@@ -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");
@@ -190,8 +189,14 @@ class V12 extends events_1.EventEmitter {
190
189
  startWsReverse(config) {
191
190
  const ws = this._createWsr(config.url, config);
192
191
  this.on('dispatch', (unserialized) => {
192
+ const serialized = JSON.stringify(unserialized);
193
193
  if (this.wsr.has(ws)) {
194
- ws.send(JSON.stringify(unserialized));
194
+ ws.send(serialized, (err) => {
195
+ if (err)
196
+ this.logger.error(`反向WS(${ws.url})上报事件失败: ` + err.message);
197
+ else
198
+ this.logger.debug(`反向WS(${ws.url})上报事件成功: ` + serialized);
199
+ });
195
200
  }
196
201
  });
197
202
  }
@@ -206,22 +211,23 @@ class V12 extends events_1.EventEmitter {
206
211
  dispatch(data) {
207
212
  if (!data || typeof data !== "object")
208
213
  data = { args: data || [] };
209
- if (!data['post_type']) {
210
- data['sub_type'] = 'online';
211
- if (data['image']) {
212
- data['system_type'] = 'login';
213
- data['sub_type'] = 'qrcode';
214
- }
215
- else if (data['url']) {
216
- data['system_type'] = 'login';
217
- data.sub_type = 'slider';
214
+ data.type = data.post_type;
215
+ if (!data.type) {
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';
218
224
  if (data.phone) {
219
- data.sub_type = 'device';
225
+ data.detail_type = 'device';
220
226
  }
221
227
  }
222
228
  else if (data.message) {
223
- data.system_type = 'login';
224
- data.sub_type = 'error';
229
+ data.type = 'login';
230
+ data.detial_type = 'error';
225
231
  }
226
232
  }
227
233
  const payload = {
@@ -233,10 +239,11 @@ class V12 extends events_1.EventEmitter {
233
239
  platform: 'qq',
234
240
  user_id: `${this.client.uin}`
235
241
  },
236
- type: data.post_type || 'meta',
237
242
  alt_message: data.raw_message,
238
243
  detail_type: data.message_type || data.notice_type || data.request_type || data.system_type,
239
- ...data,
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',
240
247
  };
241
248
  if (payload.type === 'notice') {
242
249
  switch (payload.detail_type) {
@@ -286,12 +293,7 @@ class V12 extends events_1.EventEmitter {
286
293
  if (onebot_1.BOOLS.includes(k))
287
294
  params[k] = (0, utils_1.toBool)(params[k]);
288
295
  if (k === 'message') {
289
- if (typeof params[k] === 'string') {
290
- params[k] = (0, oicq2_cq_enable_1.fromCqcode)(params[k]);
291
- }
292
- else {
293
- params[k] = (0, oicq2_cq_enable_1.fromSegment)(params[k]);
294
- }
296
+ params[k] = V12.fromSegment(params[k]);
295
297
  }
296
298
  args.push(params[k]);
297
299
  }
@@ -487,6 +489,39 @@ class V12 extends events_1.EventEmitter {
487
489
  }
488
490
  exports.V12 = V12;
489
491
  (function (V12) {
492
+ function fromSegment(msgList) {
493
+ msgList = [].concat(msgList);
494
+ return msgList.map((msg) => {
495
+ if (typeof msg !== 'object')
496
+ msg = String(msg);
497
+ if (typeof msg === 'string') {
498
+ return { type: 'text', text: msg };
499
+ }
500
+ const { type, data, ...other } = msg;
501
+ return {
502
+ type: type.replace('mention', 'at').replace('at_all', 'at'),
503
+ ...other,
504
+ ...data
505
+ };
506
+ });
507
+ }
508
+ V12.fromSegment = fromSegment;
509
+ function toSegment(msgList) {
510
+ msgList = [].concat(msgList);
511
+ return msgList.map((msg) => {
512
+ if (typeof msg === 'string')
513
+ return { type: 'text', data: { text: msg } };
514
+ let { type, ...other } = msg;
515
+ return {
516
+ type: type === 'at' ? other['qq'] ? 'mention' : "mention_all" : type,
517
+ data: {
518
+ ...other,
519
+ user_id: other['qq']
520
+ }
521
+ };
522
+ });
523
+ }
524
+ V12.toSegment = toSegment;
490
525
  V12.defaultConfig = {
491
526
  heartbeat: 3,
492
527
  access_token: '',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onebots",
3
- "version": "0.0.16",
3
+ "version": "0.0.18",
4
4
  "description": "基于oicq的多例oneBot实现",
5
5
  "main": "lib/index.js",
6
6
  "bin": {