@alemonjs/discord 2.1.13 → 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.
package/lib/index.js CHANGED
@@ -66,7 +66,8 @@ const main = () => {
66
66
  const UserAvatar = createUserAvatar(UserId, user.avatar);
67
67
  const UserName = user.username;
68
68
  const [isMaster, UserKey] = getMaster(UserId);
69
- const MessageText = event.data.custom_id;
69
+ const isCustom = !!event.data?.custom_id;
70
+ const MessageText = isCustom ? event?.data?.custom_id : `/${event.data?.name}`;
70
71
  if (isPrivate) {
71
72
  cbp.send(FormatEvent.create('private.interaction.create')
72
73
  .addPlatform({ Platform: platform, value: event, BotId: botId })
@@ -87,7 +88,7 @@ const main = () => {
87
88
  .addOpen({ OpenId: UserId })
88
89
  .add({ tag: 'interaction.create' }).value);
89
90
  }
90
- void client.interactionsCallback(event.id, event.token, MessageText);
91
+ void client.interactionsCallback(event.id, event.token, '正在处理您的请求...');
91
92
  });
92
93
  client.on('MESSAGE_UPDATE', event => {
93
94
  if (!event.author) {
package/lib/sdk/api.d.ts CHANGED
@@ -145,4 +145,28 @@ export declare class DCAPI {
145
145
  url: string;
146
146
  }>;
147
147
  interactionsCallback(id: string, token: string, content: string): Promise<any>;
148
+ interactionsCallbackMessage(id: string, token: string, messageData: any): Promise<any>;
149
+ interactionsCallbackForm(id: string, token: string, formData: any): Promise<any>;
150
+ interactionsDeferred(id: string, token: string): Promise<any>;
151
+ interactionsFollowup(application_id: string, token: string, messageData: any): Promise<any>;
152
+ interactionsFollowupForm(application_id: string, token: string, formData: any): Promise<any>;
153
+ getGlobalApplicationCommands(application_id: string, params?: {
154
+ with_localizations?: boolean;
155
+ }): Promise<any>;
156
+ createGlobalApplicationCommand(application_id: string, data: any): Promise<any>;
157
+ getGlobalApplicationCommand(application_id: string, command_id: string): Promise<any>;
158
+ editGlobalApplicationCommand(application_id: string, command_id: string, data: any): Promise<any>;
159
+ deleteGlobalApplicationCommand(application_id: string, command_id: string): Promise<any>;
160
+ bulkOverwriteGlobalApplicationCommands(application_id: string, data: any[]): Promise<any>;
161
+ getGuildApplicationCommands(application_id: string, guild_id: string, params?: {
162
+ with_localizations?: boolean;
163
+ }): Promise<any>;
164
+ createGuildApplicationCommand(application_id: string, guild_id: string, data: any): Promise<any>;
165
+ getGuildApplicationCommand(application_id: string, guild_id: string, command_id: string): Promise<any>;
166
+ editGuildApplicationCommand(application_id: string, guild_id: string, command_id: string, data: any): Promise<any>;
167
+ deleteGuildApplicationCommand(application_id: string, guild_id: string, command_id: string): Promise<any>;
168
+ bulkOverwriteGuildApplicationCommands(application_id: string, guild_id: string, data: any[]): Promise<any>;
169
+ getGuildApplicationCommandPermissions(application_id: string, guild_id: string): Promise<any>;
170
+ getApplicationCommandPermissions(application_id: string, guild_id: string, command_id: string): Promise<any>;
171
+ editApplicationCommandPermissions(application_id: string, guild_id: string, command_id: string, data: any): Promise<any>;
148
172
  }
package/lib/sdk/api.js CHANGED
@@ -839,6 +839,151 @@ class DCAPI {
839
839
  }
840
840
  });
841
841
  }
842
+ interactionsCallbackMessage(id, token, messageData) {
843
+ return this.request({
844
+ method: 'POST',
845
+ url: `/interactions/${id}/${token}/callback`,
846
+ data: {
847
+ type: 4,
848
+ data: messageData
849
+ }
850
+ });
851
+ }
852
+ interactionsCallbackForm(id, token, formData) {
853
+ return this.request({
854
+ method: 'POST',
855
+ url: `/interactions/${id}/${token}/callback`,
856
+ data: formData,
857
+ headers: {
858
+ 'Content-Type': 'multipart/form-data'
859
+ }
860
+ });
861
+ }
862
+ interactionsDeferred(id, token) {
863
+ return this.request({
864
+ method: 'POST',
865
+ url: `/interactions/${id}/${token}/callback`,
866
+ data: {
867
+ type: 5
868
+ }
869
+ });
870
+ }
871
+ interactionsFollowup(application_id, token, messageData) {
872
+ return this.request({
873
+ method: 'POST',
874
+ url: `/webhooks/${application_id}/${token}`,
875
+ data: messageData
876
+ });
877
+ }
878
+ interactionsFollowupForm(application_id, token, formData) {
879
+ return this.request({
880
+ method: 'POST',
881
+ url: `/webhooks/${application_id}/${token}`,
882
+ data: formData,
883
+ headers: {
884
+ 'Content-Type': 'multipart/form-data'
885
+ }
886
+ });
887
+ }
888
+ getGlobalApplicationCommands(application_id, params) {
889
+ return this.request({
890
+ method: 'GET',
891
+ url: `/applications/${application_id}/commands`,
892
+ params
893
+ });
894
+ }
895
+ createGlobalApplicationCommand(application_id, data) {
896
+ return this.request({
897
+ method: 'POST',
898
+ url: `/applications/${application_id}/commands`,
899
+ data
900
+ });
901
+ }
902
+ getGlobalApplicationCommand(application_id, command_id) {
903
+ return this.request({
904
+ method: 'GET',
905
+ url: `/applications/${application_id}/commands/${command_id}`
906
+ });
907
+ }
908
+ editGlobalApplicationCommand(application_id, command_id, data) {
909
+ return this.request({
910
+ method: 'PATCH',
911
+ url: `/applications/${application_id}/commands/${command_id}`,
912
+ data
913
+ });
914
+ }
915
+ deleteGlobalApplicationCommand(application_id, command_id) {
916
+ return this.request({
917
+ method: 'DELETE',
918
+ url: `/applications/${application_id}/commands/${command_id}`
919
+ });
920
+ }
921
+ bulkOverwriteGlobalApplicationCommands(application_id, data) {
922
+ return this.request({
923
+ method: 'PUT',
924
+ url: `/applications/${application_id}/commands`,
925
+ data
926
+ });
927
+ }
928
+ getGuildApplicationCommands(application_id, guild_id, params) {
929
+ return this.request({
930
+ method: 'GET',
931
+ url: `/applications/${application_id}/guilds/${guild_id}/commands`,
932
+ params
933
+ });
934
+ }
935
+ createGuildApplicationCommand(application_id, guild_id, data) {
936
+ return this.request({
937
+ method: 'POST',
938
+ url: `/applications/${application_id}/guilds/${guild_id}/commands`,
939
+ data
940
+ });
941
+ }
942
+ getGuildApplicationCommand(application_id, guild_id, command_id) {
943
+ return this.request({
944
+ method: 'GET',
945
+ url: `/applications/${application_id}/guilds/${guild_id}/commands/${command_id}`
946
+ });
947
+ }
948
+ editGuildApplicationCommand(application_id, guild_id, command_id, data) {
949
+ return this.request({
950
+ method: 'PATCH',
951
+ url: `/applications/${application_id}/guilds/${guild_id}/commands/${command_id}`,
952
+ data
953
+ });
954
+ }
955
+ deleteGuildApplicationCommand(application_id, guild_id, command_id) {
956
+ return this.request({
957
+ method: 'DELETE',
958
+ url: `/applications/${application_id}/guilds/${guild_id}/commands/${command_id}`
959
+ });
960
+ }
961
+ bulkOverwriteGuildApplicationCommands(application_id, guild_id, data) {
962
+ return this.request({
963
+ method: 'PUT',
964
+ url: `/applications/${application_id}/guilds/${guild_id}/commands`,
965
+ data
966
+ });
967
+ }
968
+ getGuildApplicationCommandPermissions(application_id, guild_id) {
969
+ return this.request({
970
+ method: 'GET',
971
+ url: `/applications/${application_id}/guilds/${guild_id}/commands/permissions`
972
+ });
973
+ }
974
+ getApplicationCommandPermissions(application_id, guild_id, command_id) {
975
+ return this.request({
976
+ method: 'GET',
977
+ url: `/applications/${application_id}/guilds/${guild_id}/commands/${command_id}/permissions`
978
+ });
979
+ }
980
+ editApplicationCommandPermissions(application_id, guild_id, command_id, data) {
981
+ return this.request({
982
+ method: 'PUT',
983
+ url: `/applications/${application_id}/guilds/${guild_id}/commands/${command_id}/permissions`,
984
+ data
985
+ });
986
+ }
842
987
  }
843
988
 
844
989
  export { API_URL, CDN_URL, DCAPI };
@@ -78,8 +78,10 @@ interface Channel {
78
78
  }
79
79
  interface Data {
80
80
  id: number;
81
- custom_id: string;
82
- component_type: number;
81
+ custom_id?: string;
82
+ component_type?: number;
83
+ type?: number;
84
+ name?: string;
83
85
  }
84
86
  type Public = {
85
87
  version: number;
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.13",
3
+ "version": "2.1.15",
4
4
  "description": "discord platform connection",
5
5
  "author": "lemonade",
6
6
  "license": "MIT",
@@ -65,5 +65,5 @@
65
65
  "type": "git",
66
66
  "url": "https://github.com/lemonade-lab/alemonjs.git"
67
67
  },
68
- "gitHead": "fee3935ca19b1658ea4b41d5d3e7f281e7efc932"
69
- }
68
+ "gitHead": "c6aa5616afe091a37610dad22fbb2d2618d943b8"
69
+ }
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2013-present, Yuxi (Evan) You
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIdED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.