@alemonjs/kook 0.2.2 → 0.2.5

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.
@@ -1,144 +1,151 @@
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
- #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)
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'])
43
+ this.#events['ERROR'](err);
44
+ });
45
+ if (!gatewayUrl && gatewayUrl == '')
46
+ return;
47
+ // 建立连接
48
+ const map = {
49
+ 0: async ({ d, sn }) => {
50
+ /**
51
+ * 处理 EVENT 信令
52
+ * 包括按序处理消息和记录最新的消息序号
53
+ */
54
+ if (d && sn) {
55
+ if (sn === this.#lastMessageSN + 1) {
56
+ /**
57
+ * 消息序号正确
58
+ * 按序处理消息
59
+ */
60
+ this.#lastMessageSN = sn;
61
+ try {
62
+ if (d.channel_type == 'GROUP') {
63
+ const t = ConversationMap[d.type]['public'](d);
64
+ if (this.#events[t])
65
+ await this.#events[t](d);
66
+ }
67
+ else {
68
+ const t = ConversationMap[d.type]['direct'](d);
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
+ }));
69
140
  }
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
- }
141
+ }, 30000);
142
+ this.#ws.on('close', () => {
143
+ console.error('[ws] close');
144
+ });
145
+ this.#ws.on('error', err => {
146
+ console.error('[ws] error', err);
147
+ });
114
148
  }
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
- }
142
149
  }
143
150
 
144
- export { KOOKClient }
151
+ export { KOOKClient };
@@ -3,10 +3,10 @@
3
3
  * ***
4
4
  */
5
5
  interface KOOKOptions {
6
- /**
7
- * 钥匙
8
- */
9
- token: string
6
+ /**
7
+ * 钥匙
8
+ */
9
+ token: string;
10
10
  }
11
11
 
12
- export type { KOOKOptions }
12
+ export type { KOOKOptions };
package/package.json CHANGED
@@ -1,23 +1,47 @@
1
1
  {
2
2
  "name": "@alemonjs/kook",
3
- "version": "0.2.2",
4
- "description": "kook-bot",
3
+ "version": "0.2.5",
4
+ "description": "阿柠檬kook平台连接",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
7
7
  "type": "module",
8
8
  "main": "lib/index.js",
9
9
  "types": "lib",
10
+ "scripts": {
11
+ "build": "node bundle.js"
12
+ },
10
13
  "exports": {
11
14
  ".": {
12
15
  "import": "./lib/index.js",
13
16
  "types": "./lib/index.d.ts"
17
+ },
18
+ "./package": "./package.json",
19
+ "./desktop": "./lib/desktop.js"
20
+ },
21
+ "alemonjs": {
22
+ "desktop": {
23
+ "platform": [
24
+ {
25
+ "name": "kook",
26
+ "value": "@alemonjs/kook"
27
+ }
28
+ ],
29
+ "commond": [
30
+ {
31
+ "name": "kook",
32
+ "commond": "open.kook"
33
+ }
34
+ ],
35
+ "sidebars": [
36
+ {
37
+ "name": "kook",
38
+ "commond": "open.kook"
39
+ }
40
+ ]
14
41
  }
15
42
  },
16
43
  "keywords": [
17
- "alemonjs",
18
- "kook",
19
- "bot",
20
- "chat-bot"
44
+ "alemonjs"
21
45
  ],
22
46
  "publishConfig": {
23
47
  "registry": "https://registry.npmjs.org",