@alemonjs/discord 2.1.1 → 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
@@ -37,6 +37,11 @@ discord:
37
37
  # ws 代理
38
38
  websocket_proxy: 'http://localhost:7890'
39
39
  # request_proxy: 'http://localhost:7890'
40
+ # 隐藏不支持的消息类型(可选,默认: false)
41
+ # 开启后,转为文本时不可读内容(如 [视频]、[音频]、[图片]、[附件] 等占位符)将被置空
42
+ # 可读内容(如标题、按钮文本、链接等)仍会保留为纯文本
43
+ # 转换后内容为空时,将跳过发送并输出 info 日志
44
+ hideUnsupported: true
40
45
 
41
46
  ```
42
47
 
package/lib/config.d.ts CHANGED
@@ -6,6 +6,7 @@ export interface Options {
6
6
  shard?: number[];
7
7
  master_key?: string[];
8
8
  master_id?: string[];
9
+ hideUnsupported?: boolean;
9
10
  }
10
11
  export declare const platform = "discord";
11
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
2
  export declare const markdownToDiscordText: (items: DataMarkDown["value"]) => string;
3
- export declare const markdownRawToDiscordText: (raw: string) => string;
4
- export declare const dataEnumToDiscordText: (item: DataEnums) => string;
3
+ export declare const markdownRawToDiscordText: (raw: string, hideUnsupported?: boolean) => string;
4
+ export declare const dataEnumToDiscordText: (item: DataEnums, hideUnsupported?: boolean) => string;
package/lib/format.js CHANGED
@@ -55,21 +55,21 @@ const markdownToDiscordText = (items) => {
55
55
  })
56
56
  .join('');
57
57
  };
58
- const markdownRawToDiscordText = (raw) => {
58
+ const markdownRawToDiscordText = (raw, hideUnsupported) => {
59
59
  let text = raw;
60
- text = text.replace(/!\[([^\]]*)\]\(([^)]*)\)/g, '[$1]($2)');
60
+ text = text.replace(/!\[([^\]]*)\]\(([^)]*)\)/g, hideUnsupported ? '' : '[$1]($2)');
61
61
  return text;
62
62
  };
63
- const dataEnumToDiscordText = (item) => {
63
+ const dataEnumToDiscordText = (item, hideUnsupported) => {
64
64
  switch (item.type) {
65
65
  case 'MarkdownOriginal':
66
- return markdownRawToDiscordText(String(item.value));
66
+ return markdownRawToDiscordText(String(item.value), hideUnsupported);
67
67
  case 'Attachment':
68
- return `[附件${item.options?.filename ? ': ' + item.options.filename : ''}]`;
68
+ return hideUnsupported ? '' : `[附件${item.options?.filename ? ': ' + item.options.filename : ''}]`;
69
69
  case 'Audio':
70
- return '[音频]';
70
+ return hideUnsupported ? '' : '[音频]';
71
71
  case 'Video':
72
- return '[视频]';
72
+ return hideUnsupported ? '' : '[视频]';
73
73
  default:
74
74
  return '';
75
75
  }
package/lib/send.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { createResult, ResultCode } from 'alemonjs';
2
2
  import { readFileSync } from 'fs';
3
3
  import { dataEnumToDiscordText } from './format.js';
4
+ import { getDiscordConfig } from './config.js';
4
5
 
5
6
  const ImageURLToBuffer = async (url) => {
6
7
  const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
@@ -101,7 +102,11 @@ const sendchannel = async (client, param, val) => {
101
102
  const mds = val.filter(item => item.type === 'Markdown');
102
103
  const nativeTypes = new Set(['Image', 'ImageURL', 'ImageFile', 'BT.group', 'Markdown', 'Mention', 'Text', 'Link']);
103
104
  const unsupportedItems = val.filter(item => !nativeTypes.has(item.type));
104
- const fallbackText = unsupportedItems.map(item => dataEnumToDiscordText(item)).filter(Boolean).join('\n');
105
+ const hide = getDiscordConfig().hideUnsupported === true;
106
+ const fallbackText = unsupportedItems
107
+ .map(item => dataEnumToDiscordText(item, hide))
108
+ .filter(Boolean)
109
+ .join('\n');
105
110
  const content = val
106
111
  .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
107
112
  .map(item => {
@@ -139,6 +144,10 @@ const sendchannel = async (client, param, val) => {
139
144
  .join('');
140
145
  const contentMd = buildDiscordMdContent(mds);
141
146
  const finalContent = [content, contentMd, fallbackText].filter(Boolean).join('\n');
147
+ if (hide && !finalContent && images.length <= 0 && buttons.length <= 0) {
148
+ logger.info('[discord] hideUnsupported: 消息内容转换后为空,跳过发送');
149
+ return [];
150
+ }
142
151
  if (images.length > 0) {
143
152
  let bufferData = null;
144
153
  for (let i = 0; i < images.length; i++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "discord platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",