@alemonjs/discord 2.1.2 → 2.1.4

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
@@ -38,10 +38,12 @@ discord:
38
38
  websocket_proxy: 'http://localhost:7890'
39
39
  # request_proxy: 'http://localhost:7890'
40
40
  # 隐藏不支持的消息类型(可选,默认: false)
41
- # 开启后,转为文本时不可读内容(如 [视频]、[音频]、[图片]、[附件] 等占位符)将被置空
42
- # 可读内容(如标题、按钮文本、链接等)仍会保留为纯文本
41
+ # 1:一级隐藏,不可读占位符([视频]、[音频]、[图片]、[附件]等)被置空,可读内容保留
42
+ # 2:二级隐藏,按钮仅显示指令数据(如 /挑战),链接仅显示 URL
43
+ # 3:三级隐藏,按钮和链接的 data 也不保留,完全隐藏
44
+ # 4:四级隐藏,不进行任何转换,降级数据直接丢弃
43
45
  # 转换后内容为空时,将跳过发送并输出 info 日志
44
- hideUnsupported: true
46
+ hideUnsupported: 1
45
47
 
46
48
  ```
47
49
 
package/lib/config.d.ts CHANGED
@@ -6,7 +6,7 @@ export interface Options {
6
6
  shard?: number[];
7
7
  master_key?: string[];
8
8
  master_id?: string[];
9
- hideUnsupported?: boolean;
9
+ hideUnsupported?: boolean | number;
10
10
  }
11
11
  export declare const platform = "discord";
12
12
  export declare const getDiscordConfig: () => Options & {
package/lib/format.d.ts CHANGED
@@ -1,4 +1,4 @@
1
1
  import type { DataEnums, DataMarkDown } from 'alemonjs';
2
- export declare const markdownToDiscordText: (items: DataMarkDown["value"]) => string;
3
- export declare const markdownRawToDiscordText: (raw: string, hideUnsupported?: boolean) => string;
4
- export declare const dataEnumToDiscordText: (item: DataEnums, hideUnsupported?: boolean) => string;
2
+ export declare const markdownToDiscordText: (items: DataMarkDown["value"], hideUnsupported?: boolean | number) => string;
3
+ export declare const markdownRawToDiscordText: (raw: string, hideUnsupported?: boolean | number) => string;
4
+ export declare const dataEnumToDiscordText: (item: DataEnums, hideUnsupported?: boolean | number) => string;
package/lib/format.js CHANGED
@@ -1,4 +1,6 @@
1
- const markdownToDiscordText = (items) => {
1
+ const markdownToDiscordText = (items, hideUnsupported) => {
2
+ if (Number(hideUnsupported) >= 4)
3
+ return '';
2
4
  return items
3
5
  .map(item => {
4
6
  switch (item.type) {
@@ -17,7 +19,9 @@ const markdownToDiscordText = (items) => {
17
19
  return `~~${item.value}~~`;
18
20
  case 'MD.link': {
19
21
  const v = item.value;
20
- return `[${v.text}](${v.url})`;
22
+ if (Number(hideUnsupported) >= 3)
23
+ return '';
24
+ return Number(hideUnsupported) >= 2 ? v.url : `[${v.text}](${v.url})`;
21
25
  }
22
26
  case 'MD.image':
23
27
  return `![image](${item.value})`;
@@ -48,6 +52,11 @@ const markdownToDiscordText = (items) => {
48
52
  case 'MD.content':
49
53
  return item.value;
50
54
  case 'MD.button':
55
+ if (Number(hideUnsupported) >= 3)
56
+ return '';
57
+ if (Number(hideUnsupported) >= 2) {
58
+ return item.options?.data || String(item.value);
59
+ }
51
60
  return `[${item.value}]`;
52
61
  default:
53
62
  return String(item?.value ?? '');
@@ -56,11 +65,15 @@ const markdownToDiscordText = (items) => {
56
65
  .join('');
57
66
  };
58
67
  const markdownRawToDiscordText = (raw, hideUnsupported) => {
68
+ if (Number(hideUnsupported) >= 4)
69
+ return '';
59
70
  let text = raw;
60
71
  text = text.replace(/!\[([^\]]*)\]\(([^)]*)\)/g, hideUnsupported ? '' : '[$1]($2)');
61
72
  return text;
62
73
  };
63
74
  const dataEnumToDiscordText = (item, hideUnsupported) => {
75
+ if (Number(hideUnsupported) >= 4)
76
+ return '';
64
77
  switch (item.type) {
65
78
  case 'MarkdownOriginal':
66
79
  return markdownRawToDiscordText(String(item.value), hideUnsupported);
package/lib/send.js CHANGED
@@ -102,7 +102,7 @@ const sendchannel = async (client, param, val) => {
102
102
  const mds = val.filter(item => item.type === 'Markdown');
103
103
  const nativeTypes = new Set(['Image', 'ImageURL', 'ImageFile', 'BT.group', 'Markdown', 'Mention', 'Text', 'Link']);
104
104
  const unsupportedItems = val.filter(item => !nativeTypes.has(item.type));
105
- const hide = getDiscordConfig().hideUnsupported === true;
105
+ const hide = getDiscordConfig().hideUnsupported;
106
106
  const fallbackText = unsupportedItems
107
107
  .map(item => dataEnumToDiscordText(item, hide))
108
108
  .filter(Boolean)
@@ -143,7 +143,7 @@ const sendchannel = async (client, param, val) => {
143
143
  })
144
144
  .join('');
145
145
  const contentMd = buildDiscordMdContent(mds);
146
- const finalContent = [content, contentMd, fallbackText].filter(Boolean).join('\n');
146
+ const finalContent = [content, contentMd, fallbackText].filter(Boolean).join('\n').replace(/^[^\S\n\r]+|[^\S\n\r]+$/g, '');
147
147
  if (hide && !finalContent && images.length <= 0 && buttons.length <= 0) {
148
148
  logger.info('[discord] hideUnsupported: 消息内容转换后为空,跳过发送');
149
149
  return [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "2.1.2",
3
+ "version": "2.1.4",
4
4
  "description": "discord platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",