@alemonjs/discord 2.1.4 → 2.1.6
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/lib/send.js +48 -53
- package/package.json +1 -1
package/lib/send.js
CHANGED
|
@@ -25,6 +25,48 @@ const createButtonsData = (rows) => {
|
|
|
25
25
|
};
|
|
26
26
|
});
|
|
27
27
|
};
|
|
28
|
+
const mapToMarkdown = {
|
|
29
|
+
'MD.text': (item) => item.value,
|
|
30
|
+
'MD.blockquote': (item) => `> ${item.value}\n`,
|
|
31
|
+
'MD.bold': (item) => `**${item.value}**`,
|
|
32
|
+
'MD.italic': (item) => `*${item.value}*`,
|
|
33
|
+
'MD.divider': () => '---\n',
|
|
34
|
+
'MD.image': (item) => ``,
|
|
35
|
+
'MD.italicStar': (item) => `*${item.value}*`,
|
|
36
|
+
'MD.link': (item) => `[${item.value.text}](${item.value.url})`,
|
|
37
|
+
'MD.newline': () => '\n',
|
|
38
|
+
'MD.strikethrough': (item) => `~~${item.value}~~`,
|
|
39
|
+
'MD.subtitle': (item) => `## ${item.value}\n`,
|
|
40
|
+
'MD.title': (item) => `# ${item.value}\n`,
|
|
41
|
+
'MD.code': (item) => `\`\`\`${item.options?.language ?? ''}\n${item.value}\n\`\`\`\n`,
|
|
42
|
+
'MD.list': (item) => {
|
|
43
|
+
const listStr = item.value.map(listItem => {
|
|
44
|
+
if (typeof listItem.value === 'object') {
|
|
45
|
+
return `\n${listItem.value.index}. ${listItem.value.text}`;
|
|
46
|
+
}
|
|
47
|
+
return `\n- ${listItem.value}`;
|
|
48
|
+
});
|
|
49
|
+
return `${listStr.join('')}\n`;
|
|
50
|
+
},
|
|
51
|
+
'MD.mention': (item) => {
|
|
52
|
+
const { value, options } = item;
|
|
53
|
+
if (value === 'everyone' || value === 'all' || value === '' || typeof value !== 'string') {
|
|
54
|
+
return '<@everyone>';
|
|
55
|
+
}
|
|
56
|
+
if (options?.belong === 'user') {
|
|
57
|
+
return `<@${value}>`;
|
|
58
|
+
}
|
|
59
|
+
else if (options?.belong === 'channel') {
|
|
60
|
+
return `<#${value}>`;
|
|
61
|
+
}
|
|
62
|
+
return '';
|
|
63
|
+
},
|
|
64
|
+
'MD.content': (item) => item.value,
|
|
65
|
+
'MD.button': (item) => {
|
|
66
|
+
const { value } = item || {};
|
|
67
|
+
return value;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
28
70
|
const buildDiscordMdContent = (mds) => {
|
|
29
71
|
let contentMd = '';
|
|
30
72
|
if (mds && mds.length > 0) {
|
|
@@ -32,58 +74,8 @@ const buildDiscordMdContent = (mds) => {
|
|
|
32
74
|
if (item.type === 'Markdown') {
|
|
33
75
|
const md = item.value;
|
|
34
76
|
md.forEach(line => {
|
|
35
|
-
if (line.type
|
|
36
|
-
contentMd += line.
|
|
37
|
-
}
|
|
38
|
-
else if (line.type === 'MD.blockquote') {
|
|
39
|
-
contentMd += `> ${line.value}\n`;
|
|
40
|
-
}
|
|
41
|
-
else if (line.type === 'MD.bold') {
|
|
42
|
-
contentMd += `**${line.value}**`;
|
|
43
|
-
}
|
|
44
|
-
else if (line.type === 'MD.italic') {
|
|
45
|
-
contentMd += `*${line.value}*`;
|
|
46
|
-
}
|
|
47
|
-
else if (line.type === 'MD.divider') {
|
|
48
|
-
contentMd += '---\n';
|
|
49
|
-
}
|
|
50
|
-
else if (line.type === 'MD.image') {
|
|
51
|
-
contentMd += ``;
|
|
52
|
-
}
|
|
53
|
-
else if (line.type === 'MD.italicStar') {
|
|
54
|
-
contentMd += `*${line.value}*`;
|
|
55
|
-
}
|
|
56
|
-
else if (line.type === 'MD.link') {
|
|
57
|
-
contentMd += `[${line.value.text}](${line.value.url})`;
|
|
58
|
-
}
|
|
59
|
-
else if (line.type === 'MD.list') {
|
|
60
|
-
const listStr = line.value.map(listItem => {
|
|
61
|
-
if (typeof listItem.value === 'object') {
|
|
62
|
-
return `\n${listItem.value.index}. ${listItem.value.text}`;
|
|
63
|
-
}
|
|
64
|
-
return `\n- ${listItem.value}`;
|
|
65
|
-
});
|
|
66
|
-
contentMd += `${listStr.join('')}\n`;
|
|
67
|
-
}
|
|
68
|
-
else if (line.type === 'MD.newline') {
|
|
69
|
-
contentMd += '\n';
|
|
70
|
-
}
|
|
71
|
-
else if (line.type === 'MD.strikethrough') {
|
|
72
|
-
contentMd += `~~${line.value}~~`;
|
|
73
|
-
}
|
|
74
|
-
else if (line.type === 'MD.subtitle') {
|
|
75
|
-
contentMd += `## ${line.value}\n`;
|
|
76
|
-
}
|
|
77
|
-
else if (line.type === 'MD.title') {
|
|
78
|
-
contentMd += `# ${line.value}\n`;
|
|
79
|
-
}
|
|
80
|
-
else if (line.type === 'MD.code') {
|
|
81
|
-
const language = line?.options?.language || '';
|
|
82
|
-
contentMd += `\`\`\`${language}\n${line.value}\n\`\`\`\n`;
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
const value = line['value'] || '';
|
|
86
|
-
contentMd += String(value);
|
|
77
|
+
if (mapToMarkdown[line.type]) {
|
|
78
|
+
contentMd += mapToMarkdown[line.type](line);
|
|
87
79
|
}
|
|
88
80
|
});
|
|
89
81
|
}
|
|
@@ -143,7 +135,10 @@ const sendchannel = async (client, param, val) => {
|
|
|
143
135
|
})
|
|
144
136
|
.join('');
|
|
145
137
|
const contentMd = buildDiscordMdContent(mds);
|
|
146
|
-
const finalContent = [content, contentMd, fallbackText]
|
|
138
|
+
const finalContent = [content, contentMd, fallbackText]
|
|
139
|
+
.filter(Boolean)
|
|
140
|
+
.join('\n')
|
|
141
|
+
.replace(/^[^\S\n\r]+|[^\S\n\r]+$/g, '');
|
|
147
142
|
if (hide && !finalContent && images.length <= 0 && buttons.length <= 0) {
|
|
148
143
|
logger.info('[discord] hideUnsupported: 消息内容转换后为空,跳过发送');
|
|
149
144
|
return [];
|