@alemonjs/kook 2.1.0-alpha.4 → 2.1.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/lib/index.js CHANGED
@@ -12,6 +12,10 @@ const main = () => {
12
12
  token: config.token
13
13
  });
14
14
  void client.connect();
15
+ let botId = '';
16
+ client.userMe().then(res => {
17
+ botId = String(res?.data?.id ?? '');
18
+ }).catch(() => { });
15
19
  const port = process.env?.port || 17117;
16
20
  const url = `ws://127.0.0.1:${port}`;
17
21
  const cbp = cbpPlatform(url);
@@ -38,8 +42,8 @@ const main = () => {
38
42
  MessageId: event.msg_id,
39
43
  MessageText: msg,
40
44
  OpenId: data?.code,
41
- CreateAt: Date.now(),
42
- tag: 'MESSAGES_DIRECT',
45
+ BotId: botId,
46
+ _tag: 'MESSAGES_DIRECT',
43
47
  value: event
44
48
  };
45
49
  cbp.send(e);
@@ -51,12 +55,12 @@ const main = () => {
51
55
  const data = await client.userChatCreate(event.extra.author.id).then(res => res?.data);
52
56
  const avatar = event.extra.author.avatar;
53
57
  let msg = event.content;
54
- const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? [];
55
- for (const item of mention_role_part) {
58
+ const mentionRolePart = event.extra.kmarkdown?.mention_role_part ?? [];
59
+ for (const item of mentionRolePart) {
56
60
  msg = msg.replace(`(rol)${item.role_id}(rol)`, '').trim();
57
61
  }
58
- const mention_part = event.extra.kmarkdown?.mention_part ?? [];
59
- for (const item of mention_part) {
62
+ const mentionPart = event.extra.kmarkdown?.mention_part ?? [];
63
+ for (const item of mentionPart) {
60
64
  msg = msg.replace(`(met)${item.id}(met)`, '').trim();
61
65
  }
62
66
  const UserAvatar = avatar.substring(0, avatar.indexOf('?'));
@@ -77,8 +81,8 @@ const main = () => {
77
81
  MessageId: event.msg_id,
78
82
  MessageText: msg,
79
83
  OpenId: data?.code,
80
- CreateAt: Date.now(),
81
- tag: 'MESSAGES_PUBLIC',
84
+ BotId: botId,
85
+ _tag: 'MESSAGES_PUBLIC',
82
86
  value: event
83
87
  };
84
88
  cbp.send(e);
@@ -86,11 +90,247 @@ const main = () => {
86
90
  client.on('ERROR', msg => {
87
91
  console.error(msg);
88
92
  });
93
+ client.on('REACTIONS', event => {
94
+ const reactionType = event.extra?.type;
95
+ const body = event.extra?.body;
96
+ if (!body) {
97
+ return;
98
+ }
99
+ if (reactionType === 'added_reaction') {
100
+ const e = {
101
+ name: 'message.reaction.add',
102
+ Platform: platform,
103
+ GuildId: body.channel_id ?? '',
104
+ ChannelId: body.channel_id ?? '',
105
+ SpaceId: body.channel_id ?? '',
106
+ MessageId: body.msg_id ?? '',
107
+ BotId: botId,
108
+ _tag: 'REACTIONS_ADD',
109
+ value: event
110
+ };
111
+ cbp.send(e);
112
+ }
113
+ else if (reactionType === 'deleted_reaction') {
114
+ const e = {
115
+ name: 'message.reaction.remove',
116
+ Platform: platform,
117
+ GuildId: body.channel_id ?? '',
118
+ ChannelId: body.channel_id ?? '',
119
+ SpaceId: body.channel_id ?? '',
120
+ MessageId: body.msg_id ?? '',
121
+ BotId: botId,
122
+ _tag: 'REACTIONS_REMOVE',
123
+ value: event
124
+ };
125
+ cbp.send(e);
126
+ }
127
+ });
128
+ client.on('MEMBER_ADD', event => {
129
+ const body = event.extra?.body;
130
+ if (!body) {
131
+ return;
132
+ }
133
+ const UserId = body.user_id ?? event.author_id;
134
+ const [isMaster, UserKey] = getMaster(UserId);
135
+ const e = {
136
+ name: 'member.add',
137
+ Platform: platform,
138
+ GuildId: event.target_id ?? '',
139
+ ChannelId: '',
140
+ SpaceId: event.target_id ?? '',
141
+ UserId: UserId,
142
+ UserKey,
143
+ IsMaster: isMaster,
144
+ IsBot: false,
145
+ MessageId: event.msg_id ?? '',
146
+ BotId: botId,
147
+ _tag: 'MEMBER_ADD',
148
+ value: event
149
+ };
150
+ cbp.send(e);
151
+ });
152
+ client.on('MEMBER_REMOVE', event => {
153
+ const body = event.extra?.body;
154
+ if (!body) {
155
+ return;
156
+ }
157
+ const UserId = body.user_id ?? event.author_id;
158
+ const [isMaster, UserKey] = getMaster(UserId);
159
+ const e = {
160
+ name: 'member.remove',
161
+ Platform: platform,
162
+ GuildId: event.target_id ?? '',
163
+ ChannelId: '',
164
+ SpaceId: event.target_id ?? '',
165
+ UserId: UserId,
166
+ UserKey,
167
+ IsMaster: isMaster,
168
+ IsBot: false,
169
+ MessageId: event.msg_id ?? '',
170
+ BotId: botId,
171
+ _tag: 'MEMBER_REMOVE',
172
+ value: event
173
+ };
174
+ cbp.send(e);
175
+ });
176
+ client.on('MESSAGES_UPDATE', event => {
177
+ const body = event.extra?.body;
178
+ if (!body) {
179
+ return;
180
+ }
181
+ const e = {
182
+ name: 'message.update',
183
+ Platform: platform,
184
+ GuildId: body.channel_id ?? event.target_id ?? '',
185
+ ChannelId: body.channel_id ?? event.target_id ?? '',
186
+ SpaceId: body.channel_id ?? event.target_id ?? '',
187
+ UserId: body.author_id ?? event.author_id ?? '',
188
+ UserKey: '',
189
+ IsMaster: false,
190
+ IsBot: false,
191
+ MessageId: body.msg_id ?? event.msg_id ?? '',
192
+ BotId: botId,
193
+ _tag: 'MESSAGES_UPDATE',
194
+ value: event
195
+ };
196
+ cbp.send(e);
197
+ });
198
+ client.on('MESSAGES_DELETE', event => {
199
+ const body = event.extra?.body;
200
+ if (!body) {
201
+ return;
202
+ }
203
+ const e = {
204
+ name: 'message.delete',
205
+ Platform: platform,
206
+ GuildId: body.channel_id ?? event.target_id ?? '',
207
+ ChannelId: body.channel_id ?? event.target_id ?? '',
208
+ SpaceId: body.channel_id ?? event.target_id ?? '',
209
+ MessageId: body.msg_id ?? event.msg_id ?? '',
210
+ BotId: botId,
211
+ _tag: 'MESSAGES_DELETE',
212
+ value: event
213
+ };
214
+ cbp.send(e);
215
+ });
216
+ client.on('MESSAGES_PIN', event => {
217
+ const body = event.extra?.body;
218
+ if (!body) {
219
+ return;
220
+ }
221
+ const e = {
222
+ name: 'message.pin',
223
+ Platform: platform,
224
+ GuildId: body.channel_id ?? event.target_id ?? '',
225
+ ChannelId: body.channel_id ?? event.target_id ?? '',
226
+ SpaceId: body.channel_id ?? event.target_id ?? '',
227
+ MessageId: body.msg_id ?? event.msg_id ?? '',
228
+ BotId: botId,
229
+ _tag: 'MESSAGES_PIN',
230
+ value: event
231
+ };
232
+ cbp.send(e);
233
+ });
234
+ client.on('GUILD_JOIN', event => {
235
+ const body = event.extra?.body;
236
+ const e = {
237
+ name: 'guild.join',
238
+ Platform: platform,
239
+ GuildId: body?.guild_id ?? event.target_id ?? '',
240
+ ChannelId: '',
241
+ SpaceId: body?.guild_id ?? event.target_id ?? '',
242
+ UserId: event.author_id ?? '',
243
+ UserKey: '',
244
+ IsMaster: false,
245
+ IsBot: true,
246
+ MessageId: event.msg_id ?? '',
247
+ BotId: botId,
248
+ _tag: 'GUILD_JOIN',
249
+ value: event
250
+ };
251
+ cbp.send(e);
252
+ });
253
+ client.on('GUILD_EXIT', event => {
254
+ const body = event.extra?.body;
255
+ const e = {
256
+ name: 'guild.exit',
257
+ Platform: platform,
258
+ GuildId: body?.guild_id ?? event.target_id ?? '',
259
+ ChannelId: '',
260
+ SpaceId: body?.guild_id ?? event.target_id ?? '',
261
+ UserId: event.author_id ?? '',
262
+ UserKey: '',
263
+ IsMaster: false,
264
+ IsBot: true,
265
+ MessageId: event.msg_id ?? '',
266
+ BotId: botId,
267
+ _tag: 'GUILD_EXIT',
268
+ value: event
269
+ };
270
+ cbp.send(e);
271
+ });
272
+ client.on('CHANNEL_CREATE', event => {
273
+ const body = event.extra?.body;
274
+ if (!body) {
275
+ return;
276
+ }
277
+ const e = {
278
+ name: 'channel.create',
279
+ Platform: platform,
280
+ GuildId: body.guild_id ?? event.target_id ?? '',
281
+ ChannelId: body.id ?? '',
282
+ SpaceId: body.guild_id ?? event.target_id ?? '',
283
+ MessageId: event.msg_id ?? '',
284
+ BotId: botId,
285
+ _tag: 'CHANNEL_CREATE',
286
+ value: event
287
+ };
288
+ cbp.send(e);
289
+ });
290
+ client.on('CHANNEL_DELETE', event => {
291
+ const body = event.extra?.body;
292
+ if (!body) {
293
+ return;
294
+ }
295
+ const e = {
296
+ name: 'channel.delete',
297
+ Platform: platform,
298
+ GuildId: body.guild_id ?? event.target_id ?? '',
299
+ ChannelId: body.id ?? '',
300
+ SpaceId: body.guild_id ?? event.target_id ?? '',
301
+ MessageId: event.msg_id ?? '',
302
+ BotId: botId,
303
+ _tag: 'CHANNEL_DELETE',
304
+ value: event
305
+ };
306
+ cbp.send(e);
307
+ });
308
+ client.on('CHANNEL_UPDATE', event => {
309
+ const body = event.extra?.body;
310
+ if (!body) {
311
+ return;
312
+ }
313
+ const e = {
314
+ name: 'channel.update',
315
+ Platform: platform,
316
+ GuildId: body.guild_id ?? event.target_id ?? '',
317
+ ChannelId: body.id ?? '',
318
+ SpaceId: body.guild_id ?? event.target_id ?? '',
319
+ MessageId: event.msg_id ?? '',
320
+ BotId: botId,
321
+ _tag: 'CHANNEL_UPDATE',
322
+ value: event
323
+ };
324
+ cbp.send(e);
325
+ });
89
326
  const formatKookContent = (val) => {
90
327
  return val
91
- .filter(item => item.type === 'Mention' || item.type === 'Text')
328
+ .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
92
329
  .map(item => {
93
- if (item.type === 'Mention') {
330
+ if (item.type === 'Link') {
331
+ return `[${item.value}](${item?.options?.link ?? item.value})`;
332
+ }
333
+ else if (item.type === 'Mention') {
94
334
  if (item.value === 'everyone' || item.value === 'all' || item.value === '' || typeof item.value !== 'string') {
95
335
  return '(met)all(met)';
96
336
  }
@@ -167,25 +407,38 @@ const main = () => {
167
407
  const url = imageRes.data?.url;
168
408
  return url || null;
169
409
  };
170
- const sendChannel = async (target_id, val) => {
410
+ const sendChannel = async (targetId, val) => {
171
411
  if (!val || val.length <= 0) {
172
412
  return [];
173
413
  }
174
414
  const content = formatKookContent(val);
175
415
  try {
416
+ const imageUrl = await uploadAndGetImageUrl(val);
417
+ if (imageUrl && content) {
418
+ const imgRes = await client.createMessage({
419
+ type: 2,
420
+ target_id: targetId,
421
+ content: imageUrl
422
+ });
423
+ const txtRes = await client.createMessage({
424
+ type: 9,
425
+ target_id: targetId,
426
+ content: content
427
+ });
428
+ return [createResult(ResultCode.Ok, 'client.createMessage', imgRes), createResult(ResultCode.Ok, 'client.createMessage', txtRes)];
429
+ }
176
430
  if (content) {
177
431
  const res = await client.createMessage({
178
432
  type: 9,
179
- target_id: target_id,
433
+ target_id: targetId,
180
434
  content: content
181
435
  });
182
436
  return [createResult(ResultCode.Ok, 'client.createMessage', res)];
183
437
  }
184
- const imageUrl = await uploadAndGetImageUrl(val);
185
438
  if (imageUrl) {
186
439
  const res = await client.createMessage({
187
440
  type: 2,
188
- target_id: target_id,
441
+ target_id: targetId,
189
442
  content: imageUrl
190
443
  });
191
444
  return [createResult(ResultCode.Ok, 'client.createMessage', res)];
@@ -196,25 +449,38 @@ const main = () => {
196
449
  return [createResult(ResultCode.Fail, 'client.createMessage', error)];
197
450
  }
198
451
  };
199
- const sendUser = async (open_id, val) => {
452
+ const sendUser = async (openId, val) => {
200
453
  if (!val || val.length <= 0) {
201
454
  return [];
202
455
  }
203
456
  const content = formatKookContent(val);
204
457
  try {
458
+ const imageUrl = await uploadAndGetImageUrl(val);
459
+ if (imageUrl && content) {
460
+ const imgRes = await client.createDirectMessage({
461
+ type: 2,
462
+ chat_code: openId,
463
+ content: imageUrl
464
+ });
465
+ const txtRes = await client.createDirectMessage({
466
+ type: 9,
467
+ chat_code: openId,
468
+ content: content
469
+ });
470
+ return [createResult(ResultCode.Ok, 'client.createDirectMessage', imgRes), createResult(ResultCode.Ok, 'client.createDirectMessage', txtRes)];
471
+ }
205
472
  if (content) {
206
473
  const res = await client.createDirectMessage({
207
474
  type: 9,
208
- chat_code: open_id,
475
+ chat_code: openId,
209
476
  content: content
210
477
  });
211
478
  return [createResult(ResultCode.Ok, 'client.createDirectMessage', res)];
212
479
  }
213
- const imageUrl = await uploadAndGetImageUrl(val);
214
480
  if (imageUrl) {
215
481
  const res = await client.createDirectMessage({
216
482
  type: 2,
217
- chat_code: open_id,
483
+ chat_code: openId,
218
484
  content: imageUrl
219
485
  });
220
486
  return [createResult(ResultCode.Ok, 'client.createDirectMessage', res)];
@@ -248,8 +514,8 @@ const main = () => {
248
514
  mention: e => {
249
515
  const event = e.value;
250
516
  const MessageMention = [];
251
- const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? [];
252
- for (const item of mention_role_part) {
517
+ const mentionRolePart = event.extra.kmarkdown?.mention_role_part ?? [];
518
+ for (const item of mentionRolePart) {
253
519
  const UserId = item.role_id;
254
520
  const [isMaster, UserKey] = getMaster(UserId);
255
521
  MessageMention.push({
@@ -260,8 +526,8 @@ const main = () => {
260
526
  IsBot: true
261
527
  });
262
528
  }
263
- const mention_part = event.extra.kmarkdown?.mention_part ?? [];
264
- for (const item of mention_part) {
529
+ const mentionPart = event.extra.kmarkdown?.mention_part ?? [];
530
+ for (const item of mentionPart) {
265
531
  const UserId = item.id;
266
532
  const [isMaster, UserKey] = getMaster(UserId);
267
533
  MessageMention.push({
@@ -291,9 +557,9 @@ const main = () => {
291
557
  consume(res.map(item => createResult(ResultCode.Ok, '请求完成', item)));
292
558
  }
293
559
  else if (data.action === 'message.send.channel') {
294
- const channel_id = data.payload.ChannelId;
560
+ const channelId = data.payload.ChannelId;
295
561
  const val = data.payload.params.format;
296
- const res = await api.active.send.channel(channel_id, val);
562
+ const res = await api.active.send.channel(channelId, val);
297
563
  if (!res) {
298
564
  consume([createResult(ResultCode.Ok, '请求完成', null)]);
299
565
  return;
@@ -39,12 +39,12 @@ const ConversationMap = {
39
39
  return '';
40
40
  }
41
41
  else if (event.extra.type === 'exited_channel') {
42
- console.info('111exited_channel');
42
+ console.info('exited_channel');
43
43
  return '';
44
44
  }
45
45
  else if (event.extra.type === 'updated_channel') {
46
46
  console.info('updated_channel');
47
- return '';
47
+ return KOOKEventKey['CHANNEL_UPDATE'];
48
48
  }
49
49
  else if (event.extra.type === 'joined_guild') {
50
50
  console.info('joined_guild');
@@ -54,13 +54,33 @@ const ConversationMap = {
54
54
  console.info('exited_guild');
55
55
  return KOOKEventKey['MEMBER_REMOVE'];
56
56
  }
57
+ else if (event.extra.type === 'self_joined_guild') {
58
+ console.info('self_joined_guild');
59
+ return KOOKEventKey['GUILD_JOIN'];
60
+ }
61
+ else if (event.extra.type === 'self_exited_guild') {
62
+ console.info('self_exited_guild');
63
+ return KOOKEventKey['GUILD_EXIT'];
64
+ }
65
+ else if (event.extra.type === 'added_channel') {
66
+ console.info('added_channel');
67
+ return KOOKEventKey['CHANNEL_CREATE'];
68
+ }
69
+ else if (event.extra.type === 'deleted_channel') {
70
+ console.info('deleted_channel');
71
+ return KOOKEventKey['CHANNEL_DELETE'];
72
+ }
57
73
  else if (event.extra.type === 'updated_message') {
58
74
  console.info('updated_message');
59
- return '';
75
+ return KOOKEventKey['MESSAGES_UPDATE'];
76
+ }
77
+ else if (event.extra.type === 'deleted_message') {
78
+ console.info('deleted_message');
79
+ return KOOKEventKey['MESSAGES_DELETE'];
60
80
  }
61
81
  else if (event.extra.type === 'pinned_message') {
62
82
  console.info('pinned_message');
63
- return '';
83
+ return KOOKEventKey['MESSAGES_PIN'];
64
84
  }
65
85
  },
66
86
  direct: (event) => {
@@ -11,6 +11,14 @@ export declare const KOOKEventKey: {
11
11
  MESSAGES_DIRECT: string;
12
12
  MESSAGES_PUBLIC: string;
13
13
  REACTIONS: string;
14
+ MESSAGES_UPDATE: string;
15
+ MESSAGES_DELETE: string;
16
+ MESSAGES_PIN: string;
17
+ CHANNEL_CREATE: string;
18
+ CHANNEL_DELETE: string;
19
+ CHANNEL_UPDATE: string;
20
+ GUILD_JOIN: string;
21
+ GUILD_EXIT: string;
14
22
  ERROR: string;
15
23
  };
16
24
  export type KOOKEventMap = {
@@ -20,5 +28,13 @@ export type KOOKEventMap = {
20
28
  MESSAGES_DIRECT: MESSAGES_DIRECT_TYPE;
21
29
  MESSAGES_PUBLIC: MESSAGES_PUBLIC_TYPE;
22
30
  REACTIONS: REACTIONS_TYPE;
31
+ MESSAGES_UPDATE: any;
32
+ MESSAGES_DELETE: any;
33
+ MESSAGES_PIN: any;
34
+ CHANNEL_CREATE: any;
35
+ CHANNEL_DELETE: any;
36
+ CHANNEL_UPDATE: any;
37
+ GUILD_JOIN: any;
38
+ GUILD_EXIT: any;
23
39
  ERROR: any;
24
40
  };
@@ -5,6 +5,14 @@ const KOOKEventKey = {
5
5
  MESSAGES_DIRECT: 'MESSAGES_DIRECT',
6
6
  MESSAGES_PUBLIC: 'MESSAGES_PUBLIC',
7
7
  REACTIONS: 'REACTIONS',
8
+ MESSAGES_UPDATE: 'MESSAGES_UPDATE',
9
+ MESSAGES_DELETE: 'MESSAGES_DELETE',
10
+ MESSAGES_PIN: 'MESSAGES_PIN',
11
+ CHANNEL_CREATE: 'CHANNEL_CREATE',
12
+ CHANNEL_DELETE: 'CHANNEL_DELETE',
13
+ CHANNEL_UPDATE: 'CHANNEL_UPDATE',
14
+ GUILD_JOIN: 'GUILD_JOIN',
15
+ GUILD_EXIT: 'GUILD_EXIT',
8
16
  ERROR: 'ERROR'
9
17
  };
10
18
 
@@ -255,4 +255,4 @@ export interface EditingData {
255
255
  embeds: any[];
256
256
  msg_id: string;
257
257
  }
258
- export declare const SystemDataEnum: readonly ["exited_guild", "joined_guild", "joined_channel", "exited_channel", "updated_channel", "pinned_message", "guild_member_online", "added_reaction", "deleted_reaction", "updated_message", "message_btn_click"];
258
+ export declare const SystemDataEnum: readonly ["joined_guild", "exited_guild", "self_joined_guild", "self_exited_guild", "joined_channel", "exited_channel", "updated_channel", "added_channel", "deleted_channel", "pinned_message", "deleted_message", "guild_member_online", "added_reaction", "deleted_reaction", "updated_message", "message_btn_click"];
@@ -77,12 +77,17 @@ var ApiEnum;
77
77
  ApiEnum["GatewayIndex"] = "/api/v3/gateway/index";
78
78
  })(ApiEnum || (ApiEnum = {}));
79
79
  const SystemDataEnum = [
80
- 'exited_guild',
81
80
  'joined_guild',
81
+ 'exited_guild',
82
+ 'self_joined_guild',
83
+ 'self_exited_guild',
82
84
  'joined_channel',
83
85
  'exited_channel',
84
86
  'updated_channel',
87
+ 'added_channel',
88
+ 'deleted_channel',
85
89
  'pinned_message',
90
+ 'deleted_message',
86
91
  'guild_member_online',
87
92
  'added_reaction',
88
93
  'deleted_reaction',
package/lib/sdk/wss.js CHANGED
@@ -83,7 +83,12 @@ class KOOKClient extends KOOKAPI {
83
83
  console.info('[ws] Connection failed, reconnect');
84
84
  this.#isConnected = false;
85
85
  this.#sessionId = null;
86
- console.info('[ws] sessionId', this.#sessionId);
86
+ if (this.#heartbeatInterval) {
87
+ clearInterval(this.#heartbeatInterval);
88
+ this.#heartbeatInterval = null;
89
+ }
90
+ this.#ws.close();
91
+ void this.connect();
87
92
  },
88
93
  6: () => {
89
94
  console.info('[ws] resume ack');
@@ -120,6 +125,13 @@ class KOOKClient extends KOOKAPI {
120
125
  this.#heartbeatInterval = null;
121
126
  }
122
127
  console.error('[ws] close');
128
+ if (this.#isConnected) {
129
+ this.#isConnected = false;
130
+ this.#sessionId = null;
131
+ setTimeout(() => {
132
+ void this.connect();
133
+ }, 5000);
134
+ }
123
135
  });
124
136
  this.#ws.on('error', err => {
125
137
  console.error('[ws] error', err);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/kook",
3
- "version": "2.1.0-alpha.4",
3
+ "version": "2.1.0",
4
4
  "description": "kook platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",