@alemonjs/discord 2.1.14 → 2.1.16

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/index.js CHANGED
@@ -88,7 +88,7 @@ const main = () => {
88
88
  .addOpen({ OpenId: UserId })
89
89
  .add({ tag: 'interaction.create' }).value);
90
90
  }
91
- void client.interactionsCallback(event.id, event.token, '正在处理您的请求...');
91
+ void client.interactionsCallbackEphemeral(event.id, event.token, '正在处理您的请求...');
92
92
  });
93
93
  client.on('MESSAGE_UPDATE', event => {
94
94
  if (!event.author) {
package/lib/sdk/api.d.ts CHANGED
@@ -144,10 +144,15 @@ export declare class DCAPI {
144
144
  gateway(): Promise<{
145
145
  url: string;
146
146
  }>;
147
- interactionsCallback(id: string, token: string, content: string): Promise<any>;
147
+ interactionsCallback(id: string, token: string, data: {
148
+ type: number;
149
+ data: any;
150
+ }): Promise<any>;
151
+ interactionsCallbackEphemeral(id: string, token: string, content: string): Promise<any>;
148
152
  interactionsCallbackMessage(id: string, token: string, messageData: any): Promise<any>;
149
153
  interactionsCallbackForm(id: string, token: string, formData: any): Promise<any>;
150
154
  interactionsDeferred(id: string, token: string): Promise<any>;
155
+ interactionsCallbackModal(id: string, token: string, modalData: any): Promise<any>;
151
156
  interactionsFollowup(application_id: string, token: string, messageData: any): Promise<any>;
152
157
  interactionsFollowupForm(application_id: string, token: string, formData: any): Promise<any>;
153
158
  getGlobalApplicationCommands(application_id: string, params?: {
package/lib/sdk/api.js CHANGED
@@ -826,14 +826,21 @@ class DCAPI {
826
826
  url: '/gateway'
827
827
  });
828
828
  }
829
- interactionsCallback(id, token, content) {
829
+ interactionsCallback(id, token, data) {
830
+ return this.request({
831
+ method: 'POST',
832
+ url: `/interactions/${id}/${token}/callback`,
833
+ data
834
+ });
835
+ }
836
+ interactionsCallbackEphemeral(id, token, content) {
830
837
  return this.request({
831
838
  method: 'POST',
832
839
  url: `/interactions/${id}/${token}/callback`,
833
840
  data: {
834
841
  type: 4,
835
842
  data: {
836
- content: content,
843
+ content,
837
844
  flags: 64
838
845
  }
839
846
  }
@@ -868,6 +875,16 @@ class DCAPI {
868
875
  }
869
876
  });
870
877
  }
878
+ interactionsCallbackModal(id, token, modalData) {
879
+ return this.request({
880
+ method: 'POST',
881
+ url: `/interactions/${id}/${token}/callback`,
882
+ data: {
883
+ type: 9,
884
+ data: modalData
885
+ }
886
+ });
887
+ }
871
888
  interactionsFollowup(application_id, token, messageData) {
872
889
  return this.request({
873
890
  method: 'POST',
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,25 @@ 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 ? await client.channelsMessages(channelId, payload) : await client.channelsMessagesForm(channelId, payload);
143
205
  return [createResult(ResultCode.Ok, '完成', res)];
144
206
  }
145
207
  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.16",
4
4
  "description": "discord platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",