@alemonjs/discord 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 +7 -0
- package/lib/config.d.ts +1 -0
- package/lib/format.d.ts +3 -3
- package/lib/format.js +24 -10
- package/lib/send.js +10 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,6 +37,13 @@ discord:
|
|
|
37
37
|
# ws 代理
|
|
38
38
|
websocket_proxy: 'http://localhost:7890'
|
|
39
39
|
# request_proxy: 'http://localhost:7890'
|
|
40
|
+
# 隐藏不支持的消息类型(可选,默认: false)
|
|
41
|
+
# 1:一级隐藏,不可读占位符([视频]、[音频]、[图片]、[附件]等)被置空,可读内容保留
|
|
42
|
+
# 2:二级隐藏,按钮仅显示指令数据(如 /挑战),链接仅显示 URL
|
|
43
|
+
# 3:三级隐藏,按钮和链接的 data 也不保留,完全隐藏
|
|
44
|
+
# 4:四级隐藏,不进行任何转换,降级数据直接丢弃
|
|
45
|
+
# 转换后内容为空时,将跳过发送并输出 info 日志
|
|
46
|
+
hideUnsupported: 1
|
|
40
47
|
|
|
41
48
|
```
|
|
42
49
|
|
package/lib/config.d.ts
CHANGED
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) => string;
|
|
4
|
-
export declare const dataEnumToDiscordText: (item: DataEnums) => 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
|
-
|
|
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 ``;
|
|
@@ -48,28 +52,38 @@ 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 ?? '');
|
|
54
63
|
}
|
|
55
64
|
})
|
|
56
|
-
.join('')
|
|
65
|
+
.join('')
|
|
66
|
+
.trim();
|
|
57
67
|
};
|
|
58
|
-
const markdownRawToDiscordText = (raw) => {
|
|
68
|
+
const markdownRawToDiscordText = (raw, hideUnsupported) => {
|
|
69
|
+
if (Number(hideUnsupported) >= 4)
|
|
70
|
+
return '';
|
|
59
71
|
let text = raw;
|
|
60
|
-
text = text.replace(/!\[([^\]]*)\]\(([^)]*)\)/g, '[$1]($2)');
|
|
72
|
+
text = text.replace(/!\[([^\]]*)\]\(([^)]*)\)/g, hideUnsupported ? '' : '[$1]($2)');
|
|
61
73
|
return text;
|
|
62
74
|
};
|
|
63
|
-
const dataEnumToDiscordText = (item) => {
|
|
75
|
+
const dataEnumToDiscordText = (item, hideUnsupported) => {
|
|
76
|
+
if (Number(hideUnsupported) >= 4)
|
|
77
|
+
return '';
|
|
64
78
|
switch (item.type) {
|
|
65
79
|
case 'MarkdownOriginal':
|
|
66
|
-
return markdownRawToDiscordText(String(item.value));
|
|
80
|
+
return markdownRawToDiscordText(String(item.value), hideUnsupported);
|
|
67
81
|
case 'Attachment':
|
|
68
|
-
return `[附件${item.options?.filename ? ': ' + item.options.filename : ''}]`;
|
|
82
|
+
return hideUnsupported ? '' : `[附件${item.options?.filename ? ': ' + item.options.filename : ''}]`;
|
|
69
83
|
case 'Audio':
|
|
70
|
-
return '[音频]';
|
|
84
|
+
return hideUnsupported ? '' : '[音频]';
|
|
71
85
|
case 'Video':
|
|
72
|
-
return '[视频]';
|
|
86
|
+
return hideUnsupported ? '' : '[视频]';
|
|
73
87
|
default:
|
|
74
88
|
return '';
|
|
75
89
|
}
|
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
|
|
105
|
+
const hide = getDiscordConfig().hideUnsupported;
|
|
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++) {
|