mirai-js 2.8.0 → 2.8.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,7 +1,8 @@
1
- import { Processor, EventType } from './BaseType';
1
+ import { Processor, EventType, MessageChainElementTypes, MessageType } from './BaseType';
2
2
  import { Bot } from './Bot';
3
+ import { ArrayToValuesUnion } from './typeHelpers';
3
4
 
4
- export class Middleware {
5
+ export class Middleware<CTX = { [key: string]: any }> {
5
6
  private middleware: ((data: any, next: Middleware.NextMiddlewareCaller) => any)[];
6
7
  private catcher: (error: any) => any;
7
8
 
@@ -12,44 +13,44 @@ export class Middleware {
12
13
  * @param verifyKey mirai-api-http server 设置的 verifyKey
13
14
  * @param password 欲重新登陆的 qq 密码
14
15
  */
15
- autoReLogin({ bot, baseUrl, verifyKey, password }: Middleware.AutoReLoginOptions): Middleware;
16
+ autoReLogin({ bot, baseUrl, verifyKey, password }: Middleware.AutoReLoginOptions): Middleware<CTX>;
16
17
 
17
18
  /**
18
19
  * @description 自动重建 ws 连接
19
20
  * @param bot 欲重连的 Bot 实例
20
21
  */
21
- autoReconnection(bot: Bot): Middleware;
22
+ autoReconnection(bot: Bot): Middleware<CTX>;
22
23
 
23
24
  /**
24
25
  * @description 过滤出指定类型的消息,消息类型为 key,对应类型的
25
26
  * message 数组为 value,置于 data.classified
26
27
  * @param typeArr message 的类型,例如 Plain Image Voice
27
28
  */
28
- messageProcessor(typeArr: string[]): Middleware;
29
+ messageProcessor<U extends MessageChainElementTypes[]>(typeArr: U): Middleware<CTX & { classified: { [type in ArrayToValuesUnion<U>]: any[] } }>;
29
30
 
30
31
  /**
31
32
  * @description 过滤出字符串类型的 message,并拼接在一起,置于 data.text
32
33
  */
33
- textProcessor(): Middleware;
34
+ textProcessor(): Middleware<CTX & { text: string }>;
34
35
 
35
36
  /**
36
37
  * @description 过滤出消息 id,置于 data.messageId
37
38
  */
38
- messageIdProcessor(): Middleware;
39
+ messageIdProcessor(): Middleware<CTX & { messageId: string }>;
39
40
 
40
41
  /**
41
42
  * @description 过滤指定的群消息
42
43
  * @param groupArr 允许通过的群号数组
43
44
  * @param allow 允许通过还是禁止通过
44
45
  */
45
- groupFilter(groupArr: number[], allow?: boolean): Middleware;
46
+ groupFilter(groupArr: number[], allow?: boolean): Middleware<CTX>;
46
47
 
47
48
  /**
48
49
  * @description 过滤指定的好友消息
49
50
  * @param friendArr 好友 qq 号数组
50
51
  * @param allow 允许通过还是禁止通过
51
52
  */
52
- friendFilter(friendArr: number[], allow?: boolean): Middleware;
53
+ friendFilter(friendArr: number[], allow?: boolean): Middleware<CTX>;
53
54
 
54
55
  /**
55
56
  * @description 过滤指定群的群成员的消息
@@ -57,32 +58,32 @@ export class Middleware {
57
58
  * @param allow 允许通过还是禁止通过
58
59
  * 结构 { number => number[], } key 为允许通过的群号,value 为该群允许通过的成员 qq
59
60
  */
60
- groupMemberFilter(groupMemberMap: Middleware.GroupMemberMap, allow?: boolean): Middleware;
61
+ groupMemberFilter(groupMemberMap: Middleware.GroupMemberMap, allow?: boolean): Middleware<CTX>;
61
62
 
62
63
  /**
63
64
  * @description 这是一个对话锁,保证群中同一成员不能在中途触发处理器
64
65
  * @use 在你需要保护的过程结束后调用 data.unlock 即可
65
66
  */
66
- memberLock({ autoUnlock }?: Middleware.LockOptions): Middleware;
67
+ memberLock({ autoUnlock }?: Middleware.LockOptions): Middleware<CTX & { unlock: () => void }>;
67
68
 
68
69
  /**
69
70
  * @description 这是一个对话锁,保证同一好友不能在中途触发处理器
70
71
  * @use 在你需要保护的过程结束后调用 data.unlock 即可
71
72
  */
72
- friendLock({ autoUnlock }?: Middleware.LockOptions): Middleware;
73
+ friendLock({ autoUnlock }?: Middleware.LockOptions): Middleware<CTX & { unlock: () => void }>;
73
74
 
74
75
  /**
75
76
  * @description 过滤包含指定 @ 信息的消息
76
77
  * @param atArr 必选,qq 号数组
77
78
  * @param allow 可选,允许通过还是禁止通过
78
79
  */
79
- atFilter(friendArr: number[], allow?: boolean): Middleware;
80
+ atFilter(friendArr: number[], allow?: boolean): Middleware<CTX>;
80
81
 
81
82
  /**
82
83
  * @description 用于 NewFriendRequestEvent 的中间件,经过该中间件后,将在 data 下放置三个方法
83
84
  * agree、refuse、refuseAndAddBlacklist,调用后将分别进行好友请求的 同意、拒绝和拒绝并加入黑名单
84
85
  */
85
- friendRequestProcessor(): Middleware;
86
+ friendRequestProcessor(): Middleware<CTX & { agree: () => void, refuse: () => void, refuseAndAddBlacklist: () => void }>;
86
87
 
87
88
  /**
88
89
  * ! mirai-core 的问题,有时候收不到 MemberJoinRequestEvent 事件
@@ -93,9 +94,11 @@ export class Middleware {
93
94
  * ignore 忽略
94
95
  * refuseAndAddBlacklist 拒绝并移入黑名单
95
96
  * ignoreAndAddBlacklist 忽略并移入黑名单
96
- * @param bot 必选,Bot 实例
97
97
  */
98
- memberJoinRequestProcessor(): Middleware;
98
+ memberJoinRequestProcessor(): Middleware<CTX & {
99
+ agree: () => void, refuse: () => void, ignore: () => void,
100
+ refuseAndAddBlacklist: () => void, ignoreAndAddBlacklist: () => void,
101
+ }>;
99
102
 
100
103
  /**
101
104
  * ! 目前被邀请入群不会触发 BotInvitedJoinGroupRequestEvent 事件
@@ -103,32 +106,47 @@ export class Middleware {
103
106
  * @description 用于 BotInvitedJoinGroupRequestEvent 的中间件,经过该中间件后,将在 data 下放置两个方法
104
107
  * agree 同意
105
108
  * refuse 拒绝
106
- * @param bot 必选,Bot 实例
107
109
  */
108
- invitedJoinGroupRequestProcessor(): Middleware;
110
+ invitedJoinGroupRequestProcessor(): Middleware<CTX & { agree: () => void, refuse: () => void }>;
109
111
 
110
112
  /**
111
113
  * @description Waiter 的包装器,提供方便的同步 IO 方式
112
114
  */
113
- syncWrapper(): Middleware;
115
+ syncWrapper(): Middleware<CTX & {
116
+ waitFor: {
117
+ groupMember: (qq: number) => {
118
+ messageChain: () => Promise<MessageType[]>,
119
+ text: () => Promise<string>,
120
+ custom: <R>(processor: () => R) => Promise<R>,
121
+ },
122
+ friend: (qq) => {
123
+ messageChain: () => Promise<MessageType[]>,
124
+ text: () => Promise<string>,
125
+ custom: <R>(processor: () => R) => Promise<R>,
126
+ },
127
+ messageChain: () => Promise<MessageType[]>,
128
+ text: Promise<string>,
129
+ custom: <R>(processor: () => R) => Promise<R>,
130
+ }
131
+ }>;
114
132
 
115
133
  /**
116
134
  * @description 添加一个自定义中间件
117
135
  * @param callback (data, next) => void
118
136
  */
119
- use(callback: (data: any, next: Middleware.NextMiddlewareCaller) => any): Middleware;
137
+ use(callback: (data: CTX, next: Middleware.NextMiddlewareCaller) => any): Middleware<CTX>;
120
138
 
121
139
  /**
122
140
  * @description 使用错误处理器
123
141
  * @param catcher 错误处理器 (err) => void
124
142
  */
125
- catch(catcher: (error: any) => any): Middleware;
143
+ catch(catcher: (error: any) => any): Middleware<CTX>;
126
144
 
127
145
  /**
128
146
  * @description 生成一个带有中间件的事件处理器
129
147
  * @param callback 事件处理器
130
148
  */
131
- done<E extends EventType[] | EventType>(callback: Processor<E extends EventType ? [E] : E>): Processor<E extends EventType ? [E] : E>;
149
+ done<E extends EventType[]>(callback: Processor<E, CTX>): Processor<E, CTX>;
132
150
 
133
151
  }
134
152
 
@@ -0,0 +1,40 @@
1
+ const { errCodeMap } = require('../util/errCode');
2
+ const axios = require('axios');
3
+ const { URL } = require('../polyfill/URL');
4
+ const errorHandler = require('../util/errorHandler');
5
+ const path = require('path');
6
+ const { isBrowserEnv } = require('../util/isBrowserEnv');
7
+ const locationStr = !isBrowserEnv() ? `core.${path.basename(__filename, path.extname(__filename))}` : 'borwser';
8
+
9
+ /**
10
+ * @description 通过 messageId 获取消息
11
+ * @param {string} baseUrl mirai-api-http server 的地址
12
+ * @param {string} sessionKey 会话标识
13
+ * @param {number} target qq 号/群号
14
+ * @param {number} messageId 消息 id
15
+ * @returns {Object} 结构 { type, messageChain, sender }
16
+ */
17
+ module.exports = async ({ baseUrl, sessionKey, target, messageId }) => {
18
+ try {
19
+ // 拼接 url
20
+ const url = new URL('/messageFromId', baseUrl).toString();
21
+
22
+ // 请求
23
+ const responseData = await axios.get(url, { params: { sessionKey, target, messageId } });
24
+ try {
25
+ var {
26
+ data: { msg: message, code, data }
27
+ } = responseData;
28
+ } catch (error) {
29
+ throw new Error('请求返回格式出错,请检查 mirai-console');
30
+ }
31
+ // 抛出 mirai 的异常,到 catch 中处理后再抛出
32
+ if (code in errCodeMap) {
33
+ throw new Error(message);
34
+ }
35
+ return data;
36
+ } catch (error) {
37
+ console.error(`mirai-js: error ${locationStr}`);
38
+ errorHandler(error);
39
+ }
40
+ };