@alemonjs/kook 0.2.2 → 0.2.3

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 };
package/package.json CHANGED
@@ -1,23 +1,31 @@
1
1
  {
2
2
  "name": "@alemonjs/kook",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "kook-bot",
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"
14
- }
17
+ },
18
+ "./package": "./package.json"
19
+ },
20
+ "devDependencies": {
21
+ "@rollup/plugin-typescript": "^11.1.6",
22
+ "lvyjs": "^0.2.13",
23
+ "rollup": "^4.18.1",
24
+ "rollup-plugin-dts": "^6.1.1",
25
+ "tsx": "^4.19.1"
15
26
  },
16
27
  "keywords": [
17
- "alemonjs",
18
- "kook",
19
- "bot",
20
- "chat-bot"
28
+ "alemonjs"
21
29
  ],
22
30
  "publishConfig": {
23
31
  "registry": "https://registry.npmjs.org",
package/lib/index.d.ts DELETED
@@ -1,9 +0,0 @@
1
- import * as alemonjs from 'alemonjs'
2
- import { KOOKClient } from './sdk/platform/kook/sdk/wss.js'
3
-
4
- type Client = typeof KOOKClient.prototype
5
- declare const platform = 'kook'
6
- declare const client: Client
7
- declare const _default: () => alemonjs.ClientAPI
8
-
9
- export { type Client, client, _default as default, platform }
@@ -1,288 +0,0 @@
1
- import * as axios from 'axios'
2
- import { AxiosRequestConfig } from 'axios'
3
- import { Readable } from 'stream'
4
- import { SendMessageParams, SendDirectMessageParams } from './typings.js'
5
-
6
- /**
7
- * api接口
8
- */
9
- declare class KOOKAPI {
10
- API_URL: string
11
- /**
12
- * KOOK服务
13
- * @param config
14
- * @returns
15
- */
16
- kookService(opstoin: AxiosRequestConfig): Promise<axios.AxiosResponse<any, any>>
17
- /**
18
- *
19
- * @returns
20
- */
21
- gateway(): Promise<any>
22
- /**
23
- * ************
24
- * 资源床单
25
- * ***********
26
- */
27
- /**
28
- * 发送buffer资源
29
- * @param id 私信传频道id,公信传子频道id
30
- * @param message {消息编号,图片,内容}
31
- * @returns
32
- */
33
- postImage(
34
- file: string | Buffer | Readable,
35
- Name?: string
36
- ): Promise<
37
- | false
38
- | {
39
- data: {
40
- url: string
41
- }
42
- }
43
- >
44
- /**
45
- * 发送buffer资源
46
- * @param id 私信传频道id,公信传子频道id
47
- * @param message {消息编号,图片,内容}
48
- * @returns
49
- */
50
- postFile(
51
- file: Buffer,
52
- Name?: string
53
- ): Promise<
54
- | false
55
- | {
56
- data: {
57
- url: string
58
- }
59
- }
60
- >
61
- /**
62
- * 转存
63
- * @param formdata
64
- * @returns
65
- */
66
- createUrl(formdata: any): Promise<{
67
- data: {
68
- url: string
69
- }
70
- }>
71
- /**
72
- * *********
73
- * 消息api
74
- * *********
75
- */
76
- /**
77
- * 创建消息
78
- * @param data
79
- * @returns
80
- */
81
- createMessage(data: SendMessageParams): Promise<{
82
- data: {
83
- msg_id: string
84
- msg_timestamp: number
85
- nonce: string
86
- }
87
- }>
88
- /**
89
- * 创建私聊消息
90
- */
91
- /**
92
- * 创建消息
93
- * @param target_id
94
- * @returns
95
- */
96
- userChatCreate(target_id: string): Promise<{
97
- data: {
98
- code: string
99
- last_read_time: number
100
- latest_msg_time: number
101
- unread_count: number
102
- is_friend: boolean
103
- is_blocked: boolean
104
- is_target_blocked: boolean
105
- target_info: {
106
- id: string
107
- username: string
108
- online: boolean
109
- avatar: string
110
- }
111
- }
112
- }>
113
- /**
114
- * 创建消息
115
- * @param data
116
- * @returns
117
- */
118
- createDirectMessage(data: SendDirectMessageParams): Promise<{
119
- data: {
120
- msg_id: string
121
- msg_timestamp: number
122
- nonce: string
123
- }
124
- }>
125
- /**
126
- * 删除指定消息
127
- * @param msg_id
128
- * @returns
129
- */
130
- messageDelete(msg_id: string): Promise<{
131
- data: {
132
- code: number
133
- message: string
134
- data: any[]
135
- }
136
- }>
137
- /**
138
- * 重编辑指定消息
139
- * @param data
140
- * @returns
141
- */
142
- messageUpdate(data: {
143
- msg_id: string
144
- content: any
145
- quote?: string
146
- temp_target_id?: string
147
- }): Promise<{
148
- data: {
149
- code: number
150
- message: string
151
- data: any[]
152
- }
153
- }>
154
- /**
155
- * 删回应
156
- * @param data
157
- * @returns
158
- */
159
- messageDeleteReaction(data: { msg_id: string; emoji: string; user_id: string }): Promise<{
160
- code: number
161
- message: string
162
- data: any[]
163
- }>
164
- /**
165
- * 发回应
166
- * @param data
167
- * @returns
168
- */
169
- messageAddReaction(data: { msg_id: string; emoji: string }): Promise<{
170
- code: number
171
- message: string
172
- data: any[]
173
- }>
174
- /**
175
- * 某个回应的所有用户
176
- * @param data
177
- * @returns
178
- */
179
- messageReactionList(params: { msg_id: string; emoji: string }): Promise<{
180
- code: number
181
- message: string
182
- data: {
183
- id: string
184
- username: string
185
- identify_num: string
186
- online: boolean
187
- status: number
188
- avatar: string
189
- bot: boolean
190
- tag_info: {
191
- color: string
192
- text: string
193
- }
194
- nickname: string
195
- reaction_time: number
196
- }
197
- }>
198
- /**
199
- * **********
200
- * user
201
- * *********
202
- */
203
- /**
204
- * 得到当前信息
205
- * @param guild_id
206
- * @param user_id
207
- * @returns
208
- */
209
- userMe(): Promise<{
210
- code: number
211
- message: string
212
- data: {
213
- id: string
214
- username: string
215
- identify_num: string
216
- online: false
217
- os: string
218
- status: number
219
- avatar: string
220
- banner: string
221
- bot: true
222
- mobile_verified: true
223
- client_id: string
224
- mobile_prefix: string
225
- mobile: string
226
- invited_count: number
227
- }
228
- }>
229
- /**
230
- * 得到用户信息
231
- * @param guild_id
232
- * @param user_id
233
- * @returns
234
- */
235
- userView(
236
- guild_id: string,
237
- user_id: string
238
- ): Promise<{
239
- code: number
240
- message: string
241
- data: {
242
- id: string
243
- username: string
244
- identify_num: string
245
- online: false
246
- status: 0
247
- bot: true
248
- avatar: string
249
- vip_avatar: string
250
- mobile_verified: true
251
- roles: number[]
252
- joined_at: number
253
- active_time: number
254
- }
255
- }>
256
- /**
257
- * 踢出
258
- * @param guild_id
259
- * @param user_id
260
- * @returns
261
- */
262
- guildKickout(
263
- guild_id: string,
264
- user_id: string
265
- ): Promise<{
266
- code: number
267
- message: string
268
- data: any
269
- }>
270
- /**
271
- * 创建角色
272
- * @param channel_id
273
- * @param type
274
- * @param value
275
- * @returns
276
- */
277
- channelRoleCreate(
278
- channel_id: string,
279
- type: string,
280
- value: string
281
- ): Promise<{
282
- code: number
283
- message: string
284
- data: any
285
- }>
286
- }
287
-
288
- export { KOOKAPI }
@@ -1,8 +0,0 @@
1
- import { SystemData } from '../typings.js'
2
-
3
- /**
4
- * @param event 按钮数据
5
- */
6
- type INTERACTION_TYPE = SystemData
7
-
8
- export type { INTERACTION_TYPE }
@@ -1,10 +0,0 @@
1
- import { SystemData } from '../typings.js'
2
-
3
- /**
4
- * 当成员加入时
5
- * @param event
6
- * @returns
7
- */
8
- type MEMBER_ADD_TYPE = SystemData
9
-
10
- export type { MEMBER_ADD_TYPE }
@@ -1,10 +0,0 @@
1
- import { SystemData } from '../typings.js'
2
-
3
- /**
4
- * 当成员移除时
5
- * @param event
6
- * @returns
7
- */
8
- type MEMBER_REMOVE_TYPE = SystemData
9
-
10
- export type { MEMBER_REMOVE_TYPE }
@@ -1,10 +0,0 @@
1
- import { EventData } from '../typings.js'
2
-
3
- /**
4
- * 私聊消息
5
- * @param event
6
- * @returns
7
- */
8
- type MESSAGES_DIRECT_TYPE = EventData
9
-
10
- export type { MESSAGES_DIRECT_TYPE }
@@ -1,10 +0,0 @@
1
- import { EventData } from '../typings.js'
2
-
3
- /**
4
- * 群聊消息
5
- * @param event
6
- * @returns
7
- */
8
- type MESSAGES_PUBLIC_TYPE = EventData
9
-
10
- export type { MESSAGES_PUBLIC_TYPE }
@@ -1,10 +0,0 @@
1
- import { SystemData } from '../typings.js'
2
-
3
- /**
4
- *
5
- * @param event
6
- * @returns
7
- */
8
- type REACTIONS_TYPE = SystemData
9
-
10
- export type { REACTIONS_TYPE }