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/.eslintrc.json +45 -45
- package/.github/FUNDING.yml +12 -12
- package/.github/dependabot.yml +14 -14
- package/.github/workflows/codeql-analysis.yml +70 -0
- package/.husky/pre-commit +5 -5
- package/LICENSE +661 -661
- package/demo.html +1 -1
- package/demo.js +59 -89
- package/dist/browser/mirai-js.js +1 -1
- package/dist/node/Bot.d.ts +6 -15
- package/dist/node/Bot.js +51 -44
- package/dist/node/Middleware.js +92 -27
- package/dist/node/core/getFriendList.js +2 -2
- package/dist/node/core/sendCommand.js +23 -12
- package/dist/node/core/sendTempMessage.js +12 -20
- package/dist/node/core/startListeningBrowser.js +3 -1
- package/docs/checkDocShouldBuild.sh +4 -0
- package/package.json +51 -51
- package/src/Bot.d.ts +6 -15
- package/src/Bot.js +29 -19
- package/src/Middleware.js +54 -10
- package/src/core/getFriendList.js +1 -1
- package/src/core/sendCommand.js +13 -8
- package/src/core/sendTempMessage.js +12 -12
- package/src/core/startListeningBrowser.js +1 -1
package/src/Middleware.js
CHANGED
@@ -19,15 +19,11 @@ class Middleware {
|
|
19
19
|
* @param {string} verifyKey mirai-api-http server 设置的 verifyKey
|
20
20
|
* @param {string} password 欲重新登陆的 qq 密码
|
21
21
|
*/
|
22
|
-
autoReLogin({
|
23
|
-
const { Bot } = require('./index.js');
|
22
|
+
autoReLogin({ password }) {
|
24
23
|
this.middleware.push(async (data, next) => {
|
25
24
|
try {
|
26
|
-
await
|
27
|
-
|
28
|
-
verifyKey,
|
29
|
-
command: '/login',
|
30
|
-
args: [data.qq, password],
|
25
|
+
await data.bot.sendCommand({
|
26
|
+
command: ['/login', data.qq, password],
|
31
27
|
});
|
32
28
|
await data.bot.open();
|
33
29
|
await next();
|
@@ -538,10 +534,58 @@ class Middleware {
|
|
538
534
|
if (data.type != 'GroupMessage' && data.type != 'FriendMessage') {
|
539
535
|
throw new Error('Middleware.syncWrapper 消息格式出错');
|
540
536
|
}
|
537
|
+
const watiForMessageChain = async (qq) => {
|
538
|
+
qq = qq ?? data?.sender?.id;
|
539
|
+
if (qq == undefined) {
|
540
|
+
throw new Error('Middleware.syncWrapper 消息格式出错');
|
541
|
+
}
|
542
|
+
do {
|
543
|
+
var { messageChain, id } = await data.bot?.waiter?.wait(data.type, ({ messageChain, sender: { id } }) => ({ messageChain, id })) ?? {};
|
544
|
+
} while (qq != id);
|
545
|
+
|
546
|
+
return messageChain;
|
547
|
+
};
|
548
|
+
|
549
|
+
const waitForText = async (qq) => {
|
550
|
+
qq = qq ?? data?.sender?.id;
|
551
|
+
if (qq == undefined) {
|
552
|
+
throw new Error('Middleware.syncWrapper 消息格式出错');
|
553
|
+
}
|
554
|
+
do {
|
555
|
+
var { text, id } = await data.bot?.waiter?.wait(data.type, new Middleware().textProcessor().done(({ text, sender: { id } }) => ({ text, id }))) ?? {};
|
556
|
+
} while (qq != id);
|
557
|
+
return text;
|
558
|
+
};
|
559
|
+
|
560
|
+
const waitForCustom = async (qq, processor) => {
|
561
|
+
qq = qq ?? data?.sender?.id;
|
562
|
+
if (qq == undefined) {
|
563
|
+
throw new Error('Middleware.syncWrapper 消息格式出错');
|
564
|
+
}
|
565
|
+
do {
|
566
|
+
var data = await data.bot?.waiter?.wait(data.type, new Middleware().textProcessor().done((data) => data));
|
567
|
+
} while (qq != data?.sender?.id);
|
568
|
+
return await processor(data);
|
569
|
+
};
|
570
|
+
|
541
571
|
data.waitFor = {
|
542
|
-
|
543
|
-
|
544
|
-
|
572
|
+
groupMember: (qq = undefined) => {
|
573
|
+
return {
|
574
|
+
messageChain: () => watiForMessageChain(qq),
|
575
|
+
text: () => waitForText(qq),
|
576
|
+
custom: (processor) => waitForCustom(qq, processor),
|
577
|
+
};
|
578
|
+
},
|
579
|
+
friend: (qq) => {
|
580
|
+
return {
|
581
|
+
messageChain: () => watiForMessageChain(qq),
|
582
|
+
text: () => waitForText(qq),
|
583
|
+
custom: (processor) => waitForCustom(qq, processor),
|
584
|
+
};
|
585
|
+
},
|
586
|
+
messageChain: () => watiForMessageChain(data.sender.id),
|
587
|
+
text: () => waitForText(data.sender.id),
|
588
|
+
custom: (processor) => waitForCustom(data.sender.id, processor),
|
545
589
|
};
|
546
590
|
|
547
591
|
await next();
|
@@ -22,7 +22,7 @@ module.exports = async ({ baseUrl, sessionKey }) => {
|
|
22
22
|
// 请求
|
23
23
|
const responseData = await axios.get(url, { params: { sessionKey } });
|
24
24
|
try {
|
25
|
-
var { data
|
25
|
+
var { data: { msg: message, code, data } } = responseData;
|
26
26
|
} catch (error) {
|
27
27
|
throw new Error('core.getFriendList 请求返回格式出错,请检查 mirai-console');
|
28
28
|
}
|
package/src/core/sendCommand.js
CHANGED
@@ -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'));
|
@@ -10,26 +11,30 @@ const errorHandler = require('../util/errorHandler');
|
|
10
11
|
/**
|
11
12
|
* @description 向 mirai-console 发送指令
|
12
13
|
* @param {string} baseUrl mirai-api-http server 的地址
|
13
|
-
* @param {string}
|
14
|
+
* @param {string} sessionKey 会话标识
|
14
15
|
* @param {string} command 指令名
|
15
|
-
* @param {
|
16
|
-
* @returns {Object} 结构 { message }
|
16
|
+
* @param {MessageChain[]} args 指令的参数
|
17
|
+
* @returns {Object} 结构 { message, code }
|
17
18
|
*/
|
18
|
-
module.exports = async ({ baseUrl,
|
19
|
+
module.exports = async ({ baseUrl, sessionKey, command }) => {
|
19
20
|
try {
|
20
21
|
// 拼接 url
|
21
|
-
const url = new URL('/
|
22
|
+
const url = new URL('/cmd/execute', baseUrl).toString();
|
22
23
|
|
23
24
|
// 请求
|
24
|
-
const responseData = await axios.post(url, {
|
25
|
+
const responseData = await axios.post(url, { sessionKey, command, });
|
25
26
|
try {
|
26
27
|
var {
|
27
|
-
data: message
|
28
|
+
data: { msg: message, code },
|
28
29
|
} = responseData;
|
29
30
|
} catch (error) {
|
30
31
|
throw new Error('core.sendCommand 请求返回格式出错,请检查 mirai-console');
|
31
32
|
}
|
32
|
-
|
33
|
+
// 抛出 mirai 的异常,到 catch 中处理后再抛出
|
34
|
+
if (code in errCodeMap) {
|
35
|
+
throw new Error(message);
|
36
|
+
}
|
37
|
+
return { message, code };
|
33
38
|
} catch (error) {
|
34
39
|
errorHandler(error);
|
35
40
|
}
|
@@ -23,19 +23,19 @@ module.exports = async ({ baseUrl, sessionKey, qq, group, quote, messageChain })
|
|
23
23
|
// 拼接 url
|
24
24
|
const url = new URL('/sendTempMessage', baseUrl).toString();
|
25
25
|
|
26
|
-
|
27
|
-
|
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 参数');
|
26
|
+
if (!qq || !group) {
|
27
|
+
throw new Error('sendTempMessage 缺少必要的 qq 和 group 参数');
|
38
28
|
}
|
29
|
+
|
30
|
+
// 请求
|
31
|
+
const responseData = await axios.post(url, {
|
32
|
+
sessionKey,
|
33
|
+
qq,
|
34
|
+
group,
|
35
|
+
quote,
|
36
|
+
messageChain
|
37
|
+
});
|
38
|
+
|
39
39
|
try {
|
40
40
|
var {
|
41
41
|
data: { msg: message, code, messageId }
|