@alemonjs/discord 2.1.14 → 2.1.15

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.
Files changed (2) hide show
  1. package/lib/send.js +78 -14
  2. package/package.json +1 -1
package/lib/send.js CHANGED
@@ -16,6 +16,44 @@ const createButtonsData = (rows) => rows.map(row => ({
16
16
  label: button.value
17
17
  }))
18
18
  }));
19
+ const createSelectData = (select) => {
20
+ const meta = select.options ?? {};
21
+ const kind = meta.kind ?? 'string';
22
+ const typeMap = { string: 3, user: 5, role: 6, mentionable: 7, channel: 8 };
23
+ const base = {
24
+ type: typeMap[kind] ?? 3,
25
+ custom_id: meta.customId ?? '',
26
+ placeholder: meta.placeholder,
27
+ min_values: meta.minValues,
28
+ max_values: meta.maxValues,
29
+ disabled: meta.disabled
30
+ };
31
+ if (kind === 'string') {
32
+ base.options = (select.value ?? []).map(opt => ({
33
+ label: opt.label,
34
+ value: opt.value,
35
+ description: opt.description,
36
+ default: opt.default,
37
+ emoji: opt.emoji ? { name: opt.emoji } : undefined
38
+ }));
39
+ }
40
+ return { type: 1, components: [base] };
41
+ };
42
+ const createEmbedData = (embed) => {
43
+ const v = embed.value ?? {};
44
+ return {
45
+ title: v.title,
46
+ description: v.description,
47
+ url: v.url,
48
+ color: v.color,
49
+ timestamp: v.timestamp ? new Date(v.timestamp).toISOString() : undefined,
50
+ image: v.image ? { url: v.image } : undefined,
51
+ thumbnail: v.thumbnail ? { url: v.thumbnail } : undefined,
52
+ author: v.author ? { name: v.author.name, url: v.author.url, icon_url: v.author.iconUrl } : undefined,
53
+ footer: v.footer ? { text: v.footer.text, icon_url: v.footer.iconUrl } : undefined,
54
+ fields: v.fields
55
+ };
56
+ };
19
57
  const resolveImageBuffer = async (item) => {
20
58
  if (item.type === 'Image') {
21
59
  if (Buffer.isBuffer(item.value)) {
@@ -75,6 +113,8 @@ const sendchannel = async (client, param, val) => {
75
113
  const hide = getDiscordConfig().hideUnsupported;
76
114
  const images = [];
77
115
  const buttons = [];
116
+ const selects = [];
117
+ const embeds = [];
78
118
  const textParts = [];
79
119
  const mdParts = [];
80
120
  const fallbackParts = [];
@@ -89,6 +129,15 @@ const sendchannel = async (client, param, val) => {
89
129
  case 'ButtonGroup':
90
130
  buttons.push(item);
91
131
  break;
132
+ case 'Select':
133
+ selects.push(item);
134
+ break;
135
+ case 'Embed':
136
+ embeds.push(item);
137
+ break;
138
+ case 'Modal':
139
+ logger.warn('[discord] Modal 必须通过 interaction callback 发送,channelsMessages 流程已跳过');
140
+ break;
92
141
  case 'Markdown':
93
142
  mdParts.push(markdownToDiscordText(item.value, hide));
94
143
  break;
@@ -112,10 +161,20 @@ const sendchannel = async (client, param, val) => {
112
161
  .filter(Boolean)
113
162
  .join('\n')
114
163
  .replace(/^[^\S\n\r]+|[^\S\n\r]+$/g, '');
115
- if (hide && !finalContent && images.length <= 0 && buttons.length <= 0) {
164
+ if (hide && !finalContent && images.length <= 0 && buttons.length <= 0 && selects.length <= 0 && embeds.length <= 0) {
116
165
  logger.info('[discord] hideUnsupported: 消息内容转换后为空,跳过发送');
117
166
  return [];
118
167
  }
168
+ const components = [];
169
+ for (const btn of buttons) {
170
+ if (typeof btn.value !== 'string') {
171
+ components.push(...createButtonsData(btn.value));
172
+ }
173
+ }
174
+ for (const sel of selects) {
175
+ components.push(createSelectData(sel));
176
+ }
177
+ const embedData = embeds.length > 0 ? embeds.map(createEmbedData) : undefined;
119
178
  if (images.length > 0) {
120
179
  let bufferData = null;
121
180
  for (const img of images) {
@@ -124,22 +183,27 @@ const sendchannel = async (client, param, val) => {
124
183
  break;
125
184
  }
126
185
  }
127
- const res = await client.channelsMessagesForm(channelId, { content: finalContent }, bufferData);
128
- return [createResult(ResultCode.Ok, '完成', res)];
129
- }
130
- if (buttons.length > 0) {
131
- let components = null;
132
- for (const item of buttons) {
133
- if (typeof item.value !== 'string') {
134
- components = createButtonsData(item.value);
135
- break;
136
- }
186
+ const payload = { content: finalContent };
187
+ if (components.length > 0) {
188
+ payload.components = components;
189
+ }
190
+ if (embedData) {
191
+ payload.embeds = embedData;
137
192
  }
138
- const res = await client.channelsMessages(channelId, { content: finalContent, components });
193
+ const res = await client.channelsMessagesForm(channelId, payload, bufferData);
139
194
  return [createResult(ResultCode.Ok, '完成', res)];
140
195
  }
141
- if (finalContent) {
142
- const res = await client.channelsMessagesForm(channelId, { content: finalContent });
196
+ if (components.length > 0 || embedData || finalContent) {
197
+ const payload = { content: finalContent };
198
+ if (components.length > 0) {
199
+ payload.components = components;
200
+ }
201
+ if (embedData) {
202
+ payload.embeds = embedData;
203
+ }
204
+ const res = components.length > 0 || embedData
205
+ ? await client.channelsMessages(channelId, payload)
206
+ : await client.channelsMessagesForm(channelId, payload);
143
207
  return [createResult(ResultCode.Ok, '完成', res)];
144
208
  }
145
209
  return [];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "2.1.14",
3
+ "version": "2.1.15",
4
4
  "description": "discord platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",