@alemonjs/kook 2.1.0 → 2.1.2

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 CHANGED
@@ -24,4 +24,9 @@ kook:
24
24
  # 使用 user_id
25
25
  master_id:
26
26
  - 'yyy'
27
+ # 隐藏不支持的消息类型(可选,默认: false)
28
+ # 开启后,转为文本时不可读内容(如 [视频]、[音频]、[图片]、[附件] 等占位符)将被置空
29
+ # 可读内容(如标题、按钮文本、链接等)仍会保留为纯文本
30
+ # 转换后内容为空时,将跳过发送并输出 info 日志
31
+ hideUnsupported: true
27
32
  ```
package/lib/config.d.ts CHANGED
@@ -3,6 +3,7 @@ export type Options = {
3
3
  token: string;
4
4
  master_key?: string[];
5
5
  master_id?: string[];
6
+ hideUnsupported?: boolean;
6
7
  };
7
8
  export declare const getKOOKConfig: () => Options;
8
9
  export declare const getMaster: (UserId: string) => readonly [boolean, string];
@@ -0,0 +1,4 @@
1
+ import type { DataEnums, DataMarkDown } from 'alemonjs';
2
+ export declare const markdownToKMarkdown: (items: DataMarkDown["value"], hideUnsupported?: boolean) => string;
3
+ export declare const markdownRawToKMarkdown: (raw: string, hideUnsupported?: boolean) => string;
4
+ export declare const dataEnumToKMarkdown: (item: DataEnums, hideUnsupported?: boolean) => string;
package/lib/format.js ADDED
@@ -0,0 +1,83 @@
1
+ const markdownToKMarkdown = (items, hideUnsupported) => {
2
+ return items
3
+ .map(item => {
4
+ switch (item.type) {
5
+ case 'MD.text':
6
+ return item.value;
7
+ case 'MD.title':
8
+ return `**${item.value}**\n`;
9
+ case 'MD.subtitle':
10
+ return `**${item.value}**\n`;
11
+ case 'MD.bold':
12
+ return `**${item.value}**`;
13
+ case 'MD.italic':
14
+ case 'MD.italicStar':
15
+ return `*${item.value}*`;
16
+ case 'MD.strikethrough':
17
+ return `~~${item.value}~~`;
18
+ case 'MD.link': {
19
+ const v = item.value;
20
+ return `[${v.text}](${v.url})`;
21
+ }
22
+ case 'MD.image':
23
+ return hideUnsupported ? '' : '[图片]';
24
+ case 'MD.list':
25
+ return (item.value
26
+ .map(li => {
27
+ if (typeof li.value === 'object') {
28
+ return `${li.value.index}. ${li.value.text ?? ''}`;
29
+ }
30
+ return `- ${li.value}`;
31
+ })
32
+ .join('\n') + '\n');
33
+ case 'MD.blockquote':
34
+ return `> ${item.value}\n`;
35
+ case 'MD.divider':
36
+ return hideUnsupported ? '' : '---\n';
37
+ case 'MD.newline':
38
+ return '\n';
39
+ case 'MD.code': {
40
+ const lang = item?.options?.language || '';
41
+ return `\`\`\`${lang}\n${item.value}\n\`\`\`\n`;
42
+ }
43
+ case 'MD.mention':
44
+ if (item.value === 'everyone') {
45
+ return '(met)all(met)';
46
+ }
47
+ return `(met)${item.value ?? ''}(met)`;
48
+ case 'MD.content':
49
+ return item.value;
50
+ case 'MD.button':
51
+ return hideUnsupported ? String(item.value) : `[${item.value}]`;
52
+ default:
53
+ return String(item?.value ?? '');
54
+ }
55
+ })
56
+ .join('');
57
+ };
58
+ const markdownRawToKMarkdown = (raw, hideUnsupported) => {
59
+ let text = raw;
60
+ text = text.replace(/!\[([^\]]*)\]\([^)]*\)/g, hideUnsupported ? '' : '[图片]');
61
+ return text;
62
+ };
63
+ const dataEnumToKMarkdown = (item, hideUnsupported) => {
64
+ switch (item.type) {
65
+ case 'Markdown':
66
+ return markdownToKMarkdown(item.value, hideUnsupported);
67
+ case 'MarkdownOriginal':
68
+ return markdownRawToKMarkdown(String(item.value), hideUnsupported);
69
+ case 'BT.group':
70
+ case 'ButtonGroup':
71
+ return hideUnsupported ? '' : item.value.map((row) => row.value.map((btn) => `[${btn.value}]`).join(' ')).join('\n');
72
+ case 'Attachment':
73
+ return hideUnsupported ? '' : `[附件${item.options?.filename ? ': ' + item.options.filename : ''}]`;
74
+ case 'Audio':
75
+ return hideUnsupported ? '' : '[音频]';
76
+ case 'Video':
77
+ return hideUnsupported ? '' : '[视频]';
78
+ default:
79
+ return '';
80
+ }
81
+ };
82
+
83
+ export { dataEnumToKMarkdown, markdownRawToKMarkdown, markdownToKMarkdown };
package/lib/index.js CHANGED
@@ -4,6 +4,7 @@ import { KOOKClient } from './sdk/wss.js';
4
4
  import { readFileSync } from 'fs';
5
5
  import { getKOOKConfig, getMaster, platform } from './config.js';
6
6
  import { getBufferByURL } from 'alemonjs/utils';
7
+ import { dataEnumToKMarkdown } from './format.js';
7
8
  export { useClient, useValue } from './hook.js';
8
9
 
9
10
  const main = () => {
@@ -13,9 +14,12 @@ const main = () => {
13
14
  });
14
15
  void client.connect();
15
16
  let botId = '';
16
- client.userMe().then(res => {
17
+ client
18
+ .userMe()
19
+ .then(res => {
17
20
  botId = String(res?.data?.id ?? '');
18
- }).catch(() => { });
21
+ })
22
+ .catch(() => { });
19
23
  const port = process.env?.port || 17117;
20
24
  const url = `ws://127.0.0.1:${port}`;
21
25
  const cbp = cbpPlatform(url);
@@ -324,8 +328,14 @@ const main = () => {
324
328
  cbp.send(e);
325
329
  });
326
330
  const formatKookContent = (val) => {
327
- return val
328
- .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
331
+ const nativeItems = val.filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link');
332
+ const unsupportedItems = val.filter(item => item.type !== 'Mention'
333
+ && item.type !== 'Text'
334
+ && item.type !== 'Link'
335
+ && item.type !== 'Image'
336
+ && item.type !== 'ImageURL'
337
+ && item.type !== 'ImageFile');
338
+ const nativeText = nativeItems
329
339
  .map(item => {
330
340
  if (item.type === 'Link') {
331
341
  return `[${item.value}](${item?.options?.link ?? item.value})`;
@@ -363,6 +373,14 @@ const main = () => {
363
373
  return '';
364
374
  })
365
375
  .join('');
376
+ const hide = getKOOKConfig().hideUnsupported === true;
377
+ const fallbackText = unsupportedItems.length > 0
378
+ ? unsupportedItems
379
+ .map(item => dataEnumToKMarkdown(item, hide))
380
+ .filter(Boolean)
381
+ .join('')
382
+ : '';
383
+ return [nativeText, fallbackText].filter(Boolean).join('');
366
384
  };
367
385
  const resolveImageBuffer = async (val) => {
368
386
  const images = val.filter(item => item.type === 'Image' || item.type === 'ImageFile' || item.type === 'ImageURL');
@@ -414,6 +432,10 @@ const main = () => {
414
432
  const content = formatKookContent(val);
415
433
  try {
416
434
  const imageUrl = await uploadAndGetImageUrl(val);
435
+ if (getKOOKConfig().hideUnsupported === true && !content && !imageUrl) {
436
+ logger.info('[kook] hideUnsupported: 消息内容转换后为空,跳过发送');
437
+ return [];
438
+ }
417
439
  if (imageUrl && content) {
418
440
  const imgRes = await client.createMessage({
419
441
  type: 2,
@@ -456,6 +478,10 @@ const main = () => {
456
478
  const content = formatKookContent(val);
457
479
  try {
458
480
  const imageUrl = await uploadAndGetImageUrl(val);
481
+ if (getKOOKConfig().hideUnsupported === true && !content && !imageUrl) {
482
+ logger.info('[kook] hideUnsupported: 消息内容转换后为空,跳过发送');
483
+ return [];
484
+ }
459
485
  if (imageUrl && content) {
460
486
  const imgRes = await client.createDirectMessage({
461
487
  type: 2,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/kook",
3
- "version": "2.1.0",
3
+ "version": "2.1.2",
4
4
  "description": "kook platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",