onebots 0.4.22 → 0.4.25

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.
Files changed (84) hide show
  1. package/README.md +129 -72
  2. package/lib/adapter.d.ts +55 -0
  3. package/lib/adapter.js +60 -0
  4. package/lib/adapters/icqq/index.d.ts +36 -0
  5. package/lib/adapters/icqq/index.js +300 -0
  6. package/lib/adapters/icqq/utils.d.ts +6 -0
  7. package/lib/adapters/icqq/utils.js +26 -0
  8. package/lib/adapters/qq/bot.d.ts +24 -0
  9. package/lib/adapters/qq/bot.js +20 -0
  10. package/lib/adapters/qq/constans.d.ts +69 -0
  11. package/lib/adapters/qq/constans.js +128 -0
  12. package/lib/adapters/qq/elements.d.ts +125 -0
  13. package/lib/adapters/qq/elements.js +8 -0
  14. package/lib/adapters/qq/entries/channel.d.ts +17 -0
  15. package/lib/adapters/qq/entries/channel.js +2 -0
  16. package/lib/adapters/qq/entries/friend.d.ts +8 -0
  17. package/lib/adapters/qq/entries/friend.js +2 -0
  18. package/lib/adapters/qq/entries/group.d.ts +6 -0
  19. package/lib/adapters/qq/entries/group.js +2 -0
  20. package/lib/adapters/qq/entries/groupMember.d.ts +7 -0
  21. package/lib/adapters/qq/entries/groupMember.js +2 -0
  22. package/lib/adapters/qq/entries/guild.d.ts +21 -0
  23. package/lib/adapters/qq/entries/guild.js +2 -0
  24. package/lib/adapters/qq/entries/guildMember.d.ts +9 -0
  25. package/lib/adapters/qq/entries/guildMember.js +2 -0
  26. package/lib/adapters/qq/entries/user.d.ts +9 -0
  27. package/lib/adapters/qq/entries/user.js +2 -0
  28. package/lib/adapters/qq/event.d.ts +28 -0
  29. package/lib/adapters/qq/event.js +24 -0
  30. package/lib/adapters/qq/index.d.ts +35 -0
  31. package/lib/adapters/qq/index.js +170 -0
  32. package/lib/adapters/qq/message.d.ts +83 -0
  33. package/lib/adapters/qq/message.js +272 -0
  34. package/lib/adapters/qq/qqBot.d.ts +122 -0
  35. package/lib/adapters/qq/qqBot.js +438 -0
  36. package/lib/adapters/qq/sessionManager.d.ts +33 -0
  37. package/lib/adapters/qq/sessionManager.js +257 -0
  38. package/lib/adapters/qq/types.d.ts +45 -0
  39. package/lib/adapters/qq/types.js +2 -0
  40. package/lib/adapters/qq/utils.d.ts +12 -0
  41. package/lib/adapters/qq/utils.js +86 -0
  42. package/lib/bin.js +17 -2
  43. package/lib/config.sample.yaml +11 -5
  44. package/lib/db.js +6 -6
  45. package/lib/index.d.ts +5 -5
  46. package/lib/onebot.d.ts +30 -22
  47. package/lib/onebot.js +42 -165
  48. package/lib/server/app.d.ts +22 -12
  49. package/lib/server/app.js +97 -71
  50. package/lib/service/V11/action/common.d.ts +11 -12
  51. package/lib/service/V11/action/common.js +19 -65
  52. package/lib/service/V11/action/friend.d.ts +6 -14
  53. package/lib/service/V11/action/friend.js +6 -16
  54. package/lib/service/V11/action/group.d.ts +20 -23
  55. package/lib/service/V11/action/group.js +20 -30
  56. package/lib/service/V11/action/index.d.ts +1 -1
  57. package/lib/service/V11/db_entities.d.ts +1 -1
  58. package/lib/service/V11/db_entities.js +1 -1
  59. package/lib/service/V11/db_sqlite.d.ts +1 -1
  60. package/lib/service/V11/db_sqlite.js +5 -7
  61. package/lib/service/V11/index.d.ts +46 -14
  62. package/lib/service/V11/index.js +163 -134
  63. package/lib/service/V12/action/common.d.ts +3 -67
  64. package/lib/service/V12/action/common.js +9 -63
  65. package/lib/service/V12/action/friend.d.ts +4 -10
  66. package/lib/service/V12/action/friend.js +4 -12
  67. package/lib/service/V12/action/group.d.ts +19 -21
  68. package/lib/service/V12/action/group.js +19 -27
  69. package/lib/service/V12/action/guild.d.ts +40 -13
  70. package/lib/service/V12/action/guild.js +99 -11
  71. package/lib/service/V12/action/index.d.ts +1 -1
  72. package/lib/service/V12/index.d.ts +37 -13
  73. package/lib/service/V12/index.js +63 -94
  74. package/lib/service.d.ts +7 -2
  75. package/lib/service.js +5 -1
  76. package/lib/types.d.ts +2 -2
  77. package/lib/types.js +3 -3
  78. package/lib/utils.d.ts +7 -4
  79. package/lib/utils.js +18 -1
  80. package/package.json +74 -70
  81. package/lib/service/V11/utils.d.ts +0 -9
  82. package/lib/service/V11/utils.js +0 -31
  83. package/lib/service/V12/utils.d.ts +0 -8
  84. package/lib/service/V12/utils.js +0 -47
@@ -4,70 +4,114 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.V11 = void 0;
7
- const icqq_1 = require("icqq");
8
7
  const action_1 = require("./action");
9
8
  const onebot_1 = require("../../onebot");
9
+ const crypto_1 = __importDefault(require("crypto"));
10
10
  const ws_1 = require("ws");
11
11
  const url_1 = require("url");
12
12
  const utils_1 = require("../../utils");
13
- const icqq_cq_enable_1 = require("icqq-cq-enable");
14
13
  const onebot_2 = require("../../onebot");
15
14
  const http_1 = __importDefault(require("http"));
16
15
  const https_1 = __importDefault(require("https"));
17
- const fs_1 = require("fs");
18
16
  const db_sqlite_1 = require("./db_sqlite");
19
17
  const path_1 = require("path");
20
18
  const app_1 = require("../../server/app");
21
19
  const db_entities_1 = require("./db_entities");
22
20
  const service_1 = require("../../service");
21
+ const sendMsgTypes = ["private", "group", "discuss"];
22
+ const sendMsgMethodRegex = new RegExp(`send_(${sendMsgTypes.join("|")})_msg`);
23
23
  class V11 extends service_1.Service {
24
- constructor(oneBot, client, config) {
25
- super(config);
24
+ constructor(oneBot, config) {
25
+ super(oneBot.adapter, config);
26
26
  this.oneBot = oneBot;
27
- this.client = client;
27
+ this.config = config;
28
28
  this.version = 'V11';
29
29
  this.timestamp = Date.now();
30
30
  this._queue = [];
31
31
  this.queue_running = false;
32
32
  this.wsr = new Set();
33
33
  this.action = new action_1.Action();
34
- this.logger = this.oneBot.app.getLogger(this.oneBot.uin, this.version);
34
+ this.logger = this.oneBot.adapter.getLogger(this.oneBot.uin, this.version);
35
35
  this.db = new db_sqlite_1.Database((0, path_1.join)(app_1.App.configDir, 'data', this.oneBot.uin + '.db'), this.logger);
36
+ this.oneBot.on('online', async () => {
37
+ this.logger.info("【好友列表】");
38
+ const friendList = await this.oneBot.getFriendList('V11');
39
+ friendList.forEach((item) => this.logger.info(`\t${item.user_name}(${item.user_id})`));
40
+ this.logger.info("【群列表】");
41
+ const groupList = await this.oneBot.getGroupList('V11');
42
+ groupList.forEach(item => this.logger.info(`\t${item.group_name}(${item.group_id})`));
43
+ this.logger.info('');
44
+ });
36
45
  }
37
- start(path) {
38
- this.path = `/${this.oneBot.uin}`;
39
- if (path)
40
- this.path += path;
46
+ start() {
41
47
  if (this.config.use_http)
42
48
  this.startHttp();
43
49
  if (this.config.use_ws)
44
50
  this.startWs();
45
- this.config.http_reverse.forEach(config => {
46
- if (typeof config === 'string') {
51
+ this.config.http_reverse.forEach((config) => {
52
+ if (typeof config === "string") {
47
53
  config = {
48
54
  url: config,
49
55
  access_token: this.config.access_token,
50
- secret: this.config.secret
56
+ secret: this.config.secret,
51
57
  };
52
58
  }
53
59
  else {
54
60
  config = {
55
61
  access_token: this.config.access_token,
56
62
  secret: this.config.secret,
57
- ...config
63
+ ...config,
58
64
  };
59
65
  }
60
66
  this.startHttpReverse(config);
61
67
  });
62
- this.config.ws_reverse.forEach(config => {
68
+ this.config.ws_reverse.forEach((config) => {
63
69
  this.startWsReverse(config);
64
70
  });
71
+ this.on("dispatch", (serialized) => {
72
+ for (const ws of this.wss.clients) {
73
+ ws.send(serialized, (err) => {
74
+ if (err)
75
+ this.logger.error(`正向WS(${ws.url})上报事件失败: ` + err.message);
76
+ else
77
+ this.logger.debug(`正向WS(${ws.url})上报事件成功: ` + serialized);
78
+ });
79
+ }
80
+ for (const ws of this.wsr) {
81
+ ws.send(serialized, (err) => {
82
+ if (err) {
83
+ this.logger.error(`反向WS(${ws.url})上报事件失败: ` + err.message);
84
+ }
85
+ else
86
+ this.logger.debug(`反向WS(${ws.url})上报事件成功: ` + serialized);
87
+ });
88
+ }
89
+ });
90
+ this.on("dispatch", (serialized) => {
91
+ for (const ws of this.wss.clients) {
92
+ ws.send(serialized, (err) => {
93
+ if (err)
94
+ this.logger.error(`正向WS(${ws.url})上报事件失败: ` + err.message);
95
+ else
96
+ this.logger.debug(`正向WS(${ws.url})上报事件成功: ` + serialized);
97
+ });
98
+ }
99
+ for (const ws of this.wsr) {
100
+ ws.send(serialized, (err) => {
101
+ if (err) {
102
+ this.logger.error(`反向WS(${ws.url})上报事件失败: ` + err.message);
103
+ }
104
+ else
105
+ this.logger.debug(`反向WS(${ws.url})上报事件成功: ` + serialized);
106
+ });
107
+ }
108
+ });
65
109
  if (this.config.heartbeat) {
66
110
  this.heartbeat = setInterval(() => {
67
111
  this.dispatch({
68
112
  self_id: this.oneBot.uin,
69
113
  status: {
70
- online: this.client.status === icqq_1.OnlineStatus.Online,
114
+ online: this.adapter.getSelfInfo(this.oneBot.uin, 'V11').status === onebot_1.OneBotStatus.Online,
71
115
  good: this.oneBot.status === onebot_1.OneBotStatus.Good
72
116
  },
73
117
  time: Math.floor(Date.now() / 1000),
@@ -77,14 +121,25 @@ class V11 extends service_1.Service {
77
121
  });
78
122
  }, this.config.heartbeat * 1000);
79
123
  }
124
+ this.adapter.on('message.receive', (uin, event) => {
125
+ const payload = this.adapter.formatEventPayload('V11', 'message', event);
126
+ this.dispatch(payload);
127
+ });
128
+ this.adapter.on('notice.receive', (uin, event) => {
129
+ const payload = this.adapter.formatEventPayload('V11', 'notice', event);
130
+ this.dispatch(payload);
131
+ });
132
+ this.adapter.on('request.receive', (uin, event) => {
133
+ const payload = this.adapter.formatEventPayload('V11', 'request', event);
134
+ this.dispatch(payload);
135
+ });
80
136
  }
81
137
  startHttp() {
82
138
  this.oneBot.app.router.all(new RegExp(`^${this.path}/(.*)$`), this._httpRequestHandler.bind(this));
83
139
  this.logger.mark(`开启http服务器成功,监听:http://127.0.0.1:${this.oneBot.app.config.port}${this.path}`);
84
140
  }
85
141
  startHttpReverse(config) {
86
- this.on('dispatch', (unserialized) => {
87
- const serialized = JSON.stringify(unserialized);
142
+ this.on("dispatch", (serialized) => {
88
143
  const options = {
89
144
  method: "POST",
90
145
  timeout: this.config.post_timeout * 1000,
@@ -97,30 +152,34 @@ class V11 extends service_1.Service {
97
152
  };
98
153
  if (this.config.secret) {
99
154
  //@ts-ignore
100
- options.headers["X-Signature"] = "sha1=" + crypto.createHmac("sha1", String(this.config.secret)).update(serialized).digest("hex");
155
+ options.headers["X-Signature"] =
156
+ "sha1=" + crypto_1.default.createHmac("sha1", String(this.config.secret)).update(serialized).digest("hex");
101
157
  }
102
158
  const protocol = config.url.startsWith("https") ? https_1.default : http_1.default;
103
159
  try {
104
- protocol.request(config.url, options, (res) => {
160
+ protocol
161
+ .request(config.url, options, (res) => {
105
162
  if (res.statusCode !== 200)
106
163
  return this.logger.warn(`POST(${config.url})上报事件收到非200响应:` + res.statusCode);
107
164
  let data = "";
108
165
  res.setEncoding("utf-8");
109
- res.on("data", (chunk) => data += chunk);
166
+ res.on("data", (chunk) => (data += chunk));
110
167
  res.on("end", () => {
111
168
  this.logger.debug(`收到HTTP响应 ${res.statusCode} :` + data);
112
169
  if (!data)
113
170
  return;
114
171
  try {
115
- this._quickOperate(unserialized, JSON.parse(data));
172
+ this._quickOperate(JSON.parse(serialized), JSON.parse(data));
116
173
  }
117
174
  catch (e) {
118
175
  this.logger.error(`快速操作遇到错误:` + e.message);
119
176
  }
120
177
  });
121
- }).on("error", (err) => {
178
+ })
179
+ .on("error", (err) => {
122
180
  this.logger.error(`POST(${config.url})上报事件失败:` + err.message);
123
- }).end(serialized, () => {
181
+ })
182
+ .end(serialized, () => {
124
183
  this.logger.debug(`POST(${config.url})上报事件成功: ` + serialized);
125
184
  });
126
185
  }
@@ -145,82 +204,49 @@ class V11 extends service_1.Service {
145
204
  });
146
205
  if (this.config.access_token) {
147
206
  const url = new url_1.URL(req.url, "http://127.0.0.1");
148
- const token = url.searchParams.get('access_token');
207
+ const token = url.searchParams.get("access_token");
149
208
  if (token)
150
209
  req.headers["authorization"] = `Bearer ${token}`;
151
- if (!req.headers["authorization"] || req.headers["authorization"] !== `Bearer ${this.config.access_token}`)
210
+ if (!req.headers["authorization"] ||
211
+ req.headers["authorization"] !== `Bearer ${this.config.access_token}`)
152
212
  return ws.close(1002, "wrong access token");
153
213
  }
154
214
  this._webSocketHandler(ws);
155
215
  });
156
- this.on('dispatch', (serialized) => {
157
- for (const ws of this.wss.clients) {
158
- ws.send(serialized, (err) => {
159
- if (err)
160
- this.logger.error(`正向WS(${ws.url})上报事件失败: ` + err.message);
161
- else
162
- this.logger.debug(`正向WS(${ws.url})上报事件成功: ` + serialized);
163
- });
164
- }
165
- });
166
216
  }
167
217
  startWsReverse(url) {
168
218
  this._createWsr(url);
169
- this.on('dispatch', (serialized) => {
170
- for (const ws of this.wsr) {
171
- ws.send(serialized, (err) => {
172
- if (err) {
173
- this.logger.error(`反向WS(${ws.url})上报事件失败: ` + err.message);
174
- }
175
- else
176
- this.logger.debug(`反向WS(${ws.url})上报事件成功: ` + serialized);
177
- });
178
- }
179
- });
180
219
  }
181
220
  async stop(force) {
182
- if (this.client.status === icqq_1.OnlineStatus.Online) {
183
- await this.client.terminate();
221
+ for (const ws of this.wss.clients) {
222
+ ws.close();
184
223
  }
185
- if (force) {
186
- (0, fs_1.rmSync)(this.client.dir, { force: true, recursive: true });
224
+ this.wss.close();
225
+ for (const ws of this.wsr) {
226
+ ws.close();
187
227
  }
188
228
  }
189
229
  format(_, data) {
190
230
  return data;
191
231
  }
192
- system_online(data) {
193
- this.logger.info("【好友列表】");
194
- this.client.fl.forEach(item => this.logger.info(`\t${item.nickname}(${item.user_id})`));
195
- this.logger.info("【群列表】");
196
- this.client.gl.forEach(item => this.logger.info(`\t${item.group_name}(${item.group_id})`));
197
- this.logger.info('');
198
- }
199
232
  async dispatch(data) {
200
233
  data.post_type = data.post_type || 'system';
201
234
  if (data.message && data.post_type === 'message') {
202
235
  if (this.config.post_message_format === 'array') {
203
- data.message = (0, icqq_cq_enable_1.toSegment)(data.message);
236
+ data.message = this.adapter.toSegment('V11', data.message);
204
237
  if (data.source) { // reply
205
238
  let msg0 = data.message[0];
206
- msg0.data['id'] = await this.getReplyMsgIdFromDB(data);
239
+ msg0.data["id"] = await this.getReplyMsgIdFromDB(data);
207
240
  }
208
241
  }
209
242
  else {
210
- if (data.source) {
211
- data.message.shift();
212
- // segment 更好用, cq 一般只用来显示,就不存储真实id了, 有需求的自己去改
213
- data.message = (0, icqq_cq_enable_1.toCqcode)(data).replace(/^(\[CQ:reply,id=)(.+?)\]/, `$1${data.source.seq}]`);
214
- }
215
- else {
216
- data.message = (0, icqq_cq_enable_1.toCqcode)(data);
217
- }
243
+ data.message = this.adapter.toCqcode("V11", data.message);
218
244
  }
219
245
  }
220
246
  if (data.message_id) {
221
247
  data.message_id = await this.addMsgToDB(data);
222
248
  }
223
- if (data.post_type == 'notice' && String(data.notice_type).endsWith('_recall')) {
249
+ if (data.post_type == "notice" && String(data.notice_type).endsWith("_recall")) {
224
250
  this.db.markMsgAsRecalled(data.base64_id);
225
251
  }
226
252
  if (data.font) {
@@ -233,39 +259,39 @@ class V11 extends service_1.Service {
233
259
  // if (!['user_id', 'group_id', 'discuss_id', 'member_id', 'channel_id', 'guild_id'].includes(key)) return value
234
260
  // return value + ''
235
261
  // })
236
- this.emit('dispatch', this._formatEvent(data));
262
+ this.emit("dispatch", this._formatEvent(data));
237
263
  }
238
264
  _formatEvent(data) {
239
265
  if (data.post_type === 'notice') {
240
266
  // console.log(JSON.stringify(data))
241
267
  const data1 = { ...data };
242
- if (data.notice_type === 'group') {
268
+ if (data.notice_type === "group") {
243
269
  delete data1.group;
244
270
  delete data1.member;
245
271
  switch (data.sub_type) {
246
272
  case 'decrease':
247
- data1.sub_type = data.operator_id === data.user_id ? 'leave' : data.user_id === this.client.uin ? 'kick_me' : 'kick';
273
+ data1.sub_type = data.operator_id === data.user_id ? 'leave' : data.user_id === this.oneBot.uin ? 'kick_me' : 'kick';
248
274
  data1.notice_type = `${data.notice_type}_${data.sub_type}`;
249
275
  break;
250
- case 'increase':
276
+ case "increase":
251
277
  data1.notice_type = `${data.notice_type}_${data.sub_type}`;
252
- data1.sub_type = 'approve'; // todo 尚未实现
278
+ data1.sub_type = "approve"; // todo 尚未实现
253
279
  data1.operator_id = data1.user_id; // todo 尚未实现
254
280
  break;
255
- case 'ban':
281
+ case "ban":
256
282
  data1.notice_type = `${data.notice_type}_${data.sub_type}`;
257
- data1.subtype = data.duration ? 'ban' : 'lift_ban';
283
+ data1.subtype = data.duration ? "ban" : "lift_ban";
258
284
  break;
259
- case 'recall':
285
+ case "recall":
260
286
  data1.notice_type = `${data.notice_type}_${data.sub_type}`;
261
287
  delete data1.sub_type;
262
288
  break;
263
- case 'admin':
289
+ case "admin":
264
290
  data1.notice_type = `${data.notice_type}_${data.sub_type}`;
265
- data1.sub_type = data.set ? 'set' : 'unset';
291
+ data1.sub_type = data.set ? "set" : "unset";
266
292
  break;
267
- case 'poke':
268
- data1.notice_type = 'notify';
293
+ case "poke":
294
+ data1.notice_type = "notify";
269
295
  data1.user_id = data.operator_id;
270
296
  break;
271
297
  default:
@@ -275,10 +301,10 @@ class V11 extends service_1.Service {
275
301
  else {
276
302
  delete data1.friend;
277
303
  switch (data.sub_type) {
278
- case 'increase':
304
+ case "increase":
279
305
  data1.notice_type = `friend_add`;
280
306
  break;
281
- case 'recall':
307
+ case "recall":
282
308
  data1.notice_type = `friend_recall`;
283
309
  break;
284
310
  default:
@@ -292,7 +318,8 @@ class V11 extends service_1.Service {
292
318
  }
293
319
  }
294
320
  async addMsgToDB(data) {
295
- if (!data.sender || !('user_id' in data.sender)) { // eg. notice
321
+ if (!data.sender || !("user_id" in data.sender)) {
322
+ // eg. notice
296
323
  return;
297
324
  }
298
325
  let msg = new db_entities_1.MsgEntry();
@@ -300,15 +327,15 @@ class V11 extends service_1.Service {
300
327
  msg.seq = data.seq;
301
328
  msg.user_id = data.sender.user_id;
302
329
  msg.nickname = data.sender.nickname;
303
- if (data.message_type === 'group') {
330
+ if (data.message_type === "group") {
304
331
  msg.group_id = data.group_id;
305
- msg.group_name = data["group_name"] || ''; // 可能不存在(gocq默认不发)
332
+ msg.group_name = data["group_name"] || ""; // 可能不存在(gocq默认不发)
306
333
  }
307
334
  else {
308
335
  msg.group_id = 0;
309
- msg.group_name = '';
336
+ msg.group_name = "";
310
337
  }
311
- msg.content = data.cqCode;
338
+ msg.content = data.cqCode || data.message;
312
339
  return await this.db.addOrUpdateMsg(msg);
313
340
  }
314
341
  /**
@@ -324,24 +351,26 @@ class V11 extends service_1.Service {
324
351
  msg.base64_id = base64_id;
325
352
  msg.seq = seq;
326
353
  msg.user_id = user_id;
327
- msg.nickname = '';
354
+ msg.nickname = "";
328
355
  msg.group_id = group_id;
329
- msg.group_name = '';
330
- msg.content = '';
356
+ msg.group_name = "";
357
+ msg.content = "";
331
358
  return await this.db.addOrUpdateMsg(msg);
332
359
  }
333
360
  async getReplyMsgIdFromDB(data) {
334
- let group_id = (data.message_type === 'group') ? data.group_id : 0;
361
+ let group_id = data.message_type === "group" ? data.group_id : 0;
335
362
  let msg = await this.db.getMsgByParams(data.source.user_id, group_id, data.source.seq);
336
363
  return msg ? msg.id : 0;
337
364
  }
338
365
  async _httpRequestHandler(ctx) {
339
- if (ctx.method === 'OPTIONS') {
340
- return ctx.writeHead(200, {
341
- 'Access-Control-Allow-Origin': '*',
342
- 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
343
- 'Access-Control-Allow-Headers': 'Content-Type, authorization'
344
- }).end();
366
+ if (ctx.method === "OPTIONS") {
367
+ return ctx
368
+ .writeHead(200, {
369
+ "Access-Control-Allow-Origin": "*",
370
+ "Access-Control-Allow-Methods": "POST, GET, OPTIONS",
371
+ "Access-Control-Allow-Headers": "Content-Type, authorization",
372
+ })
373
+ .end();
345
374
  }
346
375
  const url = new url_1.URL(ctx.url, `http://127.0.0.1`);
347
376
  if (this.config.access_token) {
@@ -360,7 +389,7 @@ class V11 extends service_1.Service {
360
389
  ctx.res.setHeader("Content-Type", "application/json; charset=utf-8");
361
390
  if (this.config.enable_cors)
362
391
  ctx.res.setHeader("Access-Control-Allow-Origin", "*");
363
- const action = url.pathname.replace(`${this.path}`, '').slice(1);
392
+ const action = url.pathname.replace(`${this.path}`, "").slice(1);
364
393
  if (ctx.method === "GET") {
365
394
  try {
366
395
  const ret = await this.apply({ action, params: ctx.query });
@@ -372,7 +401,10 @@ class V11 extends service_1.Service {
372
401
  }
373
402
  else if (ctx.method === "POST") {
374
403
  try {
375
- const params = { ...(ctx.request.query || {}), ...(ctx.request.body || {}) };
404
+ const params = {
405
+ ...(ctx.request.query || {}),
406
+ ...(ctx.request.body || {}),
407
+ };
376
408
  const ret = await this.apply({ action, params });
377
409
  ctx.res.writeHead(200).end(ret);
378
410
  }
@@ -403,7 +435,7 @@ class V11 extends service_1.Service {
403
435
  status: "async",
404
436
  data: null,
405
437
  error: null,
406
- echo: data.echo
438
+ echo: data.echo,
407
439
  });
408
440
  }
409
441
  else {
@@ -426,11 +458,12 @@ class V11 extends service_1.Service {
426
458
  status: "failed",
427
459
  data: null,
428
460
  error: {
429
- code, message
461
+ code,
462
+ message,
430
463
  },
431
464
  echo: data?.echo,
432
465
  msg: e.message,
433
- action: data.action
466
+ action: data.action,
434
467
  }));
435
468
  }
436
469
  });
@@ -480,20 +513,20 @@ class V11 extends service_1.Service {
480
513
  return;
481
514
  const action = event.message_type === "private" ? "sendPrivateMsg" : "sendGroupMsg";
482
515
  const id = event.message_type === "private" ? event.user_id : event.group_id;
483
- this.client[action](id, res.reply, res.auto_escape);
516
+ this.action[action].apply(this, [id, res.reply, res.auto_escape]);
484
517
  }
485
518
  if (event.message_type === "group") {
486
519
  if (res.delete)
487
- this.client.deleteMsg(event.message_id);
520
+ this.adapter.deleteMessage(this.oneBot.uin, 'V11', [event.message_id]);
488
521
  if (res.kick && !event.anonymous)
489
- this.client.setGroupKick(event.group_id, event.user_id, res.reject_add_request);
522
+ this.adapter.call(this.oneBot.uin, 'V11', 'setGroupKick', [event.group_id, event.user_id, res.reject_add_request]);
490
523
  if (res.ban)
491
- this.client.setGroupBan(event.group_id, event.user_id, res.ban_duration > 0 ? res.ban_duration : 1800);
524
+ this.adapter.call(this.oneBot.uin, 'V11', 'setGroupBan', [event.group_id, event.user_id, res.ban_duration > 0 ? res.ban_duration : 1800]);
492
525
  }
493
526
  }
494
527
  if (event.post_type === "request" && "approve" in res) {
495
528
  const action = event.request_type === "friend" ? "setFriendAddRequest" : "setGroupAddRequest";
496
- this.client[action](event.flag, res.approve, res.reason ? res.reason : "", !!res.block);
529
+ this.adapter.call(this.oneBot.uin, 'V11', action, [event.flag, res.approve, res.reason ? res.reason : "", !!res.block]);
497
530
  }
498
531
  }
499
532
  /**
@@ -501,9 +534,6 @@ class V11 extends service_1.Service {
501
534
  */
502
535
  async apply(req) {
503
536
  let { action, params, echo } = req;
504
- if (typeof params.message_id == 'number' || /^\d+$/.test(params.message_id)) {
505
- params.message_id = (await this.db.getMsgById(params.message_id)).id; // 调用api时把本地的数字id转为base64发给icqq
506
- }
507
537
  action = (0, utils_1.toLine)(action);
508
538
  let is_async = action.includes("_async");
509
539
  if (is_async)
@@ -511,10 +541,9 @@ class V11 extends service_1.Service {
511
541
  let is_queue = action.includes("_rate_limited");
512
542
  if (is_queue)
513
543
  action = action.replace("_rate_limited", "");
514
- if (action === 'send_msg') {
515
- if (["private", "group", "discuss"].includes(params.message_type)) {
544
+ if (action === "send_msg") {
545
+ if (sendMsgTypes.includes(params.message_type))
516
546
  action = "send_" + params.message_type + "_msg";
517
- }
518
547
  else if (params.user_id)
519
548
  action = "send_private_msg";
520
549
  else if (params.group_id)
@@ -522,10 +551,11 @@ class V11 extends service_1.Service {
522
551
  else if (params.discuss_id)
523
552
  action = "send_discuss_msg";
524
553
  else
525
- throw new Error('required message_type or input (user_id/group_id)');
554
+ throw new Error("required message_type or input (user_id/group_id)");
555
+ }
556
+ else if (action === "send_like") {
557
+ action = "send_user_like";
526
558
  }
527
- if (action === 'send_like')
528
- action = 'send_user_like';
529
559
  const method = (0, utils_1.toHump)(action);
530
560
  if (Reflect.has(this.action, method)) {
531
561
  const ARGS = String(Reflect.get(this.action, method)).match(/\(.*\)/)?.[0]
@@ -543,14 +573,14 @@ class V11 extends service_1.Service {
543
573
  if (/[CQ:music,type=.+,id=.+]/.test(params[k])) {
544
574
  params[k] = params[k].replace(',type=', ',platform=');
545
575
  }
546
- params[k] = (0, icqq_cq_enable_1.fromCqcode)(params[k]);
576
+ params[k] = this.adapter.fromCqcode('V11', params[k]);
547
577
  }
548
578
  else {
549
579
  if (params[k][0].type == 'music' && params[k][0]?.data?.type) {
550
580
  params[k][0].data.platform = params[k][0].data.type;
551
581
  delete params[k][0].data.type;
552
582
  }
553
- params[k] = (0, icqq_cq_enable_1.fromSegment)(params[k]);
583
+ params[k] = this.adapter.fromSegment('V11', params[k]);
554
584
  }
555
585
  params['message_id'] = params[k].find(e => e.type === 'reply')?.message_id;
556
586
  }
@@ -585,18 +615,17 @@ class V11 extends service_1.Service {
585
615
  if (result.data instanceof Map)
586
616
  result.data = [...result.data.values()];
587
617
  if (result.data?.message)
588
- result.data.message = (0, icqq_cq_enable_1.toSegment)(result.data.message);
618
+ result.data.message = this.adapter.toSegment('V11', result.data.message);
589
619
  // send_msg_xxx 时提前把数据写入数据库(也有可能来的比message慢,后来的话会被数据库忽略)
590
- if (result.status === 'ok' && params.user_id && result.data?.message_id && result.data?.seq) {
591
- result.data.message_id = await this.addMsgToDBFromSendMsgResult(params.user_id, params.group_id || 0, result.data.seq, result.data.message_id);
620
+ if (result.status === "ok" && action.match(sendMsgMethodRegex) && result.data?.message_id && result.data?.seq) {
621
+ result.data.message_id = await this.addMsgToDBFromSendMsgResult(this.oneBot.uin, // msg send resp uin is always bot uin
622
+ params.group_id || 0, result.data.seq, result.data.message_id);
592
623
  }
593
624
  if (echo) {
594
625
  result.echo = echo;
595
626
  }
596
627
  return JSON.stringify(result);
597
628
  }
598
- else
599
- throw new onebot_2.NotFoundError();
600
629
  }
601
630
  /**
602
631
  * 限速队列调用
@@ -621,39 +650,39 @@ exports.V11 = V11;
621
650
  function ok(data, retcode = 0, pending) {
622
651
  return {
623
652
  retcode,
624
- status: pending ? 'async' : 'ok',
653
+ status: pending ? "async" : "ok",
625
654
  data,
626
- error: null
655
+ error: null,
627
656
  };
628
657
  }
629
658
  V11.ok = ok;
630
659
  function error(error, retcode = 1) {
631
660
  return {
632
661
  retcode,
633
- status: 'error',
662
+ status: "error",
634
663
  data: null,
635
- error
664
+ error,
636
665
  };
637
666
  }
638
667
  V11.error = error;
639
668
  V11.defaultConfig = {
640
669
  heartbeat: 3,
641
- access_token: '',
670
+ access_token: "",
642
671
  post_timeout: 15,
643
- secret: '',
672
+ secret: "",
644
673
  rate_limit_interval: 4,
645
- post_message_format: 'string',
674
+ post_message_format: "string",
646
675
  reconnect_interval: 3,
647
676
  use_http: true,
648
677
  enable_cors: true,
649
678
  enable_reissue: false,
650
679
  use_ws: true,
651
680
  http_reverse: [],
652
- ws_reverse: []
681
+ ws_reverse: [],
653
682
  };
654
683
  function genMetaEvent(uin, type) {
655
684
  return {
656
- self_id: uin,
685
+ self_id: Number.isNaN(parseInt(uin)) ? uin : parseInt(uin),
657
686
  time: Math.floor(Date.now() / 1000),
658
687
  post_type: "meta_event",
659
688
  meta_event_type: "lifecycle",