mirai-js 2.0.1 → 2.2.0

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/dist/node/Bot.js CHANGED
@@ -97,8 +97,7 @@ const {
97
97
 
98
98
  class Bot extends BotConfigGetable {
99
99
  constructor() {
100
- super(); // 实例化一个内部类 Waiter
101
-
100
+ super();
102
101
  this.waiter = new Waiter(this);
103
102
  this.config = undefined;
104
103
  this.eventProcessorMap = {};
@@ -434,14 +433,14 @@ class Bot extends BotConfigGetable {
434
433
  });
435
434
  } // 发给好友
436
435
  else if (friend) {
437
- await _sendNudge({
438
- baseUrl,
439
- sessionKey,
440
- target,
441
- subject: friend,
442
- kind: 'Friend'
443
- });
444
- }
436
+ await _sendNudge({
437
+ baseUrl,
438
+ sessionKey,
439
+ target,
440
+ subject: friend,
441
+ kind: 'Friend'
442
+ });
443
+ }
445
444
  }
446
445
  /**
447
446
  * @description 添加一个事件处理器
@@ -1325,6 +1324,48 @@ class Bot extends BotConfigGetable {
1325
1324
  target: messageId
1326
1325
  });
1327
1326
  }
1327
+ /**
1328
+ * @description 向 mirai-console 发送指令
1329
+ * @param {string[]} command 必选,指令和参数
1330
+ * @returns {Object} 结构 { message },注意查看 message 的内容,已知的问题:
1331
+ * 'Login failed: Mirai 无法完成滑块验证. 使用协议 ANDROID_PHONE 强制要求滑块验证,
1332
+ * 请更换协议后重试. 另请参阅: https://github.com/project-mirai/mirai-login-solver-selenium'
1333
+ */
1334
+
1335
+
1336
+ async sendCommand({
1337
+ command
1338
+ }) {
1339
+ // 检查对象状态
1340
+ if (!this.config) {
1341
+ throw new Error('setEssence 请先调用 open,建立一个会话');
1342
+ } // 检查参数
1343
+
1344
+
1345
+ if (!command) {
1346
+ throw new Error(`sendCommand 缺少必要的 ${getInvalidParamsString({
1347
+ command
1348
+ })} 参数`);
1349
+ }
1350
+
1351
+ const {
1352
+ Message
1353
+ } = require('./Message');
1354
+
1355
+ const {
1356
+ baseUrl,
1357
+ sessionKey
1358
+ } = this.config;
1359
+ return await _sendCommand({
1360
+ baseUrl,
1361
+ sessionKey,
1362
+ command: command.map(v => {
1363
+ var _v$toString;
1364
+
1365
+ return (_v$toString = v === null || v === void 0 ? void 0 : v.toString()) !== null && _v$toString !== void 0 ? _v$toString : '';
1366
+ }).reduce((acc, cur) => acc.addText(cur), new Message()).messageChain
1367
+ });
1368
+ }
1328
1369
  /**
1329
1370
  * @description 检测该账号是否已经在 mirai-console 登录
1330
1371
  * @param {string} baseUrl 必选,mirai-api-http server 的地址
@@ -1373,40 +1414,6 @@ class Bot extends BotConfigGetable {
1373
1414
  return true;
1374
1415
  }
1375
1416
  }
1376
- /**
1377
- * @description 向 mirai-console 发送指令
1378
- * @param {string} baseUrl 必选,mirai-api-http server 的地址
1379
- * @param {string} verifyKey 必选,mirai-api-http server 设置的 verifyKey
1380
- * @param {string} command 必选,指令名
1381
- * @param {string[]} args 可选,指令的参数
1382
- * @returns {Object} 结构 { message },注意查看 message 的内容,已知的问题:
1383
- * 'Login failed: Mirai 无法完成滑块验证. 使用协议 ANDROID_PHONE 强制要求滑块验证,
1384
- * 请更换协议后重试. 另请参阅: https://github.com/project-mirai/mirai-login-solver-selenium'
1385
- */
1386
-
1387
-
1388
- static async sendCommand({
1389
- baseUrl,
1390
- verifyKey,
1391
- command,
1392
- args
1393
- }) {
1394
- // 检查参数
1395
- if (!baseUrl || !verifyKey || !command) {
1396
- throw new Error(`sendCommand 缺少必要的 ${getInvalidParamsString({
1397
- baseUrl,
1398
- verifyKey,
1399
- command
1400
- })} 参数`);
1401
- }
1402
-
1403
- return await _sendCommand({
1404
- baseUrl,
1405
- verifyKey,
1406
- command,
1407
- args
1408
- });
1409
- }
1410
1417
 
1411
1418
  } // 静态属性: 群成员的权限
1412
1419
 
@@ -26,21 +26,12 @@ class Middleware {
26
26
 
27
27
 
28
28
  autoReLogin({
29
- baseUrl,
30
- verifyKey,
31
29
  password
32
30
  }) {
33
- const {
34
- Bot
35
- } = require('./index.js');
36
-
37
31
  this.middleware.push(async (data, next) => {
38
32
  try {
39
- await Bot.sendCommand({
40
- baseUrl,
41
- verifyKey,
42
- command: '/login',
43
- args: [data.qq, password]
33
+ await data.bot.sendCommand({
34
+ command: ['/login', data.qq, password]
44
35
  });
45
36
  await data.bot.open();
46
37
  await next();
@@ -649,26 +640,100 @@ class Middleware {
649
640
  throw new Error('Middleware.syncWrapper 消息格式出错');
650
641
  }
651
642
 
652
- data.waitFor = {
653
- messageChain: () => {
654
- var _data$bot, _data$bot$waiter;
643
+ const watiForMessageChain = async qq => {
644
+ var _qq, _data$sender17;
655
645
 
656
- return (_data$bot = data.bot) === null || _data$bot === void 0 ? void 0 : (_data$bot$waiter = _data$bot.waiter) === null || _data$bot$waiter === void 0 ? void 0 : _data$bot$waiter.wait(data.type, ({
657
- messageChain
658
- }) => messageChain);
659
- },
660
- text: () => {
661
- var _data$bot2, _data$bot2$waiter;
646
+ qq = (_qq = qq) !== null && _qq !== void 0 ? _qq : data === null || data === void 0 ? void 0 : (_data$sender17 = data.sender) === null || _data$sender17 === void 0 ? void 0 : _data$sender17.id;
662
647
 
663
- return (_data$bot2 = data.bot) === null || _data$bot2 === void 0 ? void 0 : (_data$bot2$waiter = _data$bot2.waiter) === null || _data$bot2$waiter === void 0 ? void 0 : _data$bot2$waiter.wait(data.type, new Middleware().textProcessor().done(({
664
- text
665
- }) => text));
666
- },
667
- custom: processor => {
668
- var _data$bot3, _data$bot3$waiter;
648
+ if (qq == undefined) {
649
+ throw new Error('Middleware.syncWrapper 消息格式出错');
650
+ }
651
+
652
+ do {
653
+ var _await$data$bot$waite, _data$bot, _data$bot$waiter;
654
+
655
+ var {
656
+ messageChain,
657
+ id
658
+ } = (_await$data$bot$waite = await ((_data$bot = data.bot) === null || _data$bot === void 0 ? void 0 : (_data$bot$waiter = _data$bot.waiter) === null || _data$bot$waiter === void 0 ? void 0 : _data$bot$waiter.wait(data.type, ({
659
+ messageChain,
660
+ sender: {
661
+ id
662
+ }
663
+ }) => ({
664
+ messageChain,
665
+ id
666
+ })))) !== null && _await$data$bot$waite !== void 0 ? _await$data$bot$waite : {};
667
+ } while (qq != id);
668
+
669
+ return messageChain;
670
+ };
671
+
672
+ const waitForText = async qq => {
673
+ var _qq2, _data$sender18;
674
+
675
+ qq = (_qq2 = qq) !== null && _qq2 !== void 0 ? _qq2 : data === null || data === void 0 ? void 0 : (_data$sender18 = data.sender) === null || _data$sender18 === void 0 ? void 0 : _data$sender18.id;
669
676
 
670
- return (_data$bot3 = data.bot) === null || _data$bot3 === void 0 ? void 0 : (_data$bot3$waiter = _data$bot3.waiter) === null || _data$bot3$waiter === void 0 ? void 0 : _data$bot3$waiter.wait(data.type, processor);
677
+ if (qq == undefined) {
678
+ throw new Error('Middleware.syncWrapper 消息格式出错');
671
679
  }
680
+
681
+ do {
682
+ var _await$data$bot$waite2, _data$bot2, _data$bot2$waiter;
683
+
684
+ var {
685
+ text,
686
+ id
687
+ } = (_await$data$bot$waite2 = await ((_data$bot2 = data.bot) === null || _data$bot2 === void 0 ? void 0 : (_data$bot2$waiter = _data$bot2.waiter) === null || _data$bot2$waiter === void 0 ? void 0 : _data$bot2$waiter.wait(data.type, new Middleware().textProcessor().done(({
688
+ text,
689
+ sender: {
690
+ id
691
+ }
692
+ }) => ({
693
+ text,
694
+ id
695
+ }))))) !== null && _await$data$bot$waite2 !== void 0 ? _await$data$bot$waite2 : {};
696
+ } while (qq != id);
697
+
698
+ return text;
699
+ };
700
+
701
+ const waitForCustom = async (qq, processor) => {
702
+ var _qq3, _data$sender19;
703
+
704
+ qq = (_qq3 = qq) !== null && _qq3 !== void 0 ? _qq3 : data === null || data === void 0 ? void 0 : (_data$sender19 = data.sender) === null || _data$sender19 === void 0 ? void 0 : _data$sender19.id;
705
+
706
+ if (qq == undefined) {
707
+ throw new Error('Middleware.syncWrapper 消息格式出错');
708
+ }
709
+
710
+ do {
711
+ var _data$sender20, _data$bot3, _data$bot3$waiter;
712
+
713
+ var data = await ((_data$bot3 = data.bot) === null || _data$bot3 === void 0 ? void 0 : (_data$bot3$waiter = _data$bot3.waiter) === null || _data$bot3$waiter === void 0 ? void 0 : _data$bot3$waiter.wait(data.type, new Middleware().textProcessor().done(data => data)));
714
+ } while (qq != (data === null || data === void 0 ? void 0 : (_data$sender20 = data.sender) === null || _data$sender20 === void 0 ? void 0 : _data$sender20.id));
715
+
716
+ return await processor(data);
717
+ };
718
+
719
+ data.waitFor = {
720
+ groupMember: (qq = undefined) => {
721
+ return {
722
+ messageChain: () => watiForMessageChain(qq),
723
+ text: () => waitForText(qq),
724
+ custom: processor => waitForCustom(qq, processor)
725
+ };
726
+ },
727
+ friend: qq => {
728
+ return {
729
+ messageChain: () => watiForMessageChain(qq),
730
+ text: () => waitForText(qq),
731
+ custom: processor => waitForCustom(qq, processor)
732
+ };
733
+ },
734
+ messageChain: () => watiForMessageChain(data.sender.id),
735
+ text: () => waitForText(data.sender.id),
736
+ custom: processor => waitForCustom(data.sender.id, processor)
672
737
  };
673
738
  await next();
674
739
  } catch (error) {
@@ -41,10 +41,10 @@ module.exports = async ({
41
41
 
42
42
  try {
43
43
  var {
44
- data,
45
44
  data: {
46
45
  msg: message,
47
- code
46
+ code,
47
+ data
48
48
  }
49
49
  } = responseData;
50
50
  } catch (error) {
@@ -2,6 +2,10 @@
2
2
 
3
3
  const axios = require('axios').default;
4
4
 
5
+ const {
6
+ errCodeMap
7
+ } = require('../util/errCode');
8
+
5
9
  let URL;
6
10
 
7
11
  if (!process.browser) {
@@ -16,39 +20,46 @@ const errorHandler = require('../util/errorHandler');
16
20
  /**
17
21
  * @description 向 mirai-console 发送指令
18
22
  * @param {string} baseUrl mirai-api-http server 的地址
19
- * @param {string} verifyKey mirai-api-http server 设置的 verifyKey
23
+ * @param {string} sessionKey 会话标识
20
24
  * @param {string} command 指令名
21
- * @param {string[]} args 指令的参数
22
- * @returns {Object} 结构 { message }
25
+ * @param {MessageChain[]} args 指令的参数
26
+ * @returns {Object} 结构 { message, code }
23
27
  */
24
28
 
25
29
 
26
30
  module.exports = async ({
27
31
  baseUrl,
28
- verifyKey,
29
- command: name,
30
- args
32
+ sessionKey,
33
+ command
31
34
  }) => {
32
35
  try {
33
36
  // 拼接 url
34
- const url = new URL('/command/send', baseUrl).toString(); // 请求
37
+ const url = new URL('/cmd/execute', baseUrl).toString(); // 请求
35
38
 
36
39
  const responseData = await axios.post(url, {
37
- verifyKey,
38
- name,
39
- args
40
+ sessionKey,
41
+ command
40
42
  });
41
43
 
42
44
  try {
43
45
  var {
44
- data: message
46
+ data: {
47
+ msg: message,
48
+ code
49
+ }
45
50
  } = responseData;
46
51
  } catch (error) {
47
52
  throw new Error('core.sendCommand 请求返回格式出错,请检查 mirai-console');
53
+ } // 抛出 mirai 的异常,到 catch 中处理后再抛出
54
+
55
+
56
+ if (code in errCodeMap) {
57
+ throw new Error(message);
48
58
  }
49
59
 
50
60
  return {
51
- message
61
+ message,
62
+ code
52
63
  };
53
64
  } catch (error) {
54
65
  errorHandler(error);
@@ -39,28 +39,20 @@ module.exports = async ({
39
39
  }) => {
40
40
  try {
41
41
  // 拼接 url
42
- const url = new URL('/sendTempMessage', baseUrl).toString(); // 请求
42
+ const url = new URL('/sendTempMessage', baseUrl).toString();
43
43
 
44
- var responseData;
44
+ if (!qq || !group) {
45
+ throw new Error('sendTempMessage 缺少必要的 qq 和 group 参数');
46
+ } // 请求
45
47
 
46
- if (qq) {
47
- responseData = await axios.post(url, {
48
- sessionKey,
49
- qq,
50
- quote,
51
- messageChain
52
- });
53
- } else if (group) {
54
- responseData = await axios.post(url, {
55
- sessionKey,
56
- qq,
57
- quote,
58
- messageChain
59
- });
60
- } else {
61
- // 上层已经做了参数检查,这里有些多余
62
- throw new Error('sendTempMessage 缺少必要的 qq 或 group 参数');
63
- }
48
+
49
+ const responseData = await axios.post(url, {
50
+ sessionKey,
51
+ qq,
52
+ group,
53
+ quote,
54
+ messageChain
55
+ });
64
56
 
65
57
  try {
66
58
  var {
@@ -49,7 +49,9 @@ module.exports = async ({
49
49
  data
50
50
  }) => {
51
51
  try {
52
- message(JSON.parse(data));
52
+ var _JSON$parse;
53
+
54
+ message((_JSON$parse = JSON.parse(data)) === null || _JSON$parse === void 0 ? void 0 : _JSON$parse.data);
53
55
  } catch (error) {} // eslint-disable-line no-empty
54
56
 
55
57
  };
@@ -0,0 +1,4 @@
1
+ #!/bin/bash
2
+
3
+ echo "🛑 - Build cancelled"
4
+ exit 0;
package/package.json CHANGED
@@ -1,52 +1,52 @@
1
- {
2
- "name": "mirai-js",
3
- "version": "2.0.1",
4
- "description": "QQ robot development framework based on Mirai-api-http.",
5
- "main": "dist/node/index.js",
6
- "scripts": {
7
- "lint": "eslint --fix .",
8
- "build": "webpack && babel src --out-dir dist/node --copy-files",
9
- "prepare": "husky install"
10
- },
11
- "lint-staged": {
12
- "*.js": "eslint --fix",
13
- "*.ts": "eslint --fix"
14
- },
15
- "keywords": [
16
- "mirai",
17
- "api",
18
- "http",
19
- "robot"
20
- ],
21
- "author": {
22
- "email": "devgaolihai@foxmail.com",
23
- "name": "gaolihai",
24
- "url": "https://gaolihai.cool/note/"
25
- },
26
- "repository": {
27
- "type": "git",
28
- "url": "https://github.com/Drincann/Mirai-js"
29
- },
30
- "homepage": "https://drincann.github.io/Mirai-js/",
31
- "license": "AGPL-3.0",
32
- "dependencies": {
33
- "axios": "^0.21.1",
34
- "form-data": "^3.0.0",
35
- "ws": "^7.4.2"
36
- },
37
- "devDependencies": {
38
- "@babel/cli": "^7.13.14",
39
- "@babel/core": "^7.13.14",
40
- "@babel/preset-env": "^7.13.12",
41
- "@typescript-eslint/eslint-plugin": "^4.15.2",
42
- "@typescript-eslint/parser": "^4.15.2",
43
- "eslint": "^7.19.0",
44
- "husky": "^6.0.0",
45
- "lint-staged": "^10.5.4",
46
- "path-browserify": "^1.0.1",
47
- "process": "^0.11.10",
48
- "typescript": "^4.2.2",
49
- "webpack": "^5.30.0",
50
- "webpack-cli": "^4.6.0"
51
- }
1
+ {
2
+ "name": "mirai-js",
3
+ "version": "2.2.0",
4
+ "description": "QQ robot development framework based on Mirai-api-http.",
5
+ "main": "dist/node/index.js",
6
+ "scripts": {
7
+ "lint": "eslint --fix .",
8
+ "build": "webpack && babel src --out-dir dist/node --copy-files",
9
+ "prepare": "husky install"
10
+ },
11
+ "lint-staged": {
12
+ "*.js": "eslint --fix",
13
+ "*.ts": "eslint --fix"
14
+ },
15
+ "keywords": [
16
+ "mirai",
17
+ "api",
18
+ "http",
19
+ "robot"
20
+ ],
21
+ "author": {
22
+ "email": "devgaolihai@foxmail.com",
23
+ "name": "gaolihai",
24
+ "url": "https://gaolihai.cool/note/"
25
+ },
26
+ "repository": {
27
+ "type": "git",
28
+ "url": "https://github.com/Drincann/Mirai-js"
29
+ },
30
+ "homepage": "https://drincann.github.io/Mirai-js/",
31
+ "license": "AGPL-3.0",
32
+ "dependencies": {
33
+ "axios": "^0.24.0",
34
+ "form-data": "^3.0.0",
35
+ "ws": "^7.4.2"
36
+ },
37
+ "devDependencies": {
38
+ "@babel/cli": "^7.13.14",
39
+ "@babel/core": "^7.13.14",
40
+ "@babel/preset-env": "^7.13.12",
41
+ "@typescript-eslint/eslint-plugin": "^5.4.0",
42
+ "@typescript-eslint/parser": "^5.5.0",
43
+ "eslint": "^8.2.0",
44
+ "husky": "^6.0.0",
45
+ "lint-staged": "^12.0.2",
46
+ "path-browserify": "^1.0.1",
47
+ "process": "^0.11.10",
48
+ "typescript": "^4.2.2",
49
+ "webpack": "^5.30.0",
50
+ "webpack-cli": "^4.6.0"
51
+ }
52
52
  }
package/src/Bot.d.ts CHANGED
@@ -253,6 +253,11 @@ export class Bot implements BotConfigGetable {
253
253
  */
254
254
  setEssence({ messageId }: Bot.SetEssenceOptions): Promise<void>;
255
255
 
256
+ /**
257
+ * @description 向 mirai-console 发送指令
258
+ * @param command 必选,指令名
259
+ */
260
+ sendCommand({ command }: Bot.SendCommandOptions): Promise<Bot.MiraiConsoleMessage>;
256
261
 
257
262
  // 类方法
258
263
  /**
@@ -263,17 +268,6 @@ export class Bot implements BotConfigGetable {
263
268
  */
264
269
  static isBotLoggedIn({ baseUrl, verifyKey, qq }: Bot.IsBotLoggedInOptions): Promise<boolean>;
265
270
 
266
- /**
267
- * @description 向 mirai-console 发送指令
268
- * @param baseUrl 必选,mirai-api-http server 的地址
269
- * @param verifyKey 必选,mirai-api-http server 设置的 verifyKey
270
- * @param command 必选,指令名
271
- * @param args 可选,指令的参数
272
- */
273
- static sendCommand({
274
- baseUrl, verifyKey,
275
- command, args,
276
- }: Bot.SendCommandOptions): Promise<Bot.MiraiConsoleMessage>;
277
271
  }
278
272
 
279
273
  // 类型
@@ -442,10 +436,7 @@ declare namespace Bot {
442
436
  }
443
437
 
444
438
  interface SendCommandOptions {
445
- baseUrl: string;
446
- verifyKey: string;
447
- command: string;
448
- args: string[];
439
+ command: string[];
449
440
  }
450
441
 
451
442
  interface IsBotLoggedInOptions {
package/src/Bot.js CHANGED
@@ -51,7 +51,6 @@ const { MessageChainGetable, BotConfigGetable } = require('./interface');
51
51
  class Bot extends BotConfigGetable {
52
52
  constructor() {
53
53
  super();
54
- // 实例化一个内部类 Waiter
55
54
  this.waiter = new Waiter(this);
56
55
  this.config = undefined;
57
56
  this.eventProcessorMap = {};
@@ -966,6 +965,35 @@ class Bot extends BotConfigGetable {
966
965
  await _setEssence({ baseUrl, sessionKey, target: messageId });
967
966
  }
968
967
 
968
+ /**
969
+ * @description 向 mirai-console 发送指令
970
+ * @param {string[]} command 必选,指令和参数
971
+ * @returns {Object} 结构 { message },注意查看 message 的内容,已知的问题:
972
+ * 'Login failed: Mirai 无法完成滑块验证. 使用协议 ANDROID_PHONE 强制要求滑块验证,
973
+ * 请更换协议后重试. 另请参阅: https://github.com/project-mirai/mirai-login-solver-selenium'
974
+ */
975
+ async sendCommand({ command }) {
976
+ // 检查对象状态
977
+ if (!this.config) {
978
+ throw new Error('setEssence 请先调用 open,建立一个会话');
979
+ }
980
+
981
+ // 检查参数
982
+ if (!command) {
983
+ throw new Error(`sendCommand 缺少必要的 ${getInvalidParamsString({ command })} 参数`);
984
+ }
985
+
986
+ const { Message } = require('./Message');
987
+ const { baseUrl, sessionKey } = this.config;
988
+ return await _sendCommand({
989
+ baseUrl, sessionKey,
990
+ command: command
991
+ .map((v) => v?.toString() ?? '')
992
+ .reduce((acc, cur) => acc.addText(cur), new Message)
993
+ .messageChain
994
+ });
995
+ }
996
+
969
997
  /**
970
998
  * @description 检测该账号是否已经在 mirai-console 登录
971
999
  * @param {string} baseUrl 必选,mirai-api-http server 的地址
@@ -990,24 +1018,6 @@ class Bot extends BotConfigGetable {
990
1018
  }
991
1019
  }
992
1020
 
993
- /**
994
- * @description 向 mirai-console 发送指令
995
- * @param {string} baseUrl 必选,mirai-api-http server 的地址
996
- * @param {string} verifyKey 必选,mirai-api-http server 设置的 verifyKey
997
- * @param {string} command 必选,指令名
998
- * @param {string[]} args 可选,指令的参数
999
- * @returns {Object} 结构 { message },注意查看 message 的内容,已知的问题:
1000
- * 'Login failed: Mirai 无法完成滑块验证. 使用协议 ANDROID_PHONE 强制要求滑块验证,
1001
- * 请更换协议后重试. 另请参阅: https://github.com/project-mirai/mirai-login-solver-selenium'
1002
- */
1003
- static async sendCommand({ baseUrl, verifyKey, command, args }) {
1004
- // 检查参数
1005
- if (!baseUrl || !verifyKey || !command) {
1006
- throw new Error(`sendCommand 缺少必要的 ${getInvalidParamsString({ baseUrl, verifyKey, command })} 参数`);
1007
- }
1008
-
1009
- return await _sendCommand({ baseUrl, verifyKey, command, args });
1010
- }
1011
1021
  }
1012
1022
 
1013
1023