@alemonjs/discord 2.1.0 → 2.1.1

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.
@@ -0,0 +1,4 @@
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;
package/lib/format.js ADDED
@@ -0,0 +1,78 @@
1
+ const markdownToDiscordText = (items) => {
2
+ return items
3
+ .map(item => {
4
+ switch (item.type) {
5
+ case 'MD.text':
6
+ return item.value;
7
+ case 'MD.title':
8
+ return `# ${item.value}\n`;
9
+ case 'MD.subtitle':
10
+ return `## ${item.value}\n`;
11
+ case 'MD.bold':
12
+ return `**${item.value}**`;
13
+ case 'MD.italic':
14
+ case 'MD.italicStar':
15
+ return `*${item.value}*`;
16
+ case 'MD.strikethrough':
17
+ return `~~${item.value}~~`;
18
+ case 'MD.link': {
19
+ const v = item.value;
20
+ return `[${v.text}](${v.url})`;
21
+ }
22
+ case 'MD.image':
23
+ return `![image](${item.value})`;
24
+ case 'MD.list':
25
+ return (item.value
26
+ .map(li => {
27
+ if (typeof li.value === 'object') {
28
+ return `${li.value.index}. ${li.value.text ?? ''}`;
29
+ }
30
+ return `- ${li.value}`;
31
+ })
32
+ .join('\n') + '\n');
33
+ case 'MD.blockquote':
34
+ return `> ${item.value}\n`;
35
+ case 'MD.divider':
36
+ return '---\n';
37
+ case 'MD.newline':
38
+ return '\n';
39
+ case 'MD.code': {
40
+ const lang = item?.options?.language || '';
41
+ return `\`\`\`${lang}\n${item.value}\n\`\`\`\n`;
42
+ }
43
+ case 'MD.mention':
44
+ if (item.value === 'everyone') {
45
+ return '@everyone';
46
+ }
47
+ return `<@${item.value ?? ''}>`;
48
+ case 'MD.content':
49
+ return item.value;
50
+ case 'MD.button':
51
+ return `[${item.value}]`;
52
+ default:
53
+ return String(item?.value ?? '');
54
+ }
55
+ })
56
+ .join('');
57
+ };
58
+ const markdownRawToDiscordText = (raw) => {
59
+ let text = raw;
60
+ text = text.replace(/!\[([^\]]*)\]\(([^)]*)\)/g, '[$1]($2)');
61
+ return text;
62
+ };
63
+ const dataEnumToDiscordText = (item) => {
64
+ switch (item.type) {
65
+ case 'MarkdownOriginal':
66
+ return markdownRawToDiscordText(String(item.value));
67
+ case 'Attachment':
68
+ return `[附件${item.options?.filename ? ': ' + item.options.filename : ''}]`;
69
+ case 'Audio':
70
+ return '[音频]';
71
+ case 'Video':
72
+ return '[视频]';
73
+ default:
74
+ return '';
75
+ }
76
+ };
77
+
78
+ export { dataEnumToDiscordText, markdownRawToDiscordText, markdownToDiscordText };
package/lib/send.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { createResult, ResultCode } from 'alemonjs';
2
2
  import { readFileSync } from 'fs';
3
+ import { dataEnumToDiscordText } from './format.js';
3
4
 
4
5
  const ImageURLToBuffer = async (url) => {
5
6
  const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
@@ -23,15 +24,84 @@ const createButtonsData = (rows) => {
23
24
  };
24
25
  });
25
26
  };
27
+ const buildDiscordMdContent = (mds) => {
28
+ let contentMd = '';
29
+ if (mds && mds.length > 0) {
30
+ mds.forEach(item => {
31
+ if (item.type === 'Markdown') {
32
+ const md = item.value;
33
+ md.forEach(line => {
34
+ if (line.type === 'MD.text') {
35
+ contentMd += line.value;
36
+ }
37
+ else if (line.type === 'MD.blockquote') {
38
+ contentMd += `> ${line.value}\n`;
39
+ }
40
+ else if (line.type === 'MD.bold') {
41
+ contentMd += `**${line.value}**`;
42
+ }
43
+ else if (line.type === 'MD.italic') {
44
+ contentMd += `*${line.value}*`;
45
+ }
46
+ else if (line.type === 'MD.divider') {
47
+ contentMd += '---\n';
48
+ }
49
+ else if (line.type === 'MD.image') {
50
+ contentMd += `![${line.value}](${line.value})`;
51
+ }
52
+ else if (line.type === 'MD.italicStar') {
53
+ contentMd += `*${line.value}*`;
54
+ }
55
+ else if (line.type === 'MD.link') {
56
+ contentMd += `[${line.value.text}](${line.value.url})`;
57
+ }
58
+ else if (line.type === 'MD.list') {
59
+ const listStr = line.value.map(listItem => {
60
+ if (typeof listItem.value === 'object') {
61
+ return `\n${listItem.value.index}. ${listItem.value.text}`;
62
+ }
63
+ return `\n- ${listItem.value}`;
64
+ });
65
+ contentMd += `${listStr.join('')}\n`;
66
+ }
67
+ else if (line.type === 'MD.newline') {
68
+ contentMd += '\n';
69
+ }
70
+ else if (line.type === 'MD.strikethrough') {
71
+ contentMd += `~~${line.value}~~`;
72
+ }
73
+ else if (line.type === 'MD.subtitle') {
74
+ contentMd += `## ${line.value}\n`;
75
+ }
76
+ else if (line.type === 'MD.title') {
77
+ contentMd += `# ${line.value}\n`;
78
+ }
79
+ else if (line.type === 'MD.code') {
80
+ const language = line?.options?.language || '';
81
+ contentMd += `\`\`\`${language}\n${line.value}\n\`\`\`\n`;
82
+ }
83
+ else {
84
+ const value = line['value'] || '';
85
+ contentMd += String(value);
86
+ }
87
+ });
88
+ }
89
+ });
90
+ }
91
+ return contentMd;
92
+ };
26
93
  const sendchannel = async (client, param, val) => {
27
94
  try {
28
95
  if (!val || val.length <= 0) {
29
96
  return [];
30
97
  }
31
- const channel_id = param?.channel_id ?? '';
98
+ const channelId = param?.channel_id ?? '';
32
99
  const images = val.filter(item => item.type === 'Image' || item.type === 'ImageURL' || item.type === 'ImageFile');
33
100
  const buttons = val.filter(item => item.type === 'BT.group');
34
101
  const mds = val.filter(item => item.type === 'Markdown');
102
+ const nativeTypes = new Set(['Image', 'ImageURL', 'ImageFile', 'BT.group', 'Markdown', 'Mention', 'Text', 'Link']);
103
+ const unsupportedItems = val.filter(item => !nativeTypes.has(item.type));
104
+ const fallbackText = unsupportedItems.map(item => dataEnumToDiscordText(item)).filter(Boolean).join('\n');
35
105
  const content = val
36
106
  .filter(item => item.type === 'Mention' || item.type === 'Text' || item.type === 'Link')
37
107
  .map(item => {
@@ -67,6 +137,8 @@ const sendchannel = async (client, param, val) => {
67
137
  }
68
138
  })
69
139
  .join('');
140
+ const contentMd = buildDiscordMdContent(mds);
141
+ const finalContent = [content, contentMd, fallbackText].filter(Boolean).join('\n');
70
142
  if (images.length > 0) {
71
143
  let bufferData = null;
72
144
  for (let i = 0; i < images.length; i++) {
@@ -101,74 +173,11 @@ const sendchannel = async (client, param, val) => {
101
173
  bufferData = readFileSync(item.value);
102
174
  }
103
175
  }
104
- const res = await client.channelsMessagesForm(channel_id, {
105
- content: content
176
+ const res = await client.channelsMessagesForm(channelId, {
177
+ content: finalContent
106
178
  }, bufferData);
107
179
  return [createResult(ResultCode.Ok, '完成', res)];
108
180
  }
109
- let contentMd = '';
110
- if (mds && mds.length > 0) {
111
- mds.forEach(item => {
112
- if (item.type === 'Markdown') {
113
- const md = item.value;
114
- md.forEach(line => {
115
- if (line.type === 'MD.text') {
116
- contentMd += line.value;
117
- }
118
- else if (line.type === 'MD.blockquote') {
119
- contentMd += `> ${line.value}\n`;
120
- }
121
- else if (line.type === 'MD.bold') {
122
- contentMd += `**${line.value}**`;
123
- }
124
- else if (line.type === 'MD.italic') {
125
- contentMd += `*${line.value}*`;
126
- }
127
- else if (line.type === 'MD.divider') {
128
- contentMd += '---\n';
129
- }
130
- else if (line.type === 'MD.image') {
131
- contentMd += `![${line.value}](${line.value})`;
132
- }
133
- else if (line.type === 'MD.italicStar') {
134
- contentMd += `*${line.value}*`;
135
- }
136
- else if (line.type === 'MD.link') {
137
- contentMd += `[${typeof line.value === 'object' ? line.value.text : line.value}](${typeof line.value === 'object' ? line.value.url : line.value})`;
138
- }
139
- else if (line.type === 'MD.list') {
140
- const listStr = line.value.map(listItem => {
141
- if (typeof listItem.value === 'object') {
142
- return `\n${listItem.value.index}. ${listItem.value.text}`;
143
- }
144
- return `\n- ${listItem.value}`;
145
- });
146
- contentMd += `${listStr.join('')}\n`;
147
- }
148
- else if (line.type === 'MD.newline') {
149
- contentMd += '\n';
150
- }
151
- else if (line.type === 'MD.strikethrough') {
152
- contentMd += `~~${line.value}~~`;
153
- }
154
- else if (line.type === 'MD.subtitle') {
155
- contentMd += `## ${line.value}\n`;
156
- }
157
- else if (line.type === 'MD.title') {
158
- contentMd += `# ${line.value}\n`;
159
- }
160
- else if (line.type === 'MD.code') {
161
- const language = line?.options?.language || '';
162
- contentMd += `\`\`\`${language}\n${line.value}\n\`\`\`\n`;
163
- }
164
- else {
165
- const value = line['value'] || '';
166
- contentMd += String(value);
167
- }
168
- });
169
- }
170
- });
171
- }
172
181
  if (buttons && buttons.length > 0) {
173
182
  let components = null;
174
183
  buttons.forEach(item => {
@@ -176,17 +185,20 @@ const sendchannel = async (client, param, val) => {
176
185
  return;
177
186
  }
178
187
  const rows = item.value;
188
+ if (typeof rows === 'string') {
189
+ return;
190
+ }
179
191
  components = createButtonsData(rows);
180
192
  });
181
- const res = await client.channelsMessages(channel_id, {
182
- content: contentMd || content,
193
+ const res = await client.channelsMessages(channelId, {
194
+ content: finalContent,
183
195
  components: components
184
196
  });
185
197
  return [createResult(ResultCode.Ok, '完成', res)];
186
198
  }
187
- if (content || contentMd) {
188
- const res = await client.channelsMessagesForm(channel_id, {
189
- content: contentMd || content
199
+ if (finalContent) {
200
+ const res = await client.channelsMessagesForm(channelId, {
201
+ content: finalContent
190
202
  });
191
203
  return [createResult(ResultCode.Ok, '完成', res)];
192
204
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "discord platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",