@alemonjs/qq-bot 2.1.1 → 2.1.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.
package/README.md CHANGED
@@ -46,4 +46,11 @@ qq-bot:
46
46
  # base_url_app_access_token: https://[your addr]
47
47
  # 启动md强制转换为text,特别是当没有md权限,但使用了md数据格式时
48
48
  markdownToText: true
49
+ # 隐藏不支持的消息类型(可选,默认: false)
50
+ # 1:一级隐藏,不可读占位符([视频]、[音频]、[图片]、[附件]等)被置空,可读内容保留
51
+ # 2:二级隐藏,按钮仅显示指令数据(如 /挑战),链接仅显示 URL
52
+ # 3:三级隐藏,按钮和链接的 data 也不保留,完全隐藏
53
+ # 4:四级隐藏,不进行任何转换,降级数据直接丢弃
54
+ # 转换后内容为空时,将跳过发送并输出 info 日志
55
+ hideUnsupported: 1
49
56
  ```
package/lib/config.d.ts CHANGED
@@ -4,6 +4,7 @@ export type Options = {
4
4
  master_key?: string[];
5
5
  master_id?: string[];
6
6
  markdownToText?: boolean;
7
+ hideUnsupported?: boolean | number;
7
8
  } & sdkOptions;
8
9
  export declare const getQQBotConfig: () => Options;
9
10
  export declare const getMaster: (UserId: string) => readonly [boolean, string];
package/lib/format.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { DataEnums, DataMarkDown } from 'alemonjs';
2
- export declare const markdownToText: (items: DataMarkDown["value"]) => string;
2
+ export declare const markdownToText: (items: DataMarkDown["value"], hideUnsupported?: boolean | number) => string;
3
3
  export declare const buttonsToText: (rows: any[]) => string;
4
- export declare const markdownRawToText: (raw: string) => string;
5
- export declare const dataEnumToText: (item: DataEnums) => string;
4
+ export declare const markdownRawToText: (raw: string, hideUnsupported?: boolean | number) => string;
5
+ export declare const dataEnumToText: (item: DataEnums, hideUnsupported?: boolean | number) => string;
package/lib/format.js CHANGED
@@ -1,13 +1,15 @@
1
- const markdownToText = (items) => {
1
+ const markdownToText = (items, hideUnsupported) => {
2
+ if (Number(hideUnsupported) >= 4)
3
+ return '';
2
4
  return items
3
5
  .map(item => {
4
6
  switch (item.type) {
5
7
  case 'MD.text':
6
8
  return item.value;
7
9
  case 'MD.title':
8
- return `【${item.value}】\n`;
10
+ return hideUnsupported ? `${item.value}\n` : `【${item.value}】\n`;
9
11
  case 'MD.subtitle':
10
- return `〖${item.value}〗\n`;
12
+ return hideUnsupported ? `${item.value}\n` : `〖${item.value}〗\n`;
11
13
  case 'MD.bold':
12
14
  case 'MD.italic':
13
15
  case 'MD.italicStar':
@@ -15,10 +17,12 @@ const markdownToText = (items) => {
15
17
  return item.value;
16
18
  case 'MD.link': {
17
19
  const v = item.value;
18
- return `${v.text}( ${v.url} )`;
20
+ if (Number(hideUnsupported) >= 3)
21
+ return '';
22
+ return Number(hideUnsupported) >= 2 ? v.url : `${v.text}( ${v.url} )`;
19
23
  }
20
24
  case 'MD.image':
21
- return '[图片]';
25
+ return hideUnsupported ? '' : '[图片]';
22
26
  case 'MD.list':
23
27
  return (item.value
24
28
  .map(li => {
@@ -31,7 +35,7 @@ const markdownToText = (items) => {
31
35
  case 'MD.blockquote':
32
36
  return `> ${item.value}\n`;
33
37
  case 'MD.divider':
34
- return '————————\n';
38
+ return hideUnsupported ? '' : '————————\n';
35
39
  case 'MD.newline':
36
40
  return '\n';
37
41
  case 'MD.code':
@@ -44,22 +48,28 @@ const markdownToText = (items) => {
44
48
  case 'MD.content':
45
49
  return item.value;
46
50
  case 'MD.button':
47
- return `[${item.value}]`;
51
+ if (Number(hideUnsupported) >= 3)
52
+ return '';
53
+ if (Number(hideUnsupported) >= 2) {
54
+ return item.options?.data || String(item.value);
55
+ }
56
+ return hideUnsupported ? String(item.value) : `[${item.value}]`;
48
57
  default:
49
58
  return String(item?.value ?? '');
50
59
  }
51
60
  })
52
- .join('');
61
+ .join('')
62
+ .trim();
53
63
  };
54
64
  const buttonsToText = (rows) => {
55
- return rows
56
- .map((row) => row.value.map((btn) => `[${btn.value}]`).join(' '))
57
- .join('\n');
65
+ return rows.map((row) => row.value.map((btn) => `[${btn.value}]`).join(' ')).join('\n');
58
66
  };
59
- const markdownRawToText = (raw) => {
67
+ const markdownRawToText = (raw, hideUnsupported) => {
68
+ if (Number(hideUnsupported) >= 4)
69
+ return '';
60
70
  let text = raw;
61
- text = text.replace(/!\[([^\]]*)\]\([^)]*\)/g, '[图片]');
62
- text = text.replace(/\[([^\]]*)\]\([^)]*\)/g, '$1');
71
+ text = text.replace(/!\[([^\]]*)\]\([^)]*\)/g, hideUnsupported ? '' : '[图片]');
72
+ text = text.replace(/\[([^\]]*)\]\(([^)]*)\)/g, Number(hideUnsupported) >= 2 ? '$2' : '$1');
63
73
  text = text.replace(/^#{1,6}\s+/gm, '');
64
74
  text = text.replace(/(\*{3}|_{3})([^*_]+)\1/g, '$2');
65
75
  text = text.replace(/(\*{2}|_{2})([^*_]+)\1/g, '$2');
@@ -71,21 +81,23 @@ const markdownRawToText = (raw) => {
71
81
  return match.replace(/```\w*\n?/g, '').trim();
72
82
  });
73
83
  text = text.replace(/^>\s+/gm, '');
74
- text = text.replace(/^[-*_]{3,}\s*$/gm, '————————');
84
+ text = text.replace(/^[-*_]{3,}\s*$/gm, hideUnsupported ? '' : '————————');
75
85
  text = text.replace(/^[\s]*[-*+]\s+/gm, '· ');
76
86
  text = text.replace(/^[\s]*(\d+)\.\s+/gm, '$1. ');
77
87
  return text.trim();
78
88
  };
79
- const dataEnumToText = (item) => {
89
+ const dataEnumToText = (item, hideUnsupported) => {
90
+ if (Number(hideUnsupported) >= 4)
91
+ return '';
80
92
  switch (item.type) {
81
93
  case 'MarkdownOriginal':
82
- return markdownRawToText(String(item.value));
94
+ return markdownRawToText(String(item.value), hideUnsupported);
83
95
  case 'Attachment':
84
- return `[附件${item.options?.filename ? ': ' + item.options.filename : ''}]`;
96
+ return hideUnsupported ? '' : `[附件${item.options?.filename ? ': ' + item.options.filename : ''}]`;
85
97
  case 'Audio':
86
- return '[音频]';
98
+ return hideUnsupported ? '' : '[音频]';
87
99
  case 'Video':
88
- return '[视频]';
100
+ return hideUnsupported ? '' : '[视频]';
89
101
  default:
90
102
  return '';
91
103
  }
package/lib/sends.js CHANGED
@@ -165,10 +165,18 @@ const formatMention = (item, mode) => {
165
165
  };
166
166
  const extractContent = (val, mode) => {
167
167
  const nativeTypes = new Set([
168
- 'Mention', 'Text', 'Link',
169
- 'Image', 'ImageFile', 'ImageURL',
170
- 'Markdown', 'BT.group', 'ButtonTemplate',
171
- 'Ark.list', 'Ark.Card', 'Ark.BigCard'
168
+ 'Mention',
169
+ 'Text',
170
+ 'Link',
171
+ 'Image',
172
+ 'ImageFile',
173
+ 'ImageURL',
174
+ 'Markdown',
175
+ 'BT.group',
176
+ 'ButtonTemplate',
177
+ 'Ark.list',
178
+ 'Ark.Card',
179
+ 'Ark.BigCard'
172
180
  ]);
173
181
  const nativeText = val
174
182
  .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
@@ -185,9 +193,11 @@ const extractContent = (val, mode) => {
185
193
  return '';
186
194
  })
187
195
  .join('');
196
+ const config = getQQBotConfig();
197
+ const hide = config.hideUnsupported;
188
198
  const fallbackText = val
189
199
  .filter(item => !nativeTypes.has(item.type))
190
- .map(item => dataEnumToText(item))
200
+ .map(item => dataEnumToText(item, hide))
191
201
  .filter(Boolean)
192
202
  .join('\n');
193
203
  return [nativeText, fallbackText].filter(Boolean).join('\n');
@@ -336,6 +346,10 @@ const sendOpenApiMessage = async (content, val, baseParams, uploadMedia, sendMes
336
346
  });
337
347
  return [createResult(ResultCode.Ok, label, { id: res.id })];
338
348
  }
349
+ if (config.hideUnsupported === true && !content && !buildMdAndButtonsParams(val) && !buildArkParams(val)) {
350
+ logger.info('[qq-bot] hideUnsupported: 消息内容转换后为空,跳过发送');
351
+ return [];
352
+ }
339
353
  if (mdToText) {
340
354
  const textContent = flattenMdToText(content, val);
341
355
  if (textContent) {
@@ -399,6 +413,10 @@ const sendGuildMessage = async (content, val, baseParams, sendMessage, label) =>
399
413
  const res = await sendMessage({ content: imgContent, ...baseParams }, imageBuffer);
400
414
  return [createResult(ResultCode.Ok, label, { id: res?.id })];
401
415
  }
416
+ if (config.hideUnsupported && !content && !buildMdAndButtonsParams(val) && !buildArkParams(val)) {
417
+ logger.info('[qq-bot] hideUnsupported: 消息内容转换后为空,跳过发送');
418
+ return [];
419
+ }
402
420
  if (mdToText) {
403
421
  const textContent = flattenMdToText(content, val);
404
422
  if (textContent) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/qq-bot",
3
- "version": "2.1.1",
3
+ "version": "2.1.3",
4
4
  "description": "阿柠檬qq-bot平台连接",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",