onebots 0.4.43 → 0.4.44

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.
@@ -18,7 +18,6 @@ const app_1 = require("../../server/app");
18
18
  const service_1 = require("../../service");
19
19
  const db_1 = require("../../db");
20
20
  const sendMsgTypes = ["private", "group", "discuss"];
21
- const sendMsgMethodRegex = new RegExp(`send_(${sendMsgTypes.join("|")})_msg`);
22
21
  class V11 extends service_1.Service {
23
22
  constructor(oneBot, config) {
24
23
  super(oneBot.adapter, config);
@@ -42,6 +41,41 @@ class V11 extends service_1.Service {
42
41
  this.logger.info("");
43
42
  });
44
43
  }
44
+ transformToInt(path, value) {
45
+ if (!value || typeof value !== 'string')
46
+ throw new Error(`value must be string`);
47
+ value = value.replace(/\./g, '%46');
48
+ const obj = this.db.get(path, {});
49
+ if (obj[value])
50
+ return obj[value];
51
+ const int = (0, utils_1.randomInt)(1000, Number.MAX_SAFE_INTEGER);
52
+ const isExist = () => {
53
+ return Object.keys(obj).some((key) => {
54
+ return obj[key] === int;
55
+ });
56
+ };
57
+ // 虽然重复概率小,但还是避免下
58
+ if (isExist())
59
+ return this.transformToInt(path, value);
60
+ this.db.set(`${path}.${value}`, int);
61
+ return int;
62
+ }
63
+ transformStrToIntForObj(obj, keys) {
64
+ if (!obj)
65
+ return;
66
+ for (const key of keys) {
67
+ const value = obj[key];
68
+ if (typeof value !== 'string')
69
+ continue;
70
+ Reflect.set(obj, key, this.transformToInt(key, value));
71
+ }
72
+ }
73
+ getStrByInt(path, value) {
74
+ const obj = this.db.get(path);
75
+ return Object.keys(obj).find(str => {
76
+ return obj[str] === value;
77
+ })?.replace(/%46/g, '.') || value + '';
78
+ }
45
79
  start() {
46
80
  if (this.config.use_http)
47
81
  this.startHttp();
@@ -102,15 +136,15 @@ class V11 extends service_1.Service {
102
136
  }, this.config.heartbeat * 1000);
103
137
  }
104
138
  this.adapter.on("message.receive", (uin, event) => {
105
- const payload = this.adapter.formatEventPayload("V11", "message", event);
139
+ const payload = this.adapter.formatEventPayload(uin, "V11", "message", event);
106
140
  this.dispatch(payload);
107
141
  });
108
142
  this.adapter.on("notice.receive", (uin, event) => {
109
- const payload = this.adapter.formatEventPayload("V11", "notice", event);
143
+ const payload = this.adapter.formatEventPayload(uin, "V11", "notice", event);
110
144
  this.dispatch(payload);
111
145
  });
112
146
  this.adapter.on("request.receive", (uin, event) => {
113
- const payload = this.adapter.formatEventPayload("V11", "request", event);
147
+ const payload = this.adapter.formatEventPayload(uin, "V11", "request", event);
114
148
  this.dispatch(payload);
115
149
  });
116
150
  }
@@ -214,32 +248,11 @@ class V11 extends service_1.Service {
214
248
  if (data.message && data.post_type === "message") {
215
249
  if (this.config.post_message_format === "array") {
216
250
  data.message = this.adapter.toSegment("V11", data.message);
217
- if (data.source) { // reply
218
- let msg0 = data.message[0];
219
- msg0.data["id"] = await this.getReplyMsgIdFromDB(data);
220
- }
221
251
  }
222
252
  else {
223
253
  data.message = this.adapter.toCqcode("V11", data.message);
224
254
  }
225
255
  }
226
- if (data.message_id) {
227
- data.message_id = await this.addMsgToDB(data);
228
- }
229
- if (data.post_type == "notice" && String(data.notice_type).endsWith("_recall")) {
230
- const msgIdx = this.db.findIndex('messages', (message) => {
231
- return message.base64_id === data.base64_id;
232
- });
233
- if (msgIdx >= 0) {
234
- this.db.set(`messages.${msgIdx}.recalled`, true);
235
- this.db.set(`messages.${msgIdx}.recall_time`, parseInt((Date.now() / 1000) + ''));
236
- }
237
- }
238
- if (data.font) {
239
- const fontNo = Buffer.from(data.font).readUInt32BE();
240
- // this.db.set(`KVMap.${data.fontNo}`,data.font)
241
- data.font = fontNo;
242
- }
243
256
  data.time = Math.floor(Date.now() / 1000);
244
257
  // data = transformObj(data, (key, value) => {
245
258
  // if (!['user_id', 'group_id', 'discuss_id', 'member_id', 'channel_id', 'guild_id'].includes(key)) return value
@@ -304,51 +317,6 @@ class V11 extends service_1.Service {
304
317
  return JSON.stringify(data);
305
318
  }
306
319
  }
307
- async addMsgToDB(data) {
308
- if (!data.sender || !("user_id" in data.sender)) {
309
- // eg. notice
310
- return;
311
- }
312
- const id = (0, utils_1.randomInt)(1, Number.MAX_SAFE_INTEGER);
313
- this.db.push('messages', {
314
- id,
315
- base64_id: data.base64_id,
316
- seq: data.seq,
317
- user_id: data.sender.user_id,
318
- nickname: data.sender.nickname,
319
- group_id: data.group_id || 0,
320
- group_name: data.group_name || '',
321
- content: data.cqCode || data.message,
322
- create_time: data.time,
323
- });
324
- if (this.db.get('messages').length > 1000)
325
- this.db.shift('messages');
326
- return id;
327
- }
328
- /**
329
- * 从 send_msg_xxx() 调用的返回值中提取消息存入数据库(可以让前端在没有收到同步的message数据前就有能力拿到消息对应的base64_id)
330
- * (也有可能来的比message慢,后来的话会被数据库忽略)
331
- * @param user_id 发送者
332
- * @param group_id 群号,私聊为0
333
- * @param seq 消息序号
334
- * @param base64_id icqq返回的base64格式的消息id
335
- */
336
- async addMsgToDBFromSendMsgResult(user_id, group_id, seq, base64_id) {
337
- const id = (0, utils_1.randomInt)(1, Number.MAX_SAFE_INTEGER);
338
- this.db.push('messages', {
339
- id,
340
- base64_id,
341
- seq,
342
- user_id,
343
- nickname: '',
344
- group_id,
345
- group_name: '',
346
- content: '',
347
- });
348
- if (this.db.get('messages').length > 1000)
349
- this.db.shift('messages');
350
- return id;
351
- }
352
320
  async getReplyMsgIdFromDB(data) {
353
321
  let group_id = data.message_type === "group" ? data.group_id : 0;
354
322
  let msg = await this.db.find('messages', (message) => {
@@ -528,11 +496,6 @@ class V11 extends service_1.Service {
528
496
  */
529
497
  async apply(req) {
530
498
  let { action, params, echo } = req;
531
- if (typeof params.message_id == "number" || /^\d+$/.test(params.message_id)) {
532
- params.message_id = (await this.db.find('messages', (message) => {
533
- return params.message_id === message.id;
534
- }))?.base64_id; // 调用api时把本地的数字id转为base64发给icqq
535
- }
536
499
  action = (0, utils_1.toLine)(action);
537
500
  let is_async = action.includes("_async");
538
501
  if (is_async)
@@ -547,10 +510,6 @@ class V11 extends service_1.Service {
547
510
  action = "send_private_msg";
548
511
  else if (params.group_id)
549
512
  action = "send_group_msg";
550
- else if (params.discuss_id)
551
- action = "send_discuss_msg";
552
- else if (params.guild_id)
553
- action = params.channel_id === "direct" ? "send_direct_msg" : "send_group_msg";
554
513
  else
555
514
  throw new Error("required message_type or input (user_id/group_id)");
556
515
  }
@@ -580,7 +539,7 @@ class V11 extends service_1.Service {
580
539
  }
581
540
  params[k] = this.adapter.fromSegment("V11", params[k]);
582
541
  }
583
- params["message_id"] = params[k].find(e => e.type === "reply")?.message_id;
542
+ params["message_id"] = params[k].find(e => e.type === "reply")?.id || params['message_id'];
584
543
  }
585
544
  args.push(params[k]);
586
545
  }
@@ -614,11 +573,6 @@ class V11 extends service_1.Service {
614
573
  result.data = [...result.data.values()];
615
574
  if (result.data?.message)
616
575
  result.data.message = this.adapter.toSegment("V11", result.data.message);
617
- // send_msg_xxx 时提前把数据写入数据库(也有可能来的比message慢,后来的话会被数据库忽略)
618
- if (result.status === "ok" && action.match(sendMsgMethodRegex) && result.data?.message_id && result.data?.seq) {
619
- result.data.message_id = await this.addMsgToDBFromSendMsgResult(this.oneBot.uin, // msg send resp uin is always bot uin
620
- params.group_id || 0, result.data.seq, result.data.message_id);
621
- }
622
576
  if (echo) {
623
577
  result.echo = echo;
624
578
  }
@@ -1,14 +1,14 @@
1
1
  import { V12 } from "../index";
2
2
  export declare class FriendAction {
3
- getUserInfo(this: V12, user_id: number): Promise<any>;
3
+ getUserInfo(this: V12, user_id: number): Promise<V12.MessageRet>;
4
4
  getFriendList(this: V12): Promise<any>;
5
5
  /**
6
6
  * 发送私聊消息
7
7
  * @param user_id {number} 用户id
8
- * @param message {import('onebots/lib/service/v12').Sendable} 消息
9
- * @param source {import('onebots/lib/service/v12').SegmentElem<'reply'>} 引用内容
8
+ * @param message {V12.Sendable} 消息
9
+ * @param source {string} 引用id
10
10
  */
11
- sendPrivateMsg(this: V12, user_id: number, message: V12.Sendable, source?: V12.SegmentElem<'reply'>): Promise<any>;
11
+ sendPrivateMsg(this: V12, user_id: number, message: V12.Sendable, source?: string): Promise<any>;
12
12
  /**
13
13
  * 为指定用户点赞
14
14
  * @param user_id {number} 用户id
@@ -11,8 +11,8 @@ class FriendAction {
11
11
  /**
12
12
  * 发送私聊消息
13
13
  * @param user_id {number} 用户id
14
- * @param message {import('onebots/lib/service/v12').Sendable} 消息
15
- * @param source {import('onebots/lib/service/v12').SegmentElem<'reply'>} 引用内容
14
+ * @param message {V12.Sendable} 消息
15
+ * @param source {string} 引用id
16
16
  */
17
17
  async sendPrivateMsg(user_id, message, source) {
18
18
  return this.adapter.call(this.oneBot.uin, 'V12', 'sendPrivateMessage', [user_id, message, source]);
@@ -3,10 +3,10 @@ export declare class GroupAction {
3
3
  /**
4
4
  * 发送群聊消息
5
5
  * @param group_id {number} 群id
6
- * @param message {import('onebots/lib/service/v12').Sendable} 消息
7
- * @param source {import('onebots/lib/service/v12').SegmentElem<'reply'>} 引用内容
6
+ * @param message {V12.Sendable} 消息
7
+ * @param source {source} 引用消息id
8
8
  */
9
- sendGroupMsg(this: V12, group_id: number, message: V12.Sendable, source?: V12.SegmentElem<'reply'>): Promise<any>;
9
+ sendGroupMsg(this: V12, group_id: number, message: V12.Sendable, source?: string): Promise<V12.MessageRet>;
10
10
  /**
11
11
  * 群组踢人
12
12
  * @param group_id {number} 群id
@@ -5,8 +5,8 @@ class GroupAction {
5
5
  /**
6
6
  * 发送群聊消息
7
7
  * @param group_id {number} 群id
8
- * @param message {import('onebots/lib/service/v12').Sendable} 消息
9
- * @param source {import('onebots/lib/service/v12').SegmentElem<'reply'>} 引用内容
8
+ * @param message {V12.Sendable} 消息
9
+ * @param source {source} 引用消息id
10
10
  */
11
11
  async sendGroupMsg(group_id, message, source) {
12
12
  return this.adapter.call(this.oneBot.uin, 'V12', 'sendGroupMessage', [group_id, message, source]);
@@ -40,11 +40,11 @@ export declare class GuildAction {
40
40
  getGuildMemberList(this: V12, guild_id: string): Promise<any>;
41
41
  /**
42
42
  * 发送频道消息
43
- * @param guild_id {string} 频道id
44
- * @param channel_id {string} 子频道id
45
- * @param message {import('icqq/lib/service').Sendable} 消息
43
+ * @param channel_id {string} 通道id
44
+ * @param message {V12.Sendable} 消息
45
+ * @param source
46
46
  */
47
- sendGuildMsg(this: V12, guild_id: string, channel_id: string, message: V12.Sendable): Promise<any>;
47
+ sendGuildMsg(this: V12, channel_id: string, message: V12.Sendable, source?: string): Promise<V12.MessageRet>;
48
48
  createDirectSession(this: V12, guild_id: string, user_id: string): Promise<any>;
49
- sendDirectMsg(this: V12, guild_id: string, message: V12.Sendable): Promise<any>;
49
+ sendDirectMsg(this: V12, guild_id: string, message: V12.Sendable, source?: string): Promise<V12.MessageRet>;
50
50
  }
@@ -100,21 +100,18 @@ class GuildAction {
100
100
  }
101
101
  /**
102
102
  * 发送频道消息
103
- * @param guild_id {string} 频道id
104
- * @param channel_id {string} 子频道id
105
- * @param message {import('icqq/lib/service').Sendable} 消息
103
+ * @param channel_id {string} 通道id
104
+ * @param message {V12.Sendable} 消息
105
+ * @param source
106
106
  */
107
- async sendGuildMsg(guild_id, channel_id, message) {
108
- if (channel_id === 'direct') {
109
- return this.adapter.call(this.oneBot.uin, 'V12', 'sendDirectMessage', [guild_id, message]);
110
- }
111
- return this.adapter.call(this.oneBot.uin, 'V12', 'sendGuildMessage', [channel_id, message]);
107
+ async sendGuildMsg(channel_id, message, source) {
108
+ return this.adapter.call(this.oneBot.uin, 'V12', 'sendGuildMessage', [channel_id, message, source]);
112
109
  }
113
110
  async createDirectSession(guild_id, user_id) {
114
111
  return this.adapter.call(this.oneBot.uin, 'V12', 'createDirectSession', [guild_id, user_id]);
115
112
  }
116
- async sendDirectMsg(guild_id, message) {
117
- return this.adapter.call(this.oneBot.uin, 'V12', 'sendDirectMessage', [guild_id, message]);
113
+ async sendDirectMsg(guild_id, message, source) {
114
+ return this.adapter.call(this.oneBot.uin, 'V12', 'sendDirectMessage', [guild_id, message, source]);
118
115
  }
119
116
  }
120
117
  exports.GuildAction = GuildAction;
@@ -137,15 +137,15 @@ class V12 extends service_1.Service {
137
137
  }, this.config.heartbeat * 1000);
138
138
  }
139
139
  this.adapter.on('message.receive', (uin, event) => {
140
- const payload = this.adapter.formatEventPayload('V12', 'message', event);
140
+ const payload = this.adapter.formatEventPayload(uin, 'V12', 'message', event);
141
141
  this.dispatch(payload);
142
142
  });
143
143
  this.adapter.on('notice.receive', (uin, event) => {
144
- const payload = this.adapter.formatEventPayload('V12', 'notice', event);
144
+ const payload = this.adapter.formatEventPayload(uin, 'V12', 'notice', event);
145
145
  this.dispatch(payload);
146
146
  });
147
147
  this.adapter.on('request.receive', (uin, event) => {
148
- const payload = this.adapter.formatEventPayload('V12', 'request', event);
148
+ const payload = this.adapter.formatEventPayload(uin, 'V12', 'request', event);
149
149
  this.dispatch(payload);
150
150
  });
151
151
  }
@@ -169,7 +169,7 @@ class V12 extends service_1.Service {
169
169
  timeout: config.timeout || this.config.request_timeout,
170
170
  headers: {
171
171
  "Content-Type": "application/json",
172
- "User-Agent": "OneBot/12 (qq) Node-onebots/" + utils_1.version,
172
+ "User-Agent": "OneBot/12 Node-onebots/" + 12,
173
173
  "X-OneBot-Version": 12,
174
174
  "X-Impl": "onebots",
175
175
  },
@@ -387,7 +387,7 @@ class V12 extends service_1.Service {
387
387
  if (is_async)
388
388
  action = action.replace("_async", "");
389
389
  if (action === 'send_message') {
390
- if (["private", "group", "discuss", 'channel'].includes(params.detail_type)) {
390
+ if (["private", "group", "discuss", 'direct', 'guild'].includes(params.detail_type)) {
391
391
  action = "send_" + params.detail_type + "_msg";
392
392
  }
393
393
  else if (params.user_id)
@@ -396,10 +396,12 @@ class V12 extends service_1.Service {
396
396
  action = "send_group_msg";
397
397
  else if (params.discuss_id)
398
398
  action = "send_discuss_msg";
399
- else if (params.channel_id && params.guild_id)
399
+ else if (params.channel_id)
400
400
  action = "send_guild_msg";
401
+ else if (params.guild_id)
402
+ action = 'send_direct_msg';
401
403
  else
402
- throw new Error('required detail_type or input (user_id/group_id/(guild_id and channel_id))');
404
+ throw new Error('required detail_type or input (user_id/group_id/guild_id/channel_id)');
403
405
  }
404
406
  const method = (0, utils_2.toHump)(action);
405
407
  if (Reflect.has(this.action, method)) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "onebots",
3
- "version": "0.4.43",
3
+ "version": "0.4.44",
4
4
  "description": "基于icqq的多例oneBot实现",
5
5
  "engines": {
6
6
  "node": ">=16"
@@ -70,8 +70,8 @@
70
70
  "peerDependencies": {
71
71
  "icqq": "latest",
72
72
  "lib-wechat": "latest",
73
- "node-dd-bot": "latest",
74
- "qq-group-bot": "latest"
73
+ "node-dd-bot": "^1.0.8",
74
+ "qq-group-bot": "^1.0.9"
75
75
  },
76
76
  "dependencies": {
77
77
  "@koa/router": "^10.1.1",