@alemonjs/kook 0.2.6 → 0.2.9
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/README.md +3 -13
- package/dist/assets/index.css +1 -1
- package/dist/assets/index.js +10 -30
- package/lib/desktop.js +3 -1
- package/lib/index.d.ts +1 -1
- package/lib/index.js +191 -78
- package/package.json +4 -1
package/lib/desktop.js
CHANGED
|
@@ -13,12 +13,14 @@ const activate = context => {
|
|
|
13
13
|
context.onCommand('open.kook', () => {
|
|
14
14
|
const dir = join(__dirname, '../', 'dist', 'index.html');
|
|
15
15
|
const scriptReg = /<script.*?src="(.+?)".*?>/;
|
|
16
|
-
const styleReg = /<link.*?href="(.+?)".*?>/;
|
|
16
|
+
const styleReg = /<link.*?rel="stylesheet".*?href="(.+?)".*?>/;
|
|
17
|
+
const iconReg = /<link.*?rel="icon".*?href="(.+?)".*?>/g;
|
|
17
18
|
// 创建 webview 路径
|
|
18
19
|
const styleUri = context.createExtensionDir(join(__dirname, '../', 'dist', 'assets', 'index.css'));
|
|
19
20
|
const scriptUri = context.createExtensionDir(join(__dirname, '../', 'dist', 'assets', 'index.js'));
|
|
20
21
|
// 确保路径存在
|
|
21
22
|
const html = readFileSync(dir, 'utf-8')
|
|
23
|
+
.replace(iconReg, ``)
|
|
22
24
|
.replace(scriptReg, `<script type="module" crossorigin src="${scriptUri}"></script>`)
|
|
23
25
|
.replace(styleReg, `<link rel="stylesheet" crossorigin href="${styleUri}">`);
|
|
24
26
|
// 立即渲染 webview
|
package/lib/index.d.ts
CHANGED
|
@@ -4,6 +4,6 @@ import { KOOKClient } from './sdk/platform/kook/sdk/wss.js';
|
|
|
4
4
|
type Client = typeof KOOKClient.prototype;
|
|
5
5
|
declare const platform = "kook";
|
|
6
6
|
declare const client: Client;
|
|
7
|
-
declare const _default:
|
|
7
|
+
declare const _default: alemonjs.DefineBotValue;
|
|
8
8
|
|
|
9
9
|
export { type Client, client, _default as default, platform };
|
package/lib/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { defineBot, getConfigValue, useUserHashKey,
|
|
2
|
-
import 'fs';
|
|
1
|
+
import { defineBot, getConfigValue, useUserHashKey, onProcessor } from 'alemonjs';
|
|
2
|
+
import { readFileSync } from 'fs';
|
|
3
3
|
import 'axios';
|
|
4
4
|
import 'path';
|
|
5
5
|
import 'qrcode';
|
|
@@ -36,6 +36,8 @@ var index = defineBot(() => {
|
|
|
36
36
|
// 过滤机器人
|
|
37
37
|
if (event.extra?.author?.bot)
|
|
38
38
|
return false;
|
|
39
|
+
// 创建私聊标记
|
|
40
|
+
const data = await client.userChatCreate(event.extra.author.id).then(res => res?.data);
|
|
39
41
|
// 主人
|
|
40
42
|
const master_key = config?.master_key ?? [];
|
|
41
43
|
const isMaster = master_key.includes(event.author_id);
|
|
@@ -77,20 +79,14 @@ var index = defineBot(() => {
|
|
|
77
79
|
// message
|
|
78
80
|
MessageId: event.msg_id,
|
|
79
81
|
MessageText: msg,
|
|
80
|
-
OpenId:
|
|
82
|
+
OpenId: data?.code,
|
|
81
83
|
CreateAt: Date.now(),
|
|
82
84
|
//
|
|
83
85
|
tag: 'MESSAGES_PUBLIC',
|
|
84
86
|
value: null
|
|
85
87
|
};
|
|
86
|
-
// 当访问的时候获取
|
|
87
|
-
Object.defineProperty(e, 'value', {
|
|
88
|
-
get() {
|
|
89
|
-
return event;
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
88
|
// 处理消息
|
|
93
|
-
|
|
89
|
+
onProcessor('private.message.create', e, event);
|
|
94
90
|
});
|
|
95
91
|
// 监听消息
|
|
96
92
|
client.on('MESSAGES_PUBLIC', async (event) => {
|
|
@@ -164,14 +160,8 @@ var index = defineBot(() => {
|
|
|
164
160
|
tag: 'MESSAGES_PUBLIC',
|
|
165
161
|
value: null
|
|
166
162
|
};
|
|
167
|
-
// 当访问的时候获取
|
|
168
|
-
Object.defineProperty(e, 'value', {
|
|
169
|
-
get() {
|
|
170
|
-
return event;
|
|
171
|
-
}
|
|
172
|
-
});
|
|
173
163
|
// 处理消息
|
|
174
|
-
|
|
164
|
+
onProcessor('message.create', e, event);
|
|
175
165
|
});
|
|
176
166
|
// 发送错误时
|
|
177
167
|
client.on('ERROR', msg => {
|
|
@@ -179,74 +169,197 @@ var index = defineBot(() => {
|
|
|
179
169
|
});
|
|
180
170
|
// 客户端全局化。
|
|
181
171
|
global.client = client;
|
|
172
|
+
/**
|
|
173
|
+
*
|
|
174
|
+
* @param channel_id
|
|
175
|
+
* @param val
|
|
176
|
+
* @returns
|
|
177
|
+
*/
|
|
178
|
+
const sendChannel = (channel_id, val) => {
|
|
179
|
+
if (val.length < 0)
|
|
180
|
+
return Promise.all([]);
|
|
181
|
+
const content = val
|
|
182
|
+
.filter(item => item.type == 'Mention' || item.type == 'Text')
|
|
183
|
+
.map(item => {
|
|
184
|
+
// if (item.type == 'Link') {
|
|
185
|
+
// return `[${item.options?.title ?? item.value}](${item.value})`
|
|
186
|
+
// } else
|
|
187
|
+
if (item.type == 'Mention') {
|
|
188
|
+
if (item.value == 'everyone' ||
|
|
189
|
+
item.value == 'all' ||
|
|
190
|
+
item.value == '' ||
|
|
191
|
+
typeof item.value != 'string') {
|
|
192
|
+
return `(met)all(met)`;
|
|
193
|
+
}
|
|
194
|
+
if (item.options?.belong == 'user') {
|
|
195
|
+
return `(met)${item.value}(met)`;
|
|
196
|
+
}
|
|
197
|
+
else if (item.options?.belong == 'channel') {
|
|
198
|
+
return `(chn)${item.value}(chn)`;
|
|
199
|
+
}
|
|
200
|
+
return '';
|
|
201
|
+
}
|
|
202
|
+
else if (item.type == 'Text') {
|
|
203
|
+
if (item.options?.style == 'block') {
|
|
204
|
+
return `\`${item.value}\``;
|
|
205
|
+
}
|
|
206
|
+
else if (item.options?.style == 'italic') {
|
|
207
|
+
return `*${item.value}*`;
|
|
208
|
+
}
|
|
209
|
+
else if (item.options?.style == 'bold') {
|
|
210
|
+
return `**${item.value}**`;
|
|
211
|
+
}
|
|
212
|
+
else if (item.options?.style == 'strikethrough') {
|
|
213
|
+
return `~~${item.value}~~`;
|
|
214
|
+
}
|
|
215
|
+
else if (item.options?.style == 'boldItalic') {
|
|
216
|
+
return `***${item.value}***`;
|
|
217
|
+
}
|
|
218
|
+
return item.value;
|
|
219
|
+
}
|
|
220
|
+
})
|
|
221
|
+
.join('');
|
|
222
|
+
if (content) {
|
|
223
|
+
return Promise.all([content].map(item => client.createMessage({
|
|
224
|
+
type: 9,
|
|
225
|
+
target_id: channel_id,
|
|
226
|
+
content: item
|
|
227
|
+
})));
|
|
228
|
+
}
|
|
229
|
+
const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
|
|
230
|
+
if (images) {
|
|
231
|
+
return Promise.all(images.map(async (item) => {
|
|
232
|
+
if (item.type == 'ImageURL') {
|
|
233
|
+
return await client.createMessage({
|
|
234
|
+
type: 2,
|
|
235
|
+
target_id: channel_id,
|
|
236
|
+
content: item.value
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
let data = item.value;
|
|
240
|
+
if (item.type == 'ImageFile') {
|
|
241
|
+
data = readFileSync(item.value);
|
|
242
|
+
}
|
|
243
|
+
// 上传图片
|
|
244
|
+
const res = await client.postImage(data);
|
|
245
|
+
if (!res)
|
|
246
|
+
return Promise.resolve();
|
|
247
|
+
// 发送消息
|
|
248
|
+
return await client.createMessage({
|
|
249
|
+
type: 2,
|
|
250
|
+
target_id: channel_id,
|
|
251
|
+
content: res.data.url
|
|
252
|
+
});
|
|
253
|
+
}));
|
|
254
|
+
}
|
|
255
|
+
return Promise.all([]);
|
|
256
|
+
};
|
|
257
|
+
/**
|
|
258
|
+
*
|
|
259
|
+
* @param channel_id
|
|
260
|
+
* @param val
|
|
261
|
+
* @returns
|
|
262
|
+
*/
|
|
263
|
+
const sendUser = async (user_id, val) => {
|
|
264
|
+
if (val.length < 0)
|
|
265
|
+
return Promise.all([]);
|
|
266
|
+
// 创建私聊标记
|
|
267
|
+
const data = await client.userChatCreate(user_id).then(res => res?.data);
|
|
268
|
+
const open_id = data?.code;
|
|
269
|
+
const content = val
|
|
270
|
+
.filter(item => item.type == 'Mention' || item.type == 'Text')
|
|
271
|
+
.map(item => {
|
|
272
|
+
// if (item.type == 'Link') {
|
|
273
|
+
// return `[${item.options?.title ?? item.value}](${item.value})`
|
|
274
|
+
// } else
|
|
275
|
+
if (item.type == 'Mention') {
|
|
276
|
+
if (item.value == 'everyone' ||
|
|
277
|
+
item.value == 'all' ||
|
|
278
|
+
item.value == '' ||
|
|
279
|
+
typeof item.value != 'string') {
|
|
280
|
+
return `(met)all(met)`;
|
|
281
|
+
}
|
|
282
|
+
if (item.options?.belong == 'user') {
|
|
283
|
+
return `(met)${item.value}(met)`;
|
|
284
|
+
}
|
|
285
|
+
else if (item.options?.belong == 'channel') {
|
|
286
|
+
return `(chn)${item.value}(chn)`;
|
|
287
|
+
}
|
|
288
|
+
return '';
|
|
289
|
+
}
|
|
290
|
+
else if (item.type == 'Text') {
|
|
291
|
+
if (item.options?.style == 'block') {
|
|
292
|
+
return `\`${item.value}\``;
|
|
293
|
+
}
|
|
294
|
+
else if (item.options?.style == 'italic') {
|
|
295
|
+
return `*${item.value}*`;
|
|
296
|
+
}
|
|
297
|
+
else if (item.options?.style == 'bold') {
|
|
298
|
+
return `**${item.value}**`;
|
|
299
|
+
}
|
|
300
|
+
else if (item.options?.style == 'strikethrough') {
|
|
301
|
+
return `~~${item.value}~~`;
|
|
302
|
+
}
|
|
303
|
+
else if (item.options?.style == 'boldItalic') {
|
|
304
|
+
return `***${item.value}***`;
|
|
305
|
+
}
|
|
306
|
+
return item.value;
|
|
307
|
+
}
|
|
308
|
+
})
|
|
309
|
+
.join('');
|
|
310
|
+
if (content) {
|
|
311
|
+
return Promise.all([content].map(item => client.createDirectMessage({
|
|
312
|
+
type: 9,
|
|
313
|
+
chat_code: open_id,
|
|
314
|
+
content: item
|
|
315
|
+
})));
|
|
316
|
+
}
|
|
317
|
+
const images = val.filter(item => item.type == 'Image' || item.type == 'ImageFile' || item.type == 'ImageURL');
|
|
318
|
+
if (images) {
|
|
319
|
+
return Promise.all(images.map(async (item) => {
|
|
320
|
+
if (item.type == 'ImageURL') {
|
|
321
|
+
return await client.createDirectMessage({
|
|
322
|
+
type: 2,
|
|
323
|
+
chat_code: open_id,
|
|
324
|
+
content: item.value
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
let data = item.value;
|
|
328
|
+
if (item.type == 'ImageFile') {
|
|
329
|
+
data = readFileSync(item.value);
|
|
330
|
+
}
|
|
331
|
+
// 上传图片
|
|
332
|
+
const res = await client.postImage(data);
|
|
333
|
+
if (!res)
|
|
334
|
+
return Promise.resolve();
|
|
335
|
+
// 发送消息
|
|
336
|
+
return await client.createDirectMessage({
|
|
337
|
+
type: 2,
|
|
338
|
+
chat_code: open_id,
|
|
339
|
+
content: res.data.url
|
|
340
|
+
});
|
|
341
|
+
}));
|
|
342
|
+
}
|
|
343
|
+
return Promise.all([]);
|
|
344
|
+
};
|
|
182
345
|
return {
|
|
346
|
+
platform,
|
|
183
347
|
api: {
|
|
348
|
+
active: {
|
|
349
|
+
send: {
|
|
350
|
+
channel: sendChannel,
|
|
351
|
+
user: sendUser
|
|
352
|
+
}
|
|
353
|
+
},
|
|
184
354
|
use: {
|
|
185
355
|
send: (event, val) => {
|
|
186
356
|
if (val.length < 0)
|
|
187
357
|
return Promise.all([]);
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
.map(item => {
|
|
191
|
-
if (item.type == 'Link') {
|
|
192
|
-
return `[${item.options?.title ?? item.value}](${item.value})`;
|
|
193
|
-
}
|
|
194
|
-
else if (item.type == 'Mention') {
|
|
195
|
-
if (item.value == 'everyone' ||
|
|
196
|
-
item.value == 'all' ||
|
|
197
|
-
item.value == '' ||
|
|
198
|
-
typeof item.value != 'string') {
|
|
199
|
-
return `(met)all(met)`;
|
|
200
|
-
}
|
|
201
|
-
if (item.options?.belong == 'user') {
|
|
202
|
-
return `(met)${item.value}(met)`;
|
|
203
|
-
}
|
|
204
|
-
else if (item.options?.belong == 'channel') {
|
|
205
|
-
return `(chn)${item.value}(chn)`;
|
|
206
|
-
}
|
|
207
|
-
return '';
|
|
208
|
-
}
|
|
209
|
-
else if (item.type == 'Text') {
|
|
210
|
-
if (item.options?.style == 'block') {
|
|
211
|
-
return `\`${item.value}\``;
|
|
212
|
-
}
|
|
213
|
-
else if (item.options?.style == 'italic') {
|
|
214
|
-
return `*${item.value}*`;
|
|
215
|
-
}
|
|
216
|
-
else if (item.options?.style == 'bold') {
|
|
217
|
-
return `**${item.value}**`;
|
|
218
|
-
}
|
|
219
|
-
else if (item.options?.style == 'strikethrough') {
|
|
220
|
-
return `~~${item.value}~~`;
|
|
221
|
-
}
|
|
222
|
-
else if (item.options?.style == 'boldItalic') {
|
|
223
|
-
return `***${item.value}***`;
|
|
224
|
-
}
|
|
225
|
-
return item.value;
|
|
226
|
-
}
|
|
227
|
-
})
|
|
228
|
-
.join('');
|
|
229
|
-
if (content) {
|
|
230
|
-
return Promise.all([content].map(item => client.createMessage({
|
|
231
|
-
type: 9,
|
|
232
|
-
target_id: event.ChannelId,
|
|
233
|
-
content: item
|
|
234
|
-
})));
|
|
358
|
+
if (event.name == 'message.create') {
|
|
359
|
+
return sendChannel(event.ChannelId, val);
|
|
235
360
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
return Promise.all(images.map(async (item) => {
|
|
239
|
-
// 上传图片
|
|
240
|
-
const res = await client.postImage(item);
|
|
241
|
-
if (!res)
|
|
242
|
-
return Promise.resolve();
|
|
243
|
-
// 发送消息
|
|
244
|
-
return await client.createMessage({
|
|
245
|
-
type: 2,
|
|
246
|
-
target_id: event.ChannelId,
|
|
247
|
-
content: res.data.url
|
|
248
|
-
});
|
|
249
|
-
}));
|
|
361
|
+
else if (event.name == 'private.message.create') {
|
|
362
|
+
return sendUser(event.UserId, val);
|
|
250
363
|
}
|
|
251
364
|
return Promise.all([]);
|
|
252
365
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alemonjs/kook",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.9",
|
|
4
4
|
"description": "阿柠檬kook平台连接",
|
|
5
5
|
"author": "lemonade",
|
|
6
6
|
"license": "MIT",
|
|
@@ -26,15 +26,18 @@
|
|
|
26
26
|
"value": "@alemonjs/kook"
|
|
27
27
|
}
|
|
28
28
|
],
|
|
29
|
+
"logo": "antd.RobotOutlined",
|
|
29
30
|
"commond": [
|
|
30
31
|
{
|
|
31
32
|
"name": "kook",
|
|
33
|
+
"icon": "antd.RobotOutlined",
|
|
32
34
|
"commond": "open.kook"
|
|
33
35
|
}
|
|
34
36
|
],
|
|
35
37
|
"sidebars": [
|
|
36
38
|
{
|
|
37
39
|
"name": "kook",
|
|
40
|
+
"icon": "antd.RobotOutlined",
|
|
38
41
|
"commond": "open.kook"
|
|
39
42
|
}
|
|
40
43
|
]
|