@alemonjs/kook 0.2.1 → 0.2.2
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/lib/index.d.ts +7 -7
- package/lib/index.js +284 -285
- package/lib/sdk/core/config.js +39 -40
- package/lib/sdk/core/utils/from.js +25 -32
- package/lib/sdk/platform/kook/sdk/api.d.ts +279 -267
- package/lib/sdk/platform/kook/sdk/api.js +262 -265
- package/lib/sdk/platform/kook/sdk/config.js +4 -4
- package/lib/sdk/platform/kook/sdk/conversation.js +108 -116
- package/lib/sdk/platform/kook/sdk/message/INTERACTION.d.ts +3 -3
- package/lib/sdk/platform/kook/sdk/message/MEMBER_ADD.d.ts +3 -3
- package/lib/sdk/platform/kook/sdk/message/MEMBER_REMOVE.d.ts +3 -3
- package/lib/sdk/platform/kook/sdk/message/MESSAGES_DIRECT.d.ts +3 -3
- package/lib/sdk/platform/kook/sdk/message/MESSAGES_PUBLIC.d.ts +3 -3
- package/lib/sdk/platform/kook/sdk/message/REACTIONS.d.ts +3 -3
- package/lib/sdk/platform/kook/sdk/message.d.ts +15 -15
- package/lib/sdk/platform/kook/sdk/message.js +16 -16
- package/lib/sdk/platform/kook/sdk/typings.d.ts +167 -138
- package/lib/sdk/platform/kook/sdk/typings.js +164 -164
- package/lib/sdk/platform/kook/sdk/wss.d.ts +22 -22
- package/lib/sdk/platform/kook/sdk/wss.js +138 -145
- package/lib/sdk/platform/kook/sdk/wss.types.d.ts +5 -5
- package/package.json +1 -1
|
@@ -1,151 +1,144 @@
|
|
|
1
|
-
import WebSocket from 'ws'
|
|
2
|
-
import { config } from './config.js'
|
|
3
|
-
import { KOOKAPI } from './api.js'
|
|
4
|
-
import { ConversationMap } from './conversation.js'
|
|
1
|
+
import WebSocket from 'ws'
|
|
2
|
+
import { config } from './config.js'
|
|
3
|
+
import { KOOKAPI } from './api.js'
|
|
4
|
+
import { ConversationMap } from './conversation.js'
|
|
5
5
|
|
|
6
6
|
class KOOKClient extends KOOKAPI {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
if (this.#events[t])
|
|
70
|
-
await this.#events[t](d);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
catch (err) {
|
|
74
|
-
if (this.#events['ERROR'])
|
|
75
|
-
this.#events['ERROR'](err);
|
|
76
|
-
}
|
|
77
|
-
//
|
|
78
|
-
}
|
|
79
|
-
else if (sn > this.#lastMessageSN + 1) ;
|
|
80
|
-
/**
|
|
81
|
-
* 如果收到已处理过的消息序号
|
|
82
|
-
* 则直接丢弃
|
|
83
|
-
*/
|
|
84
|
-
}
|
|
85
|
-
},
|
|
86
|
-
1: ({ d }) => {
|
|
87
|
-
if (d && d.code === 0) {
|
|
88
|
-
console.info('[ws] ok');
|
|
89
|
-
this.#sessionId = d.session_id;
|
|
90
|
-
this.#isConnected = true;
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
console.info('[ws] err');
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
2: () => {
|
|
97
|
-
console.info('[ws] ping');
|
|
98
|
-
this.#ws.send(JSON.stringify({
|
|
99
|
-
s: 3
|
|
100
|
-
}));
|
|
101
|
-
},
|
|
102
|
-
3: () => {
|
|
103
|
-
console.info('[ws] pong');
|
|
104
|
-
},
|
|
105
|
-
4: () => {
|
|
106
|
-
console.info('[ws] resume');
|
|
107
|
-
},
|
|
108
|
-
5: () => {
|
|
109
|
-
console.info('[ws] Connection failed, reconnect');
|
|
110
|
-
/**
|
|
111
|
-
* 处理 RECONNECT 信令
|
|
112
|
-
* 断开当前连接并进行重新连接
|
|
113
|
-
*/
|
|
114
|
-
this.#isConnected = false;
|
|
115
|
-
this.#sessionId = null;
|
|
116
|
-
console.info('[ws] sessionId', this.#sessionId);
|
|
117
|
-
},
|
|
118
|
-
6: () => {
|
|
119
|
-
console.info('[ws] resume ack');
|
|
120
|
-
}
|
|
121
|
-
};
|
|
122
|
-
this.#ws = new WebSocket(gatewayUrl);
|
|
123
|
-
this.#ws.on('open', () => {
|
|
124
|
-
console.info('[ws] open');
|
|
125
|
-
});
|
|
126
|
-
this.#ws.on('message', async (msg) => {
|
|
127
|
-
const message = JSON.parse(msg.toString('utf8'));
|
|
128
|
-
if (process.env.KOOK_WS == 'dev')
|
|
129
|
-
console.info('message', message);
|
|
130
|
-
if (map[message.s])
|
|
131
|
-
map[message.s](message);
|
|
132
|
-
});
|
|
133
|
-
// 心跳定时发送
|
|
134
|
-
setInterval(() => {
|
|
135
|
-
if (this.#isConnected) {
|
|
136
|
-
this.#ws.send(JSON.stringify({
|
|
137
|
-
s: 2,
|
|
138
|
-
sn: this.#lastMessageSN
|
|
139
|
-
}));
|
|
7
|
+
// 标记是否已连接
|
|
8
|
+
#isConnected = false
|
|
9
|
+
// 存储 session Id
|
|
10
|
+
#sessionId = null
|
|
11
|
+
// 存储最新的消息序号
|
|
12
|
+
#lastMessageSN = 0
|
|
13
|
+
/**
|
|
14
|
+
*
|
|
15
|
+
* @param opstion
|
|
16
|
+
*/
|
|
17
|
+
constructor(opstion) {
|
|
18
|
+
super()
|
|
19
|
+
config.set('token', opstion.token)
|
|
20
|
+
}
|
|
21
|
+
#ws
|
|
22
|
+
#events = {}
|
|
23
|
+
/**
|
|
24
|
+
* 注册事件处理程序
|
|
25
|
+
* @param key 事件名称
|
|
26
|
+
* @param val 事件处理函数
|
|
27
|
+
*/
|
|
28
|
+
on(key, val) {
|
|
29
|
+
this.#events[key] = val
|
|
30
|
+
return this
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* 使用获取到的网关连接地址建立 WebSocket 连接
|
|
34
|
+
* @param token
|
|
35
|
+
* @param conversation
|
|
36
|
+
*/
|
|
37
|
+
async connect() {
|
|
38
|
+
// 请求url
|
|
39
|
+
const gatewayUrl = await this.gateway()
|
|
40
|
+
.then(res => res?.data?.url)
|
|
41
|
+
.catch(err => {
|
|
42
|
+
if (this.#events['ERROR']) this.#events['ERROR'](err)
|
|
43
|
+
})
|
|
44
|
+
if (!gatewayUrl && gatewayUrl == '') return
|
|
45
|
+
// 建立连接
|
|
46
|
+
const map = {
|
|
47
|
+
0: async ({ d, sn }) => {
|
|
48
|
+
/**
|
|
49
|
+
* 处理 EVENT 信令
|
|
50
|
+
* 包括按序处理消息和记录最新的消息序号
|
|
51
|
+
*/
|
|
52
|
+
if (d && sn) {
|
|
53
|
+
if (sn === this.#lastMessageSN + 1) {
|
|
54
|
+
/**
|
|
55
|
+
* 消息序号正确
|
|
56
|
+
* 按序处理消息
|
|
57
|
+
*/
|
|
58
|
+
this.#lastMessageSN = sn
|
|
59
|
+
try {
|
|
60
|
+
if (d.channel_type == 'GROUP') {
|
|
61
|
+
const t = ConversationMap[d.type]['public'](d)
|
|
62
|
+
if (this.#events[t]) await this.#events[t](d)
|
|
63
|
+
} else {
|
|
64
|
+
const t = ConversationMap[d.type]['direct'](d)
|
|
65
|
+
if (this.#events[t]) await this.#events[t](d)
|
|
66
|
+
}
|
|
67
|
+
} catch (err) {
|
|
68
|
+
if (this.#events['ERROR']) this.#events['ERROR'](err)
|
|
140
69
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
70
|
+
//
|
|
71
|
+
} else if (sn > this.#lastMessageSN + 1);
|
|
72
|
+
/**
|
|
73
|
+
* 如果收到已处理过的消息序号
|
|
74
|
+
* 则直接丢弃
|
|
75
|
+
*/
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
1: ({ d }) => {
|
|
79
|
+
if (d && d.code === 0) {
|
|
80
|
+
console.info('[ws] ok')
|
|
81
|
+
this.#sessionId = d.session_id
|
|
82
|
+
this.#isConnected = true
|
|
83
|
+
} else {
|
|
84
|
+
console.info('[ws] err')
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
2: () => {
|
|
88
|
+
console.info('[ws] ping')
|
|
89
|
+
this.#ws.send(
|
|
90
|
+
JSON.stringify({
|
|
91
|
+
s: 3
|
|
92
|
+
})
|
|
93
|
+
)
|
|
94
|
+
},
|
|
95
|
+
3: () => {
|
|
96
|
+
console.info('[ws] pong')
|
|
97
|
+
},
|
|
98
|
+
4: () => {
|
|
99
|
+
console.info('[ws] resume')
|
|
100
|
+
},
|
|
101
|
+
5: () => {
|
|
102
|
+
console.info('[ws] Connection failed, reconnect')
|
|
103
|
+
/**
|
|
104
|
+
* 处理 RECONNECT 信令
|
|
105
|
+
* 断开当前连接并进行重新连接
|
|
106
|
+
*/
|
|
107
|
+
this.#isConnected = false
|
|
108
|
+
this.#sessionId = null
|
|
109
|
+
console.info('[ws] sessionId', this.#sessionId)
|
|
110
|
+
},
|
|
111
|
+
6: () => {
|
|
112
|
+
console.info('[ws] resume ack')
|
|
113
|
+
}
|
|
148
114
|
}
|
|
115
|
+
this.#ws = new WebSocket(gatewayUrl)
|
|
116
|
+
this.#ws.on('open', () => {
|
|
117
|
+
console.info('[ws] open')
|
|
118
|
+
})
|
|
119
|
+
this.#ws.on('message', async msg => {
|
|
120
|
+
const message = JSON.parse(msg.toString('utf8'))
|
|
121
|
+
if (process.env.KOOK_WS == 'dev') console.info('message', message)
|
|
122
|
+
if (map[message.s]) map[message.s](message)
|
|
123
|
+
})
|
|
124
|
+
// 心跳定时发送
|
|
125
|
+
setInterval(() => {
|
|
126
|
+
if (this.#isConnected) {
|
|
127
|
+
this.#ws.send(
|
|
128
|
+
JSON.stringify({
|
|
129
|
+
s: 2,
|
|
130
|
+
sn: this.#lastMessageSN
|
|
131
|
+
})
|
|
132
|
+
)
|
|
133
|
+
}
|
|
134
|
+
}, 30000)
|
|
135
|
+
this.#ws.on('close', () => {
|
|
136
|
+
console.error('[ws] close')
|
|
137
|
+
})
|
|
138
|
+
this.#ws.on('error', err => {
|
|
139
|
+
console.error('[ws] error', err)
|
|
140
|
+
})
|
|
141
|
+
}
|
|
149
142
|
}
|
|
150
143
|
|
|
151
|
-
export { KOOKClient }
|
|
144
|
+
export { KOOKClient }
|