onebots 0.1.0 → 0.1.2

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.
@@ -68,7 +68,7 @@ export declare namespace V11 {
68
68
  function error(error: string, retcode?: number): Result<any>;
69
69
  const defaultConfig: Config;
70
70
  function genMetaEvent(uin: number, type: string): {
71
- self_id: number;
71
+ self_id: string;
72
72
  time: number;
73
73
  post_type: string;
74
74
  meta_event_type: string;
@@ -28,10 +28,10 @@ class V11 extends events_1.EventEmitter {
28
28
  this.queue_running = false;
29
29
  this.wsr = new Set();
30
30
  this.action = new action_1.Action();
31
- this.logger = this.oneBot.app.getLogger(this.client.uin, this.version);
31
+ this.logger = this.oneBot.app.getLogger(this.oneBot.uin, this.version);
32
32
  }
33
33
  start(path) {
34
- this.path = `/${this.client.uin}`;
34
+ this.path = `/${this.oneBot.uin}`;
35
35
  if (path)
36
36
  this.path += path;
37
37
  if (this.config.use_http)
@@ -61,7 +61,7 @@ class V11 extends events_1.EventEmitter {
61
61
  if (this.config.heartbeat) {
62
62
  this.heartbeat = setInterval(() => {
63
63
  this.dispatch({
64
- self_id: this.client.uin,
64
+ self_id: this.oneBot.uin + '',
65
65
  status: {
66
66
  online: this.client.status === icqq_1.OnlineStatus.Online,
67
67
  good: this.oneBot.status === onebot_1.OneBotStatus.Good
@@ -87,7 +87,7 @@ class V11 extends events_1.EventEmitter {
87
87
  headers: {
88
88
  "Content-Type": "application/json",
89
89
  "Content-Length": Buffer.byteLength(serialized),
90
- "X-Self-ID": String(this.client.uin),
90
+ "X-Self-ID": String(this.oneBot.uin),
91
91
  "User-Agent": "OneBot",
92
92
  },
93
93
  };
@@ -193,6 +193,11 @@ class V11 extends events_1.EventEmitter {
193
193
  data.message = this.config.post_message_format === 'array' ? (0, icqq_cq_enable_1.toSegment)(data.message) : (0, icqq_cq_enable_1.toCqcode)(data);
194
194
  }
195
195
  data.time = Math.floor(Date.now() / 1000);
196
+ data = (0, utils_1.transformObj)(data, (key, value) => {
197
+ if (!['user_id', 'group_id', 'discuss_id', 'member_id', 'channel_id', 'guild_id'].includes(key))
198
+ return value;
199
+ return value + '';
200
+ });
196
201
  this.emit('dispatch', JSON.stringify(data));
197
202
  }
198
203
  async _httpRequestHandler(ctx) {
@@ -291,8 +296,8 @@ class V11 extends events_1.EventEmitter {
291
296
  }));
292
297
  }
293
298
  });
294
- ws.send(JSON.stringify(V11.genMetaEvent(this.client.uin, "connect")));
295
- ws.send(JSON.stringify(V11.genMetaEvent(this.client.uin, "enable")));
299
+ ws.send(JSON.stringify(V11.genMetaEvent(this.oneBot.uin, "connect")));
300
+ ws.send(JSON.stringify(V11.genMetaEvent(this.oneBot.uin, "enable")));
296
301
  }
297
302
  /**
298
303
  * 创建反向ws
@@ -300,7 +305,7 @@ class V11 extends events_1.EventEmitter {
300
305
  _createWsr(url) {
301
306
  const timestmap = Date.now();
302
307
  const headers = {
303
- "X-Self-ID": String(this.client.uin),
308
+ "X-Self-ID": String(this.oneBot.uin),
304
309
  "X-Client-Role": "Universal",
305
310
  "User-Agent": "OneBot",
306
311
  };
@@ -492,7 +497,7 @@ exports.V11 = V11;
492
497
  };
493
498
  function genMetaEvent(uin, type) {
494
499
  return {
495
- self_id: uin,
500
+ self_id: uin + '',
496
501
  time: Math.floor(Date.now() / 1000),
497
502
  post_type: "meta_event",
498
503
  meta_event_type: "lifecycle",
@@ -32,7 +32,7 @@ export declare class V12 extends EventEmitter implements OneBot.Base {
32
32
  private startWsReverse;
33
33
  stop(force?: boolean): Promise<void>;
34
34
  format<E extends keyof V12.BotEventMap>(event: E, ...args: [V12.BotEventMap[E]]): {
35
- self_id: number;
35
+ self_id: string;
36
36
  time: number;
37
37
  detail_type: E;
38
38
  type: string;
@@ -183,7 +183,7 @@ export declare namespace V12 {
183
183
  function success<T extends any>(data: T, retcode?: Result<T>['retcode'], echo?: string): Result<T>;
184
184
  function error(message: string, retcode?: Result<null>['retcode'], echo?: string): Result<null>;
185
185
  function formatPayload<K extends keyof BotEventMap>(uin: number, type: K, data: Omit<BotEventMap[K], K>): {
186
- self_id: number;
186
+ self_id: string;
187
187
  time: number;
188
188
  detail_type: K;
189
189
  type: string;
@@ -26,11 +26,11 @@ class V12 extends events_1.EventEmitter {
26
26
  this.version = 'V12';
27
27
  this.timestamp = Date.now();
28
28
  this.wsr = new Set();
29
- this.db = new db_1.Db((0, path_1.join)(app_1.App.configDir, 'data', this.client.uin + '.json'));
29
+ this.db = new db_1.Db((0, path_1.join)(app_1.App.configDir, 'data', this.oneBot.uin + '.json'));
30
30
  if (!this.history)
31
31
  this.history = [];
32
32
  this.action = new action_1.Action();
33
- this.logger = this.oneBot.app.getLogger(this.client.uin, this.version);
33
+ this.logger = this.oneBot.app.getLogger(this.oneBot.uin, this.version);
34
34
  }
35
35
  get history() {
36
36
  return this.db.get('eventBuffer');
@@ -39,7 +39,7 @@ class V12 extends events_1.EventEmitter {
39
39
  this.db.set('eventBuffer', value);
40
40
  }
41
41
  start(path) {
42
- this.path = `/${this.client.uin}`;
42
+ this.path = `/${this.oneBot.uin}`;
43
43
  if (path)
44
44
  this.path += path;
45
45
  if (this.config.use_http) {
@@ -90,7 +90,7 @@ class V12 extends events_1.EventEmitter {
90
90
  });
91
91
  if (this.config.heartbeat) {
92
92
  this.heartbeat = setInterval(() => {
93
- this.dispatch(V12.formatPayload(this.client.uin, 'heartbeat', {
93
+ this.dispatch(V12.formatPayload(this.oneBot.uin, 'heartbeat', {
94
94
  detail_type: "heartbeat",
95
95
  interval: new Date().getTime() + this.config.heartbeat * 1000
96
96
  }));
@@ -313,7 +313,7 @@ class V12 extends events_1.EventEmitter {
313
313
  if (!data.detail_type)
314
314
  data.detail_type = data.message_type || data.notice_type || data.request_type || data.system_type;
315
315
  data.message = data.type === 'message' ? V12.toSegment(data.message) : data.message;
316
- return V12.formatPayload(this.client.uin, event, data);
316
+ return V12.formatPayload(this.oneBot.uin, event, data);
317
317
  }
318
318
  dispatch(data) {
319
319
  const payload = {
@@ -323,9 +323,13 @@ class V12 extends events_1.EventEmitter {
323
323
  platform: 'qq',
324
324
  self: {
325
325
  platform: 'qq',
326
- user_id: `${this.client.uin}`
326
+ user_id: `${this.oneBot.uin}`
327
327
  },
328
- ...data,
328
+ ...(0, utils_1.transformObj)(data, (key, value) => {
329
+ if (!['user_id', 'group_id', 'discuss_id', 'member_id', 'channel_id', 'guild_id'].includes(key))
330
+ return value;
331
+ return value + '';
332
+ }),
329
333
  };
330
334
  this.emit('dispatch', payload);
331
335
  }
@@ -478,7 +482,7 @@ class V12 extends events_1.EventEmitter {
478
482
  _createWsr(url, config) {
479
483
  const timestmap = Date.now();
480
484
  const headers = {
481
- "X-Self-ID": String(this.client.uin),
485
+ "X-Self-ID": String(this.oneBot.uin),
482
486
  "X-Client-Role": "Universal",
483
487
  "User-Agent": "OneBot/12 (qq) Node-onebots/0.0.15",
484
488
  "Sec-WebSocket-Protocol": "12.onebots.v0.0.15"
@@ -554,8 +558,8 @@ class V12 extends events_1.EventEmitter {
554
558
  }));
555
559
  }
556
560
  });
557
- this.dispatch(V12.formatPayload(this.client.uin, "connect", this.action.getVersion.apply(this)));
558
- this.dispatch(V12.formatPayload(this.client.uin, "status_update", this.action.getStatus.apply(this)));
561
+ this.dispatch(V12.formatPayload(this.oneBot.uin, "connect", this.action.getVersion.apply(this)));
562
+ this.dispatch(V12.formatPayload(this.oneBot.uin, "status_update", this.action.getStatus.apply(this)));
559
563
  }
560
564
  }
561
565
  exports.V12 = V12;
@@ -628,7 +632,7 @@ exports.V12 = V12;
628
632
  V12.error = error;
629
633
  function formatPayload(uin, type, data) {
630
634
  return {
631
- self_id: uin,
635
+ self_id: uin + '',
632
636
  time: Math.floor(Date.now() / 1000),
633
637
  detail_type: type,
634
638
  type: 'meta',
package/lib/utils.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export declare function deepMerge<T extends any>(base: T, ...from: T[]): T;
2
+ export declare function transformObj(obj: any, callback: any): any;
2
3
  export declare function deepClone<T extends any>(obj: T): T;
3
4
  export declare function pick<T extends object, K extends keyof T>(source: T, keys?: Iterable<K>, forced?: boolean): Pick<T, K>;
4
5
  export declare function omit<T, K extends keyof T>(source: T, keys?: Iterable<K>): Omit<T, K>;
package/lib/utils.js CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.getProperties = exports.protectedFields = exports.uuid = exports.toBool = exports.toLine = exports.remove = exports.toHump = exports.Mixin = exports.omit = exports.pick = exports.deepClone = exports.deepMerge = void 0;
26
+ exports.getProperties = exports.protectedFields = exports.uuid = exports.toBool = exports.toLine = exports.remove = exports.toHump = exports.Mixin = exports.omit = exports.pick = exports.deepClone = exports.transformObj = exports.deepMerge = void 0;
27
27
  const crypto = __importStar(require("crypto"));
28
28
  // 合并对象/数组
29
29
  function deepMerge(base, ...from) {
@@ -57,6 +57,18 @@ function deepMerge(base, ...from) {
57
57
  return base;
58
58
  }
59
59
  exports.deepMerge = deepMerge;
60
+ function transformObj(obj, callback) {
61
+ if (!obj)
62
+ return obj;
63
+ if (Array.isArray(obj))
64
+ return obj.map(item => transformObj(item, callback));
65
+ if (typeof obj !== 'object')
66
+ return obj;
67
+ return Object.fromEntries(Object.keys(obj).map(key => {
68
+ return [key, callback(key, obj[key])];
69
+ }));
70
+ }
71
+ exports.transformObj = transformObj;
60
72
  // 深拷贝
61
73
  function deepClone(obj) {
62
74
  if (typeof obj !== 'object')
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onebots",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "基于icqq的多例oneBot实现",
5
5
  "engines": {
6
6
  "node": ">=16"