@alemonjs/kook 2.1.0-alpha.3 → 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/config.d.ts +4 -5
- package/lib/config.js +1 -1
- package/lib/core/config.d.ts +8 -0
- package/lib/core/config.js +0 -23
- package/lib/desktop.d.ts +1 -3
- package/lib/desktop.js +0 -9
- package/lib/hook.d.ts +7 -19
- package/lib/hook.js +0 -10
- package/lib/index.d.ts +5 -6
- package/lib/index.js +396 -242
- package/lib/sdk/api.d.ts +3 -117
- package/lib/sdk/api.js +0 -112
- package/lib/sdk/config.d.ts +4 -0
- package/lib/sdk/conversation.d.ts +35 -0
- package/lib/sdk/conversation.js +24 -49
- package/lib/sdk/index.d.ts +2 -0
- package/lib/sdk/index.js +2 -0
- package/lib/sdk/instance.d.ts +3 -0
- package/lib/sdk/instance.js +12 -28
- package/lib/sdk/message/INTERACTION.d.ts +2 -0
- package/lib/sdk/message/INTERACTION.js +1 -0
- package/lib/sdk/message/MEMBER_ADD.d.ts +2 -0
- package/lib/sdk/message/MEMBER_ADD.js +1 -0
- package/lib/sdk/message/MEMBER_REMOVE.d.ts +2 -0
- package/lib/sdk/message/MEMBER_REMOVE.js +1 -0
- package/lib/sdk/message/MESSAGES_DIRECT.d.ts +2 -0
- package/lib/sdk/message/MESSAGES_DIRECT.js +1 -0
- package/lib/sdk/message/MESSAGES_PUBLIC.d.ts +2 -0
- package/lib/sdk/message/MESSAGES_PUBLIC.js +1 -0
- package/lib/sdk/message/REACTIONS.d.ts +2 -0
- package/lib/sdk/message/REACTIONS.js +1 -0
- package/lib/sdk/message.d.ts +40 -0
- package/lib/sdk/message.js +11 -7
- package/lib/sdk/typings.d.ts +192 -43
- package/lib/sdk/typings.js +19 -89
- package/lib/sdk/wss.d.ts +9 -0
- package/lib/sdk/wss.js +23 -40
- package/lib/sdk/wss.types.d.ts +3 -0
- package/lib/sdk/wss.types.js +1 -0
- package/package.json +3 -3
package/lib/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { cbpPlatform, createResult, ResultCode } from 'alemonjs';
|
|
1
|
+
import { definePlatform, cbpPlatform, createResult, ResultCode } from 'alemonjs';
|
|
2
2
|
export { KOOKAPI as API } from './sdk/api.js';
|
|
3
3
|
import { KOOKClient } from './sdk/wss.js';
|
|
4
4
|
import { readFileSync } from 'fs';
|
|
@@ -8,128 +8,329 @@ export { useClient, useValue } from './hook.js';
|
|
|
8
8
|
|
|
9
9
|
const main = () => {
|
|
10
10
|
const config = getKOOKConfig();
|
|
11
|
-
// 创建客户端
|
|
12
11
|
const client = new KOOKClient({
|
|
13
12
|
token: config.token
|
|
14
13
|
});
|
|
15
|
-
// 连接
|
|
16
14
|
void client.connect();
|
|
15
|
+
let botId = '';
|
|
16
|
+
client.userMe().then(res => {
|
|
17
|
+
botId = String(res?.data?.id ?? '');
|
|
18
|
+
}).catch(() => { });
|
|
17
19
|
const port = process.env?.port || 17117;
|
|
18
20
|
const url = `ws://127.0.0.1:${port}`;
|
|
19
21
|
const cbp = cbpPlatform(url);
|
|
20
22
|
client.on('MESSAGES_DIRECT', async (event) => {
|
|
21
|
-
// 过滤机器人
|
|
22
23
|
if (event.extra?.author?.bot) {
|
|
23
24
|
return false;
|
|
24
25
|
}
|
|
25
|
-
// 创建私聊标记
|
|
26
26
|
const data = await client.userChatCreate(event.extra.author.id).then(res => res?.data);
|
|
27
|
-
// 头像
|
|
28
27
|
const avatar = event.extra.author.avatar;
|
|
29
|
-
// 获取消息
|
|
30
28
|
const msg = event.content;
|
|
31
29
|
const url = avatar.substring(0, avatar.indexOf('?'));
|
|
32
30
|
const UserAvatar = url;
|
|
33
31
|
const UserId = event.author_id;
|
|
34
32
|
const [isMaster, UserKey] = getMaster(UserId);
|
|
35
|
-
// 定义消
|
|
36
33
|
const e = {
|
|
37
34
|
name: 'private.message.create',
|
|
38
|
-
// 事件类型
|
|
39
35
|
Platform: platform,
|
|
40
|
-
// 用户Id
|
|
41
36
|
UserId: UserId,
|
|
42
37
|
UserKey,
|
|
43
38
|
UserName: event.extra.author.username,
|
|
44
39
|
UserAvatar: UserAvatar,
|
|
45
40
|
IsMaster: isMaster,
|
|
46
41
|
IsBot: false,
|
|
47
|
-
// message
|
|
48
42
|
MessageId: event.msg_id,
|
|
49
43
|
MessageText: msg,
|
|
50
44
|
OpenId: data?.code,
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
tag: 'MESSAGES_PUBLIC',
|
|
45
|
+
BotId: botId,
|
|
46
|
+
_tag: 'MESSAGES_DIRECT',
|
|
54
47
|
value: event
|
|
55
48
|
};
|
|
56
49
|
cbp.send(e);
|
|
57
50
|
});
|
|
58
|
-
// 监听消息
|
|
59
51
|
client.on('MESSAGES_PUBLIC', async (event) => {
|
|
60
|
-
// 过滤机器人
|
|
61
52
|
if (event.extra?.author?.bot) {
|
|
62
53
|
return false;
|
|
63
54
|
}
|
|
64
|
-
// 创建私聊标记
|
|
65
55
|
const data = await client.userChatCreate(event.extra.author.id).then(res => res?.data);
|
|
66
|
-
// 头像
|
|
67
56
|
const avatar = event.extra.author.avatar;
|
|
68
|
-
// 获取消息
|
|
69
57
|
let msg = event.content;
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
* 包括机器人在内
|
|
73
|
-
*/
|
|
74
|
-
const mention_role_part = event.extra.kmarkdown?.mention_role_part ?? [];
|
|
75
|
-
for (const item of mention_role_part) {
|
|
58
|
+
const mentionRolePart = event.extra.kmarkdown?.mention_role_part ?? [];
|
|
59
|
+
for (const item of mentionRolePart) {
|
|
76
60
|
msg = msg.replace(`(rol)${item.role_id}(rol)`, '').trim();
|
|
77
61
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
*/
|
|
81
|
-
const mention_part = event.extra.kmarkdown?.mention_part ?? [];
|
|
82
|
-
for (const item of mention_part) {
|
|
62
|
+
const mentionPart = event.extra.kmarkdown?.mention_part ?? [];
|
|
63
|
+
for (const item of mentionPart) {
|
|
83
64
|
msg = msg.replace(`(met)${item.id}(met)`, '').trim();
|
|
84
65
|
}
|
|
85
66
|
const UserAvatar = avatar.substring(0, avatar.indexOf('?'));
|
|
86
67
|
const UserId = event.author_id;
|
|
87
68
|
const [isMaster, UserKey] = getMaster(UserId);
|
|
88
|
-
// 定义消
|
|
89
69
|
const e = {
|
|
90
70
|
name: 'message.create',
|
|
91
|
-
// 事件类型
|
|
92
71
|
Platform: platform,
|
|
93
|
-
//
|
|
94
72
|
GuildId: event.extra.guild_id,
|
|
95
73
|
ChannelId: event.target_id,
|
|
96
74
|
SpaceId: event.target_id,
|
|
97
|
-
// 用户Id
|
|
98
75
|
UserId: UserId,
|
|
99
76
|
UserKey,
|
|
100
77
|
UserName: event.extra.author.username,
|
|
101
78
|
UserAvatar: UserAvatar,
|
|
102
79
|
IsMaster: isMaster,
|
|
103
80
|
IsBot: false,
|
|
104
|
-
// message
|
|
105
81
|
MessageId: event.msg_id,
|
|
106
82
|
MessageText: msg,
|
|
107
83
|
OpenId: data?.code,
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
tag: 'MESSAGES_PUBLIC',
|
|
84
|
+
BotId: botId,
|
|
85
|
+
_tag: 'MESSAGES_PUBLIC',
|
|
111
86
|
value: event
|
|
112
87
|
};
|
|
113
88
|
cbp.send(e);
|
|
114
89
|
});
|
|
115
|
-
// 发送错误时
|
|
116
90
|
client.on('ERROR', msg => {
|
|
117
91
|
console.error(msg);
|
|
118
92
|
});
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
*/
|
|
125
|
-
const sendChannel = async (target_id, val) => {
|
|
126
|
-
if (val.length < 0) {
|
|
127
|
-
return Promise.all([]);
|
|
93
|
+
client.on('REACTIONS', event => {
|
|
94
|
+
const reactionType = event.extra?.type;
|
|
95
|
+
const body = event.extra?.body;
|
|
96
|
+
if (!body) {
|
|
97
|
+
return;
|
|
128
98
|
}
|
|
129
|
-
|
|
130
|
-
|
|
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
|
+
});
|
|
326
|
+
const formatKookContent = (val) => {
|
|
327
|
+
return val
|
|
328
|
+
.filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
|
|
131
329
|
.map(item => {
|
|
132
|
-
if (item.type === '
|
|
330
|
+
if (item.type === 'Link') {
|
|
331
|
+
return `[${item.value}](${item?.options?.link ?? item.value})`;
|
|
332
|
+
}
|
|
333
|
+
else if (item.type === 'Mention') {
|
|
133
334
|
if (item.value === 'everyone' || item.value === 'all' || item.value === '' || typeof item.value !== 'string') {
|
|
134
335
|
return '(met)all(met)';
|
|
135
336
|
}
|
|
@@ -159,52 +360,86 @@ const main = () => {
|
|
|
159
360
|
}
|
|
160
361
|
return item.value;
|
|
161
362
|
}
|
|
363
|
+
return '';
|
|
162
364
|
})
|
|
163
365
|
.join('');
|
|
366
|
+
};
|
|
367
|
+
const resolveImageBuffer = async (val) => {
|
|
368
|
+
const images = val.filter(item => item.type === 'Image' || item.type === 'ImageFile' || item.type === 'ImageURL');
|
|
369
|
+
for (const item of images) {
|
|
370
|
+
if (item.type === 'Image') {
|
|
371
|
+
if (Buffer.isBuffer(item.value)) {
|
|
372
|
+
return item.value;
|
|
373
|
+
}
|
|
374
|
+
else if (typeof item.value === 'string') {
|
|
375
|
+
if (item.value.startsWith('http://') || item.value.startsWith('https://')) {
|
|
376
|
+
return await getBufferByURL(item.value);
|
|
377
|
+
}
|
|
378
|
+
else if (item.value.startsWith('base64://')) {
|
|
379
|
+
return Buffer.from(item.value.slice(9), 'base64');
|
|
380
|
+
}
|
|
381
|
+
else if (item.value.startsWith('file://')) {
|
|
382
|
+
return readFileSync(item.value.slice(7));
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
return Buffer.from(item.value, 'base64');
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
else if (item.type === 'ImageURL') {
|
|
390
|
+
return await getBufferByURL(item.value);
|
|
391
|
+
}
|
|
392
|
+
else if (item.type === 'ImageFile') {
|
|
393
|
+
return readFileSync(item.value);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
return null;
|
|
397
|
+
};
|
|
398
|
+
const uploadAndGetImageUrl = async (val) => {
|
|
399
|
+
const bufferData = await resolveImageBuffer(val);
|
|
400
|
+
if (!bufferData) {
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
const imageRes = await client.postImage(bufferData);
|
|
404
|
+
if (!imageRes || typeof imageRes === 'boolean') {
|
|
405
|
+
return null;
|
|
406
|
+
}
|
|
407
|
+
const url = imageRes.data?.url;
|
|
408
|
+
return url || null;
|
|
409
|
+
};
|
|
410
|
+
const sendChannel = async (targetId, val) => {
|
|
411
|
+
if (!val || val.length <= 0) {
|
|
412
|
+
return [];
|
|
413
|
+
}
|
|
414
|
+
const content = formatKookContent(val);
|
|
164
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
|
+
}
|
|
165
430
|
if (content) {
|
|
166
431
|
const res = await client.createMessage({
|
|
167
432
|
type: 9,
|
|
168
|
-
target_id:
|
|
433
|
+
target_id: targetId,
|
|
169
434
|
content: content
|
|
170
435
|
});
|
|
171
436
|
return [createResult(ResultCode.Ok, 'client.createMessage', res)];
|
|
172
437
|
}
|
|
173
|
-
|
|
174
|
-
if (images.length > 0) {
|
|
175
|
-
let bufferData = null;
|
|
176
|
-
for (let i = 0; i < images.length; i++) {
|
|
177
|
-
if (bufferData) {
|
|
178
|
-
break;
|
|
179
|
-
}
|
|
180
|
-
const item = images[i];
|
|
181
|
-
if (item.type === 'Image') {
|
|
182
|
-
bufferData = Buffer.from(item.value, 'base64');
|
|
183
|
-
}
|
|
184
|
-
else if (item.type === 'ImageURL') {
|
|
185
|
-
bufferData = await getBufferByURL(item.value);
|
|
186
|
-
}
|
|
187
|
-
else if (item.type === 'ImageFile') {
|
|
188
|
-
bufferData = readFileSync(item.value);
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
if (!bufferData) {
|
|
192
|
-
return [];
|
|
193
|
-
}
|
|
194
|
-
// 上传图片
|
|
195
|
-
const imageRes = await client.postImage(bufferData);
|
|
196
|
-
if (!imageRes) {
|
|
197
|
-
return [];
|
|
198
|
-
}
|
|
199
|
-
const url = imageRes.data?.url;
|
|
200
|
-
if (!url) {
|
|
201
|
-
return [];
|
|
202
|
-
}
|
|
203
|
-
// 发送消息
|
|
438
|
+
if (imageUrl) {
|
|
204
439
|
const res = await client.createMessage({
|
|
205
440
|
type: 2,
|
|
206
|
-
target_id:
|
|
207
|
-
content:
|
|
441
|
+
target_id: targetId,
|
|
442
|
+
content: imageUrl
|
|
208
443
|
});
|
|
209
444
|
return [createResult(ResultCode.Ok, 'client.createMessage', res)];
|
|
210
445
|
}
|
|
@@ -214,94 +449,39 @@ const main = () => {
|
|
|
214
449
|
return [createResult(ResultCode.Fail, 'client.createMessage', error)];
|
|
215
450
|
}
|
|
216
451
|
};
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
* @param channel_id
|
|
220
|
-
* @param val
|
|
221
|
-
* @returns
|
|
222
|
-
*/
|
|
223
|
-
const sendUser = async (open_id, val) => {
|
|
224
|
-
if (val.length < 0) {
|
|
452
|
+
const sendUser = async (openId, val) => {
|
|
453
|
+
if (!val || val.length <= 0) {
|
|
225
454
|
return [];
|
|
226
455
|
}
|
|
227
|
-
const content = val
|
|
228
|
-
.filter(item => item.type === 'Mention' || item.type === 'Text')
|
|
229
|
-
.map(item => {
|
|
230
|
-
if (item.type === 'Mention') {
|
|
231
|
-
if (item.value === 'everyone' || item.value === 'all' || item.value === '' || typeof item.value !== 'string') {
|
|
232
|
-
return '(met)all(met)';
|
|
233
|
-
}
|
|
234
|
-
if (item.options?.belong === 'user') {
|
|
235
|
-
return `(met)${item.value}(met)`;
|
|
236
|
-
}
|
|
237
|
-
else if (item.options?.belong === 'channel') {
|
|
238
|
-
return `(chn)${item.value}(chn)`;
|
|
239
|
-
}
|
|
240
|
-
return '';
|
|
241
|
-
}
|
|
242
|
-
else if (item.type === 'Text') {
|
|
243
|
-
if (item.options?.style === 'block') {
|
|
244
|
-
return `\`${item.value}\``;
|
|
245
|
-
}
|
|
246
|
-
else if (item.options?.style === 'italic') {
|
|
247
|
-
return `*${item.value}*`;
|
|
248
|
-
}
|
|
249
|
-
else if (item.options?.style === 'bold') {
|
|
250
|
-
return `**${item.value}**`;
|
|
251
|
-
}
|
|
252
|
-
else if (item.options?.style === 'strikethrough') {
|
|
253
|
-
return `~~${item.value}~~`;
|
|
254
|
-
}
|
|
255
|
-
else if (item.options?.style === 'boldItalic') {
|
|
256
|
-
return `***${item.value}***`;
|
|
257
|
-
}
|
|
258
|
-
return item.value;
|
|
259
|
-
}
|
|
260
|
-
})
|
|
261
|
-
.join('');
|
|
456
|
+
const content = formatKookContent(val);
|
|
262
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
|
+
}
|
|
263
472
|
if (content) {
|
|
264
473
|
const res = await client.createDirectMessage({
|
|
265
474
|
type: 9,
|
|
266
|
-
chat_code:
|
|
475
|
+
chat_code: openId,
|
|
267
476
|
content: content
|
|
268
477
|
});
|
|
269
478
|
return [createResult(ResultCode.Ok, 'client.createDirectMessage', res)];
|
|
270
479
|
}
|
|
271
|
-
|
|
272
|
-
if (images.length > 0) {
|
|
273
|
-
let bufferData = null;
|
|
274
|
-
for (let i = 0; i < images.length; i++) {
|
|
275
|
-
if (bufferData) {
|
|
276
|
-
break;
|
|
277
|
-
}
|
|
278
|
-
const item = images[i];
|
|
279
|
-
if (item.type === 'Image') {
|
|
280
|
-
bufferData = Buffer.from(item.value, 'base64');
|
|
281
|
-
}
|
|
282
|
-
else if (item.type === 'ImageURL') {
|
|
283
|
-
bufferData = await getBufferByURL(item.value);
|
|
284
|
-
}
|
|
285
|
-
else if (item.type === 'ImageFile') {
|
|
286
|
-
bufferData = readFileSync(item.value);
|
|
287
|
-
}
|
|
288
|
-
}
|
|
289
|
-
if (!bufferData) {
|
|
290
|
-
return [];
|
|
291
|
-
}
|
|
292
|
-
// 上传图片
|
|
293
|
-
const imageRes = await client.postImage(bufferData);
|
|
294
|
-
if (!imageRes) {
|
|
295
|
-
return [];
|
|
296
|
-
}
|
|
297
|
-
const url = imageRes.data?.url;
|
|
298
|
-
if (!url) {
|
|
299
|
-
return [];
|
|
300
|
-
}
|
|
480
|
+
if (imageUrl) {
|
|
301
481
|
const res = await client.createDirectMessage({
|
|
302
482
|
type: 2,
|
|
303
|
-
chat_code:
|
|
304
|
-
content:
|
|
483
|
+
chat_code: openId,
|
|
484
|
+
content: imageUrl
|
|
305
485
|
});
|
|
306
486
|
return [createResult(ResultCode.Ok, 'client.createDirectMessage', res)];
|
|
307
487
|
}
|
|
@@ -311,19 +491,6 @@ const main = () => {
|
|
|
311
491
|
return [createResult(ResultCode.Fail, 'client.createDirectMessage', error)];
|
|
312
492
|
}
|
|
313
493
|
};
|
|
314
|
-
/**
|
|
315
|
-
*
|
|
316
|
-
* @param user_id
|
|
317
|
-
* @param val
|
|
318
|
-
* @returns
|
|
319
|
-
*/
|
|
320
|
-
// const sendUserByUserId = async (user_id: string, val: DataEnums[]) => {
|
|
321
|
-
// if (val.length < 0) return []
|
|
322
|
-
// // 创建私聊标记
|
|
323
|
-
// const data = await client.userChatCreate(user_id).then(res => res?.data)
|
|
324
|
-
// const open_id = data?.code
|
|
325
|
-
// return await sendUser(open_id, val)
|
|
326
|
-
// }
|
|
327
494
|
const api = {
|
|
328
495
|
active: {
|
|
329
496
|
send: {
|
|
@@ -333,7 +500,7 @@ const main = () => {
|
|
|
333
500
|
},
|
|
334
501
|
use: {
|
|
335
502
|
send: async (event, val) => {
|
|
336
|
-
if (val.length
|
|
503
|
+
if (!val || val.length <= 0) {
|
|
337
504
|
return [];
|
|
338
505
|
}
|
|
339
506
|
if (event.name === 'message.create') {
|
|
@@ -347,8 +514,8 @@ const main = () => {
|
|
|
347
514
|
mention: e => {
|
|
348
515
|
const event = e.value;
|
|
349
516
|
const MessageMention = [];
|
|
350
|
-
const
|
|
351
|
-
for (const item of
|
|
517
|
+
const mentionRolePart = event.extra.kmarkdown?.mention_role_part ?? [];
|
|
518
|
+
for (const item of mentionRolePart) {
|
|
352
519
|
const UserId = item.role_id;
|
|
353
520
|
const [isMaster, UserKey] = getMaster(UserId);
|
|
354
521
|
MessageMention.push({
|
|
@@ -359,8 +526,8 @@ const main = () => {
|
|
|
359
526
|
IsBot: true
|
|
360
527
|
});
|
|
361
528
|
}
|
|
362
|
-
const
|
|
363
|
-
for (const item of
|
|
529
|
+
const mentionPart = event.extra.kmarkdown?.mention_part ?? [];
|
|
530
|
+
for (const item of mentionPart) {
|
|
364
531
|
const UserId = item.id;
|
|
365
532
|
const [isMaster, UserKey] = getMaster(UserId);
|
|
366
533
|
MessageMention.push({
|
|
@@ -378,82 +545,69 @@ const main = () => {
|
|
|
378
545
|
}
|
|
379
546
|
};
|
|
380
547
|
const onactions = async (data, consume) => {
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
548
|
+
try {
|
|
549
|
+
if (data.action === 'message.send') {
|
|
550
|
+
const event = data.payload.event;
|
|
551
|
+
const paramFormat = data.payload.params.format;
|
|
552
|
+
const res = await api.use.send(event, paramFormat);
|
|
553
|
+
if (!res) {
|
|
554
|
+
consume([createResult(ResultCode.Ok, '请求完成', null)]);
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
consume(res.map(item => createResult(ResultCode.Ok, '请求完成', item)));
|
|
388
558
|
}
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
559
|
+
else if (data.action === 'message.send.channel') {
|
|
560
|
+
const channelId = data.payload.ChannelId;
|
|
561
|
+
const val = data.payload.params.format;
|
|
562
|
+
const res = await api.active.send.channel(channelId, val);
|
|
563
|
+
if (!res) {
|
|
564
|
+
consume([createResult(ResultCode.Ok, '请求完成', null)]);
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
consume(res.map(item => createResult(ResultCode.Ok, '请求完成', item)));
|
|
398
568
|
}
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
569
|
+
else if (data.action === 'message.send.user') {
|
|
570
|
+
const userId = data.payload.UserId;
|
|
571
|
+
const val = data.payload.params.format;
|
|
572
|
+
const res = await api.active.send.user(userId, val);
|
|
573
|
+
if (!res) {
|
|
574
|
+
consume([createResult(ResultCode.Ok, '请求完成', null)]);
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
577
|
+
consume(res.map(item => createResult(ResultCode.Ok, '请求完成', item)));
|
|
578
|
+
}
|
|
579
|
+
else if (data.action === 'mention.get') {
|
|
580
|
+
const event = data.payload.event;
|
|
581
|
+
const res = await api.use.mention(event);
|
|
582
|
+
consume([createResult(ResultCode.Ok, '请求完成', res)]);
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
consume([createResult(ResultCode.Fail, '未知请求,请尝试升级版本', null)]);
|
|
408
586
|
}
|
|
409
|
-
consume(res.map(item => createResult(ResultCode.Ok, '请求完成', item)));
|
|
410
587
|
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
const res = await api.use.mention(event);
|
|
414
|
-
consume([createResult(ResultCode.Ok, '请求完成', res)]);
|
|
588
|
+
catch (error) {
|
|
589
|
+
consume([createResult(ResultCode.Fail, '请求失败', error)]);
|
|
415
590
|
}
|
|
416
591
|
};
|
|
417
592
|
cbp.onactions((data, consume) => void onactions(data, consume));
|
|
418
593
|
const onapis = async (data, consume) => {
|
|
419
594
|
const key = data.payload?.key;
|
|
420
595
|
if (client[key]) {
|
|
421
|
-
// 如果 client 上有对应的 key,直接调用。
|
|
422
596
|
const params = data.payload.params;
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
};
|
|
427
|
-
cbp.onapis((data, consume) => void onapis(data, consume));
|
|
428
|
-
};
|
|
429
|
-
const mainProcess = () => {
|
|
430
|
-
['SIGINT', 'SIGTERM', 'SIGQUIT', 'disconnect'].forEach(sig => {
|
|
431
|
-
process?.on?.(sig, () => {
|
|
432
|
-
logger?.info?.(`[@alemonjs/kook][${sig}] 收到信号,正在关闭...`);
|
|
433
|
-
setImmediate(() => process.exit(0));
|
|
434
|
-
});
|
|
435
|
-
});
|
|
436
|
-
process?.on?.('exit', code => {
|
|
437
|
-
logger?.info?.(`[@alemonjs/kook][exit] 进程退出,code=${code}`);
|
|
438
|
-
});
|
|
439
|
-
// 监听主进程消息
|
|
440
|
-
process.on('message', msg => {
|
|
441
|
-
try {
|
|
442
|
-
const data = typeof msg === 'string' ? JSON.parse(msg) : msg;
|
|
443
|
-
if (data?.type === 'start') {
|
|
444
|
-
main();
|
|
597
|
+
try {
|
|
598
|
+
const res = await client[key](...params);
|
|
599
|
+
consume([createResult(ResultCode.Ok, '请求完成', res)]);
|
|
445
600
|
}
|
|
446
|
-
|
|
447
|
-
|
|
601
|
+
catch (error) {
|
|
602
|
+
consume([createResult(ResultCode.Fail, '请求失败', error)]);
|
|
448
603
|
}
|
|
449
604
|
}
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
}
|
|
605
|
+
else {
|
|
606
|
+
consume([createResult(ResultCode.Fail, '未知请求,请尝试升级版本', null)]);
|
|
607
|
+
}
|
|
608
|
+
};
|
|
609
|
+
cbp.onapis((data, consume) => void onapis(data, consume));
|
|
456
610
|
};
|
|
457
|
-
|
|
611
|
+
var index = definePlatform({ main });
|
|
458
612
|
|
|
459
|
-
export {
|
|
613
|
+
export { index as default, platform };
|