mirai-js 2.0.2 → 2.3.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.
Files changed (113) hide show
  1. package/.github/FUNDING.yml +1 -1
  2. package/.github/dependabot.yml +1 -1
  3. package/.github/workflows/codeql-analysis.yml +70 -0
  4. package/.husky/pre-commit +0 -0
  5. package/demo.html +2 -2
  6. package/demo.js +6 -90
  7. package/dist/browser/mirai-js.js +1 -1
  8. package/dist/node/Bot.d.ts +22 -19
  9. package/dist/node/Bot.js +110 -47
  10. package/dist/node/FileManager.js +15 -6
  11. package/dist/node/Middleware.js +92 -27
  12. package/dist/node/core/auth.js +6 -2
  13. package/dist/node/core/fs/deleteGroupFile.js +77 -0
  14. package/dist/node/core/fs/getGroupFileInfo.js +83 -0
  15. package/dist/node/core/fs/getGroupFileList.js +88 -0
  16. package/dist/node/core/fs/makeGroupDir.js +79 -0
  17. package/dist/node/core/fs/moveGroupFile.js +83 -0
  18. package/dist/node/core/fs/renameGroupFile.js +79 -0
  19. package/dist/node/core/fs/uploadGroupFIle.js +87 -0
  20. package/dist/node/core/getFriendList.js +6 -2
  21. package/dist/node/core/getGroupConfig.js +6 -2
  22. package/dist/node/core/getGroupFileInfo.js +6 -2
  23. package/dist/node/core/getGroupFileList.js +6 -2
  24. package/dist/node/core/getGroupList.js +6 -2
  25. package/dist/node/core/getMemberInfo.js +6 -2
  26. package/dist/node/core/getMemberList.js +6 -2
  27. package/dist/node/core/getSessionConfig.js +6 -2
  28. package/dist/node/core/groupFileDelete.js +6 -2
  29. package/dist/node/core/groupFileMove.js +6 -2
  30. package/dist/node/core/groupFileRename.js +6 -2
  31. package/dist/node/core/mute.js +6 -2
  32. package/dist/node/core/muteAll.js +6 -2
  33. package/dist/node/core/quitGroup.js +6 -2
  34. package/dist/node/core/recall.js +6 -2
  35. package/dist/node/core/releaseSession.js +7 -2
  36. package/dist/node/core/removeFriend.js +70 -0
  37. package/dist/node/core/removeMember.js +6 -2
  38. package/dist/node/core/responseBotInvitedJoinGroupRequest.js +6 -2
  39. package/dist/node/core/responseFirendRequest.js +6 -2
  40. package/dist/node/core/responseMemberJoinRequest.js +6 -2
  41. package/dist/node/core/sendCommand.js +29 -14
  42. package/dist/node/core/sendFirendMessage.js +6 -2
  43. package/dist/node/core/sendGroupMessage.js +6 -2
  44. package/dist/node/core/sendNudge.js +6 -2
  45. package/dist/node/core/sendTempMessage.js +18 -22
  46. package/dist/node/core/setEssence.js +6 -2
  47. package/dist/node/core/setGroupConfig.js +6 -2
  48. package/dist/node/core/setMemberAdmin.js +76 -0
  49. package/dist/node/core/setMemberInfo.js +6 -2
  50. package/dist/node/core/setSessionConfig.js +6 -2
  51. package/dist/node/core/startListeningBrowser.js +8 -2
  52. package/dist/node/core/startListeningNode.js +5 -1
  53. package/dist/node/core/stopListeningBrowser.js +5 -1
  54. package/dist/node/core/stopListeningNode.js +5 -1
  55. package/dist/node/core/unmute.js +6 -2
  56. package/dist/node/core/unmuteAll.js +6 -2
  57. package/dist/node/core/uploadFileAndSend.js +6 -1
  58. package/dist/node/core/uploadImage.js +6 -1
  59. package/dist/node/core/uploadVoice.js +6 -1
  60. package/dist/node/core/verify.js +6 -2
  61. package/docs/checkDocShouldBuild.sh +4 -0
  62. package/package.json +8 -8
  63. package/src/Bot.d.ts +22 -19
  64. package/src/Bot.js +77 -26
  65. package/src/FileManager.js +10 -6
  66. package/src/Middleware.js +54 -10
  67. package/src/core/auth.js +4 -1
  68. package/src/core/fs/deleteGroupFile.js +49 -0
  69. package/src/core/fs/getGroupFileInfo.js +50 -0
  70. package/src/core/fs/getGroupFileList.js +51 -0
  71. package/src/core/fs/makeGroupDir.js +49 -0
  72. package/src/core/fs/moveGroupFile.js +51 -0
  73. package/src/core/{groupFileRename.js → fs/renameGroupFile.js} +10 -7
  74. package/src/core/{uploadFileAndSend.js → fs/uploadGroupFIle.js} +8 -5
  75. package/src/core/getFriendList.js +4 -1
  76. package/src/core/getGroupConfig.js +4 -1
  77. package/src/core/getGroupList.js +4 -1
  78. package/src/core/getMemberInfo.js +4 -1
  79. package/src/core/getMemberList.js +4 -1
  80. package/src/core/getSessionConfig.js +4 -1
  81. package/src/core/mute.js +4 -1
  82. package/src/core/muteAll.js +4 -1
  83. package/src/core/quitGroup.js +4 -1
  84. package/src/core/recall.js +4 -1
  85. package/src/core/releaseSession.js +5 -1
  86. package/src/core/{groupFileDelete.js → removeFriend.js} +10 -11
  87. package/src/core/removeMember.js +4 -1
  88. package/src/core/responseBotInvitedJoinGroupRequest.js +4 -1
  89. package/src/core/responseFirendRequest.js +4 -1
  90. package/src/core/responseMemberJoinRequest.js +4 -1
  91. package/src/core/sendCommand.js +17 -9
  92. package/src/core/sendFirendMessage.js +4 -1
  93. package/src/core/sendGroupMessage.js +4 -1
  94. package/src/core/sendNudge.js +4 -1
  95. package/src/core/sendTempMessage.js +16 -13
  96. package/src/core/setEssence.js +4 -1
  97. package/src/core/setGroupConfig.js +4 -1
  98. package/src/core/setMemberAdmin.js +48 -0
  99. package/src/core/setMemberInfo.js +4 -1
  100. package/src/core/setSessionConfig.js +4 -1
  101. package/src/core/startListeningBrowser.js +4 -1
  102. package/src/core/startListeningNode.js +3 -0
  103. package/src/core/stopListeningBrowser.js +3 -0
  104. package/src/core/stopListeningNode.js +3 -0
  105. package/src/core/unmute.js +4 -1
  106. package/src/core/unmuteAll.js +4 -1
  107. package/src/core/uploadImage.js +4 -1
  108. package/src/core/uploadVoice.js +4 -1
  109. package/src/core/verify.js +4 -1
  110. package/webpack.config.js +4 -0
  111. package/src/core/getGroupFileInfo.js +0 -44
  112. package/src/core/getGroupFileList.js +0 -40
  113. package/src/core/groupFileMove.js +0 -46
@@ -1,4 +1,5 @@
1
1
  const axios = require('axios').default;
2
+ const { errCodeMap } = require('../util/errCode');
2
3
  let URL;
3
4
  if (!process.browser) {
4
5
  ({ URL } = require('url'));
@@ -6,31 +7,38 @@ if (!process.browser) {
6
7
  URL = window.URL;
7
8
  }
8
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
9
12
 
10
13
  /**
11
14
  * @description 向 mirai-console 发送指令
12
15
  * @param {string} baseUrl mirai-api-http server 的地址
13
- * @param {string} verifyKey mirai-api-http server 设置的 verifyKey
16
+ * @param {string} sessionKey 会话标识
14
17
  * @param {string} command 指令名
15
- * @param {string[]} args 指令的参数
16
- * @returns {Object} 结构 { message }
18
+ * @param {MessageChain[]} args 指令的参数
19
+ * @returns {Object} 结构 { message, code }
17
20
  */
18
- module.exports = async ({ baseUrl, verifyKey, command: name, args }) => {
21
+ module.exports = async ({ baseUrl, sessionKey, command }) => {
19
22
  try {
20
23
  // 拼接 url
21
- const url = new URL('/command/send', baseUrl).toString();
24
+ const url = new URL('/cmd/execute', baseUrl).toString();
22
25
 
23
26
  // 请求
24
- const responseData = await axios.post(url, { verifyKey, name, args, });
27
+ const responseData = await axios.post(url, { sessionKey, command, });
25
28
  try {
26
29
  var {
27
- data: message
30
+ data: { msg: message, code },
28
31
  } = responseData;
29
32
  } catch (error) {
30
- throw new Error('core.sendCommand 请求返回格式出错,请检查 mirai-console');
33
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
31
34
  }
32
- return { message };
35
+ // 抛出 mirai 的异常,到 catch 中处理后再抛出
36
+ if (code in errCodeMap) {
37
+ throw new Error(message);
38
+ }
39
+ return { message, code };
33
40
  } catch (error) {
41
+ console.error(`mirai-js: error ${locationStr}`);
34
42
  errorHandler(error);
35
43
  }
36
44
 
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
 
11
13
  /**
12
14
  * @description 向 qq 好友发送消息
@@ -31,7 +33,7 @@ module.exports = async ({ baseUrl, sessionKey, target, quote, messageChain }) =>
31
33
  data: { msg: message, code, messageId }
32
34
  } = responseData;
33
35
  } catch (error) {
34
- throw new Error('core.sendFirendMessage 请求返回格式出错,请检查 mirai-console');
36
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
35
37
  }
36
38
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
37
39
  if (code in errCodeMap) {
@@ -39,6 +41,7 @@ module.exports = async ({ baseUrl, sessionKey, target, quote, messageChain }) =>
39
41
  }
40
42
  return messageId;
41
43
  } catch (error) {
44
+ console.error(`mirai-js: error ${locationStr}`);
42
45
  errorHandler(error);
43
46
  }
44
47
  };
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
 
11
13
  /**
12
14
  * @description 向 qq 群发送消息
@@ -31,7 +33,7 @@ module.exports = async ({ baseUrl, sessionKey, target, quote, messageChain }) =>
31
33
  data: { msg: message, code, messageId }
32
34
  } = responseData;
33
35
  } catch (error) {
34
- throw new Error('core.sendGroupMessage 请求返回格式出错,请检查 mirai-console');
36
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
35
37
  }
36
38
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
37
39
  if (code in errCodeMap) {
@@ -39,6 +41,7 @@ module.exports = async ({ baseUrl, sessionKey, target, quote, messageChain }) =>
39
41
  }
40
42
  return messageId;
41
43
  } catch (error) {
44
+ console.error(`mirai-js: error ${locationStr}`);
42
45
  errorHandler(error);
43
46
  }
44
47
  };
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
 
11
13
  /**
12
14
  * @description 发送戳一戳消息
@@ -31,7 +33,7 @@ module.exports = async ({ baseUrl, sessionKey, target, subject, kind }) => {
31
33
  data: { msg: message, code }
32
34
  } = responseData;
33
35
  } catch (error) {
34
- throw new Error('core.sendNudge 请求返回格式出错,请检查 mirai-console');
36
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
35
37
  }
36
38
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
37
39
  if (code in errCodeMap) {
@@ -39,6 +41,7 @@ module.exports = async ({ baseUrl, sessionKey, target, subject, kind }) => {
39
41
  }
40
42
  return { message, code };
41
43
  } catch (error) {
44
+ console.error(`mirai-js: error ${locationStr}`);
42
45
  errorHandler(error);
43
46
  }
44
47
  };
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
 
11
13
  /**
12
14
  * @description 向临时对象发送消息
@@ -23,25 +25,25 @@ module.exports = async ({ baseUrl, sessionKey, qq, group, quote, messageChain })
23
25
  // 拼接 url
24
26
  const url = new URL('/sendTempMessage', baseUrl).toString();
25
27
 
26
- // 请求
27
- var responseData;
28
- if (qq) {
29
- responseData = await axios.post(url, {
30
- sessionKey, qq, quote, messageChain
31
- });
32
- } else if (group) {
33
- responseData = await axios.post(url, {
34
- sessionKey, qq, quote, messageChain
35
- });
36
- } else {// 上层已经做了参数检查,这里有些多余
37
- throw new Error('sendTempMessage 缺少必要的 qq 或 group 参数');
28
+ if (!qq || !group) {
29
+ throw new Error('sendTempMessage 缺少必要的 qq 和 group 参数');
38
30
  }
31
+
32
+ // 请求
33
+ const responseData = await axios.post(url, {
34
+ sessionKey,
35
+ qq,
36
+ group,
37
+ quote,
38
+ messageChain
39
+ });
40
+
39
41
  try {
40
42
  var {
41
43
  data: { msg: message, code, messageId }
42
44
  } = responseData;
43
45
  } catch (error) {
44
- throw new Error('core.sendTempMessage 请求返回格式出错,请检查 mirai-console');
46
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
45
47
  }
46
48
 
47
49
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
@@ -50,6 +52,7 @@ module.exports = async ({ baseUrl, sessionKey, qq, group, quote, messageChain })
50
52
  }
51
53
  return messageId;
52
54
  } catch (error) {
55
+ console.error(`mirai-js: error ${locationStr}`);
53
56
  errorHandler(error);
54
57
  }
55
58
 
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
 
11
13
 
12
14
  /**
@@ -32,7 +34,7 @@ module.exports = async ({
32
34
  data: { msg: message, code }
33
35
  } = responseData;
34
36
  } catch (error) {
35
- throw new Error('core.setEssence 请求返回格式出错,请检查 mirai-console');
37
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
36
38
  }
37
39
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
38
40
  if (code in errCodeMap) {
@@ -40,6 +42,7 @@ module.exports = async ({
40
42
  }
41
43
  return { message, code };
42
44
  } catch (error) {
45
+ console.error(`mirai-js: error ${locationStr}`);
43
46
  errorHandler(error);
44
47
  }
45
48
  };
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
 
11
13
 
12
14
  /**
@@ -46,7 +48,7 @@ module.exports = async ({
46
48
  data: { msg: message, code }
47
49
  } = responseData;
48
50
  } catch (error) {
49
- throw new Error('core.setGroupConfig 请求返回格式出错,请检查 mirai-console');
51
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
50
52
  }
51
53
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
52
54
  if (code in errCodeMap) {
@@ -54,6 +56,7 @@ module.exports = async ({
54
56
  }
55
57
  return { message, code };
56
58
  } catch (error) {
59
+ console.error(`mirai-js: error ${locationStr}`);
57
60
  errorHandler(error);
58
61
  }
59
62
  };
@@ -0,0 +1,48 @@
1
+ const { errCodeMap } = require('../util/errCode');
2
+ const axios = require('axios');
3
+ let URL;
4
+ if (!process.browser) {
5
+ ({ URL } = require('url'));
6
+ } else {
7
+ URL = window.URL;
8
+ }
9
+ const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
12
+
13
+ /**
14
+ * @description 设置群成员权限
15
+ * @param {string} baseUrl mirai-api-http server 的地址
16
+ * @param {string} sessionKey 会话标识
17
+ * @param {number} target 群成员所在群号
18
+ * @param {number} memberId 群成员的 qq 号
19
+ * @param {string} assign 是否设置为管理员
20
+ * @returns {Object} 结构 { message, code }
21
+ */
22
+ module.exports = async ({ baseUrl, sessionKey, target, memberId, assign }) => {
23
+ try {
24
+ // 拼接 url
25
+ const url = new URL('/memberAdmin', baseUrl).toString();
26
+
27
+ // 请求
28
+ const responseData = await axios.post(url, {
29
+ sessionKey, target, memberId, assign
30
+ });
31
+ try {
32
+ var {
33
+ data: { msg: message, code }
34
+ } = responseData;
35
+ } catch (error) {
36
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
37
+ }
38
+ // 抛出 mirai 的异常,到 catch 中处理后再抛出
39
+ if (code in errCodeMap) {
40
+ throw new Error(message);
41
+ }
42
+ return { message, code };
43
+ } catch (error) {
44
+ console.error(`mirai-js: error ${locationStr}`);
45
+ errorHandler(error);
46
+ }
47
+
48
+ };
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
 
11
13
  /**
12
14
  * @description 设置群成员信息
@@ -35,7 +37,7 @@ module.exports = async ({ baseUrl, sessionKey, target, memberId, name, specialTi
35
37
  data: { msg: message, code }
36
38
  } = responseData;
37
39
  } catch (error) {
38
- throw new Error('core.setMemberInfo 请求返回格式出错,请检查 mirai-console');
40
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
39
41
  }
40
42
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
41
43
  if (code in errCodeMap) {
@@ -43,6 +45,7 @@ module.exports = async ({ baseUrl, sessionKey, target, memberId, name, specialTi
43
45
  }
44
46
  return { message, code };
45
47
  } catch (error) {
48
+ console.error(`mirai-js: error ${locationStr}`);
46
49
  errorHandler(error);
47
50
  }
48
51
 
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
 
11
13
 
12
14
  /**
@@ -29,7 +31,7 @@ module.exports = async ({ baseUrl, sessionKey, cacheSize, enableWebsocket }) =>
29
31
  data: { msg: message, code }
30
32
  } = responseData;
31
33
  } catch (error) {
32
- throw new Error('core.setSessionConfig 请求返回格式出错,请检查 mirai-console');
34
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
33
35
  }
34
36
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
35
37
  if (code in errCodeMap) {
@@ -37,6 +39,7 @@ module.exports = async ({ baseUrl, sessionKey, cacheSize, enableWebsocket }) =>
37
39
  }
38
40
  return { message, code };
39
41
  } catch (error) {
42
+ console.error(`mirai-js: error ${locationStr}`);
40
43
  errorHandler(error);
41
44
  }
42
45
  };
@@ -6,6 +6,8 @@ if (!process.browser) {
6
6
  URL = window.URL;
7
7
  }
8
8
  const errorHandler = require('../util/errorHandler');
9
+ const path = require('path');
10
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
9
11
 
10
12
  /**
11
13
  * @description 开始侦听事件
@@ -35,7 +37,7 @@ module.exports = async ({ baseUrl, sessionKey, verifyKey, message, error, close
35
37
 
36
38
  ws.onmessage = ({ data }) => {
37
39
  try {
38
- message(JSON.parse(data));
40
+ message(JSON.parse(data)?.data);
39
41
  } catch (error) { }// eslint-disable-line no-empty
40
42
  };
41
43
 
@@ -58,6 +60,7 @@ module.exports = async ({ baseUrl, sessionKey, verifyKey, message, error, close
58
60
  };
59
61
  return ws;
60
62
  } catch (error) {
63
+ console.error(`mirai-js: error ${locationStr}`);
61
64
  errorHandler(error);
62
65
  }
63
66
  };
@@ -6,6 +6,8 @@ if (!process.browser) {
6
6
  URL = window.URL;
7
7
  }
8
8
  const errorHandler = require('../util/errorHandler');
9
+ const path = require('path');
10
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
9
11
 
10
12
  /**
11
13
  * @description 开始侦听事件
@@ -70,6 +72,7 @@ module.exports = async ({ baseUrl, sessionKey, verifyKey, message, error, close,
70
72
  });
71
73
  return ws;
72
74
  } catch (error) {
75
+ console.error(`mirai-js: error ${locationStr}`);
73
76
  errorHandler(error);
74
77
  }
75
78
  };
@@ -1,4 +1,6 @@
1
1
  const errorHandler = require('../util/errorHandler');
2
+ const path = require('path');
3
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
2
4
 
3
5
  /**
4
6
  * @description 停止侦听事件
@@ -25,6 +27,7 @@ module.exports = async (wsConnection) => {
25
27
  // do nothing
26
28
  }
27
29
  } catch (error) {
30
+ console.error(`mirai-js: error ${locationStr}`);
28
31
  errorHandler(error);
29
32
  }
30
33
  };
@@ -1,4 +1,6 @@
1
1
  const errorHandler = require('../util/errorHandler');
2
+ const path = require('path');
3
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
2
4
 
3
5
  /**
4
6
  * @description 停止侦听事件
@@ -25,6 +27,7 @@ module.exports = async (wsConnection) => {
25
27
  // do nothing
26
28
  }
27
29
  } catch (error) {
30
+ console.error(`mirai-js: error ${locationStr}`);
28
31
  errorHandler(error);
29
32
  }
30
33
  };
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
 
11
13
  /**
12
14
  * @description 解除禁言
@@ -28,7 +30,7 @@ module.exports = async ({ baseUrl, sessionKey, target, memberId }) => {
28
30
  data: { code, msg: message }
29
31
  } = responseData;
30
32
  } catch (error) {
31
- throw new Error('core.unmute 请求返回格式出错,请检查 mirai-console');
33
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
32
34
  }
33
35
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
34
36
  if (code in errCodeMap) {
@@ -36,6 +38,7 @@ module.exports = async ({ baseUrl, sessionKey, target, memberId }) => {
36
38
  }
37
39
  return { message, code };
38
40
  } catch (error) {
41
+ console.error(`mirai-js: error ${locationStr}`);
39
42
  errorHandler(error);
40
43
  }
41
44
  };
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
 
11
13
  /**
12
14
  * @description 解除全员禁言
@@ -27,7 +29,7 @@ module.exports = async ({ baseUrl, sessionKey, target }) => {
27
29
  data: { code, msg: message }
28
30
  } = responseData;
29
31
  } catch (error) {
30
- throw new Error('core.unmuteAll 请求返回格式出错,请检查 mirai-console');
32
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
31
33
  }
32
34
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
33
35
  if (code in errCodeMap) {
@@ -35,6 +37,7 @@ module.exports = async ({ baseUrl, sessionKey, target }) => {
35
37
  }
36
38
  return { message, code };
37
39
  } catch (error) {
40
+ console.error(`mirai-js: error ${locationStr}`);
38
41
  errorHandler(error);
39
42
  }
40
43
  };
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
  const FormData = require('form-data');
11
13
 
12
14
 
@@ -42,7 +44,7 @@ module.exports = async ({ baseUrl, sessionKey, type, img }) => {
42
44
  data: { msg: message, code, imageId, url, path }
43
45
  } = responseData;
44
46
  } catch (error) {
45
- throw new Error('core.uploadImage 请求返回格式出错,请检查 mirai-console');
47
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
46
48
  }
47
49
 
48
50
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
@@ -51,6 +53,7 @@ module.exports = async ({ baseUrl, sessionKey, type, img }) => {
51
53
  }
52
54
  return { imageId, url, path };
53
55
  } catch (error) {
56
+ console.error(`mirai-js: error ${locationStr}`);
54
57
  errorHandler(error);
55
58
  }
56
59
  };
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
  const FormData = require('form-data');
11
13
 
12
14
 
@@ -42,7 +44,7 @@ module.exports = async ({ baseUrl, sessionKey, type, voice }) => {
42
44
  data: { msg: message, code, voiceId, url, path }
43
45
  } = responseData;
44
46
  } catch (error) {
45
- throw new Error('core.uploadVoice 请求返回格式出错,请检查 mirai-console');
47
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
46
48
  }
47
49
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
48
50
  if (code in errCodeMap) {
@@ -50,6 +52,7 @@ module.exports = async ({ baseUrl, sessionKey, type, voice }) => {
50
52
  }
51
53
  return { voiceId, url, path };
52
54
  } catch (error) {
55
+ console.error(`mirai-js: error ${locationStr}`);
53
56
  errorHandler(error);
54
57
  }
55
58
  };
@@ -7,6 +7,8 @@ if (!process.browser) {
7
7
  URL = window.URL;
8
8
  }
9
9
  const errorHandler = require('../util/errorHandler');
10
+ const path = require('path');
11
+ const locationStr = `core.${path.basename(__filename, path.extname(__filename))}`;
10
12
 
11
13
  /**
12
14
  * @description 校验 sessionKey,将一个 session 绑定到指定的 qq 上
@@ -28,7 +30,7 @@ module.exports = async ({ baseUrl, sessionKey, qq, throwable = true }) => {
28
30
  data: { msg: message, code },
29
31
  } = responseData;
30
32
  } catch (error) {
31
- throw new Error('core.verify 请求返回格式出错,请检查 mirai-console');
33
+ throw new Error(('请求返回格式出错,请检查 mirai-console'));
32
34
  }
33
35
  // 抛出 mirai 的异常,到 catch 中处理后再抛出
34
36
  if (code in errCodeMap) {
@@ -36,6 +38,7 @@ module.exports = async ({ baseUrl, sessionKey, qq, throwable = true }) => {
36
38
  }
37
39
  return { message, code };
38
40
  } catch (error) {
41
+ console.error(`mirai-js: error ${locationStr}`);
39
42
  errorHandler(error);
40
43
  }
41
44
 
package/webpack.config.js CHANGED
@@ -8,6 +8,10 @@ module.exports = {
8
8
  filename: 'mirai-js.js',
9
9
  clean: true
10
10
  },
11
+ externals: {
12
+ // ws v8+ 会需要配置更多 polyfill, 这里通过外部依赖直接忽略它
13
+ ws: 'ws'
14
+ },
11
15
  plugins: [
12
16
  new ProvidePlugin({
13
17
  process: 'process/browser',
@@ -1,44 +0,0 @@
1
- const { errCodeMap } = require('../util/errCode');
2
- const axios = require('axios').default;
3
- let URL;
4
- if (!process.browser) {
5
- ({ URL } = require('url'));
6
- } else {
7
- URL = window.URL;
8
- }
9
- const errorHandler = require('../util/errorHandler');
10
-
11
- /**
12
- * @description 获取群文件详细信息
13
- * @param {string} baseUrl mirai-api-http server 的地址
14
- * @param {string} sessionKey 会话标识
15
- * @param {number} target 群号
16
- * @param {string} id 文件 id
17
- * @returns {Object}
18
- * 结构 {
19
- * id, name, path, length, downloadTimes, uploaderId,
20
- * uploadTime, lastModifyTime, downloadUrl, sha1, md5
21
- * }
22
- */
23
- module.exports = async ({ baseUrl, sessionKey, target, id }) => {
24
- try {
25
- // 拼接 url
26
- const url = new URL('/groupFileInfo', baseUrl).toString();
27
-
28
- // 请求
29
- const responseData = await axios.get(url, { params: { sessionKey, target, id } });
30
- try {
31
- var { data, data: { msg: message, code } } = responseData;
32
- } catch (error) {
33
- throw new Error('core.getGroupFileInfo 请求返回格式出错,请检查 mirai-console');
34
- }
35
-
36
- // 抛出 mirai 的异常,到 catch 中处理后再抛出
37
- if (code in errCodeMap) {
38
- throw new Error(message);
39
- }
40
- return data;
41
- } catch (error) {
42
- errorHandler(error);
43
- }
44
- };
@@ -1,40 +0,0 @@
1
- const { errCodeMap } = require('../util/errCode');
2
- const axios = require('axios').default;
3
- let URL;
4
- if (!process.browser) {
5
- ({ URL } = require('url'));
6
- } else {
7
- URL = window.URL;
8
- }
9
- const errorHandler = require('../util/errorHandler');
10
-
11
- /**
12
- * @description 获取群文件列表
13
- * @param {string} baseUrl mirai-api-http server 的地址
14
- * @param {string} sessionKey 会话标识
15
- * @param {number} target 群号
16
- * @param {string} dir 可选,查询目录,默认为根目录
17
- * @returns {Object[]} 结构 array[...{ id, name, path, isFile }]
18
- */
19
- module.exports = async ({ baseUrl, sessionKey, target, dir }) => {
20
- try {
21
- // 拼接 url
22
- const url = new URL('/groupFileList', baseUrl).toString();
23
-
24
- // 请求
25
- const responseData = await axios.get(url, { params: { sessionKey, target, dir } });
26
- try {
27
- var { data, data: { msg: message, code } } = responseData;
28
- } catch (error) {
29
- throw new Error('core.getGroupFileList 请求返回格式出错,请检查 mirai-console');
30
- }
31
-
32
- // 抛出 mirai 的异常,到 catch 中处理后再抛出
33
- if (code in errCodeMap) {
34
- throw new Error(message);
35
- }
36
- return data;
37
- } catch (error) {
38
- errorHandler(error);
39
- }
40
- };