@alemonjs/discord 2.1.21 → 2.1.23

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/dist/index.html CHANGED
@@ -1,4 +1,4 @@
1
- <!DOCTYPE html>
1
+ <!doctype html>
2
2
  <html lang="en" id="__gui">
3
3
  <head>
4
4
  <meta charset="UTF-8" />
package/lib/index.js CHANGED
@@ -5,6 +5,18 @@ import { getMaster, platform } from './config.js';
5
5
  export { DCAPI as API } from './sdk/api.js';
6
6
  export { useClient, useValue } from './hook.js';
7
7
 
8
+ const SELECT_DUP_PREFIX = '__alemon_dup__:';
9
+ const decodeDuplicateSelectValue = (value) => {
10
+ if (typeof value !== 'string' || !value.startsWith(SELECT_DUP_PREFIX)) {
11
+ return value;
12
+ }
13
+ const content = value.slice(SELECT_DUP_PREFIX.length);
14
+ const separatorIndex = content.indexOf(':');
15
+ if (separatorIndex < 0) {
16
+ return value;
17
+ }
18
+ return content.slice(separatorIndex + 1);
19
+ };
8
20
  const main = () => {
9
21
  const port = process.env?.port || 17117;
10
22
  const url = `ws://127.0.0.1:${port}`;
@@ -39,7 +51,7 @@ const main = () => {
39
51
  const UserAvatar = createUserAvatar(UserId, event.author.avatar);
40
52
  if (event.type === 0 && event.member) {
41
53
  cbp.send(FormatEvent.create('message.create')
42
- .addPlatform({ Platform: platform, value: event, BotId: botId })
54
+ .addPlatform({ Platform: platform, value: event, BotId: botId, IsAtMe: atUsers.some(item => item.id === botId), IsPrivate: false })
43
55
  .addGuild({ GuildId: event.guild_id, SpaceId: event.channel_id })
44
56
  .addChannel({ ChannelId: event.channel_id })
45
57
  .addUser({ UserId, UserKey, UserName: event.author.username, UserAvatar: UserAvatar, IsMaster: isMaster, IsBot: false })
@@ -50,7 +62,7 @@ const main = () => {
50
62
  }
51
63
  else if (event.type === 0 && !event.member) {
52
64
  cbp.send(FormatEvent.create('private.message.create')
53
- .addPlatform({ Platform: platform, value: event, BotId: botId })
65
+ .addPlatform({ Platform: platform, value: event, BotId: botId, IsAtMe: false, IsPrivate: true })
54
66
  .addUser({ UserId, UserKey, UserName: event.author.username, UserAvatar: UserAvatar, IsMaster: isMaster, IsBot: false })
55
67
  .addMessage({ MessageId: event.id })
56
68
  .addText({ MessageText: msg })
@@ -60,6 +72,10 @@ const main = () => {
60
72
  else ;
61
73
  });
62
74
  client.on('INTERACTION_CREATE', event => {
75
+ const selectedValues = Array.isArray(event?.data?.values) ? event.data.values.map(item => decodeDuplicateSelectValue(item)) : [];
76
+ if (selectedValues.length > 0) {
77
+ event.data.values = selectedValues.filter((item) => typeof item === 'string');
78
+ }
63
79
  const isPrivate = typeof event['user'] === 'object' ? true : false;
64
80
  const user = isPrivate ? event['user'] : event['member'].user;
65
81
  const UserId = user.id;
@@ -68,12 +84,13 @@ const main = () => {
68
84
  const [isMaster, UserKey] = getMaster(UserId);
69
85
  const isCustom = !!event.data?.custom_id;
70
86
  const isName = !!event.data?.name;
71
- const command = isCustom ? event.data.custom_id : (isName ? `/${event.data.name}` : '');
87
+ const selectedCommand = typeof selectedValues[0] === 'string' ? selectedValues[0] : '';
88
+ const command = selectedCommand || (isCustom ? event.data.custom_id : isName ? `/${event.data.name}` : '');
72
89
  const notAutoConfirmation = isCustom ? /^notAutoConfirmation:/.test(command) : false;
73
90
  const currentMessageText = notAutoConfirmation ? command.replace(/^notAutoConfirmation:/, '') : command;
74
91
  if (isPrivate) {
75
92
  cbp.send(FormatEvent.create('private.interaction.create')
76
- .addPlatform({ Platform: platform, value: event, BotId: botId })
93
+ .addPlatform({ Platform: platform, value: event, BotId: botId, IsAtMe: false, IsPrivate: true })
77
94
  .addUser({ UserId, UserKey, UserName: UserName, UserAvatar: UserAvatar, IsMaster: isMaster, IsBot: false })
78
95
  .addMessage({ MessageId: event.id })
79
96
  .addText({ MessageText: currentMessageText })
@@ -82,7 +99,7 @@ const main = () => {
82
99
  }
83
100
  else {
84
101
  cbp.send(FormatEvent.create('interaction.create')
85
- .addPlatform({ Platform: platform, value: event, BotId: botId })
102
+ .addPlatform({ Platform: platform, value: event, BotId: botId, IsAtMe: false, IsPrivate: false })
86
103
  .addGuild({ GuildId: event['guild_id'], SpaceId: event.channel_id })
87
104
  .addChannel({ ChannelId: event.channel_id })
88
105
  .addUser({ UserId, UserKey, UserName: UserName, UserAvatar: UserAvatar, IsMaster: isMaster, IsBot: false })
@@ -82,6 +82,7 @@ interface Data {
82
82
  component_type?: number;
83
83
  type?: number;
84
84
  name?: string;
85
+ values?: string[];
85
86
  }
86
87
  type Public = {
87
88
  version: number;
package/lib/send.js CHANGED
@@ -3,19 +3,23 @@ import { readFileSync } from 'fs';
3
3
  import { dataEnumToDiscordText, markdownRawToDiscordText, markdownToDiscordText } from './format.js';
4
4
  import { getDiscordConfig } from './config.js';
5
5
 
6
+ const SELECT_DUP_PREFIX = '__alemon_dup__:';
7
+ const encodeDuplicateSelectValue = (value, index) => `${SELECT_DUP_PREFIX}${index}:${value}`;
6
8
  const fetchImageBuffer = async (url) => {
7
9
  const arrayBuffer = await fetch(url).then(res => res.arrayBuffer());
8
10
  return Buffer.from(arrayBuffer);
9
11
  };
10
- const createButtonsData = (rows) => rows.map(row => ({
11
- type: 1,
12
- components: row.value.map(button => ({
13
- type: 2,
14
- custom_id: button.options?.notAutoConfirmation ? `notAutoConfirmation:${button.options.data}` : button.options?.data ?? '',
15
- style: 1,
16
- label: button.value
17
- }))
18
- }));
12
+ const createButtonsData = (rows) => {
13
+ return rows.map(row => ({
14
+ type: 1,
15
+ components: row.value.map(button => ({
16
+ type: 2,
17
+ custom_id: button.options?.notAutoConfirmation ? `notAutoConfirmation:${button.options.data}` : (button.options?.data ?? ''),
18
+ style: 1,
19
+ label: button.value
20
+ }))
21
+ }));
22
+ };
19
23
  const createSelectData = (select) => {
20
24
  const meta = select.options ?? {};
21
25
  const kind = meta.kind ?? 'string';
@@ -29,13 +33,19 @@ const createSelectData = (select) => {
29
33
  disabled: meta.disabled
30
34
  };
31
35
  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
- }));
36
+ const seenValues = new Map();
37
+ base.options = (select.value ?? []).map(opt => {
38
+ const rawValue = String(opt.value ?? '');
39
+ const nextIndex = seenValues.get(rawValue) ?? 0;
40
+ seenValues.set(rawValue, nextIndex + 1);
41
+ return {
42
+ label: opt.label,
43
+ value: nextIndex > 0 ? encodeDuplicateSelectValue(rawValue, nextIndex) : rawValue,
44
+ description: opt.description,
45
+ default: opt.default,
46
+ emoji: opt.emoji ? { name: opt.emoji } : undefined
47
+ };
48
+ });
39
49
  }
40
50
  return { type: 1, components: [base] };
41
51
  };
@@ -135,9 +145,6 @@ const sendchannel = async (client, param, val) => {
135
145
  case 'Embed':
136
146
  embeds.push(item);
137
147
  break;
138
- case 'Modal':
139
- logger.warn('[discord] Modal 必须通过 interaction callback 发送,channelsMessages 流程已跳过');
140
- break;
141
148
  case 'Markdown':
142
149
  mdParts.push(markdownToDiscordText(item.value, hide));
143
150
  break;
@@ -195,11 +202,13 @@ const sendchannel = async (client, param, val) => {
195
202
  break;
196
203
  }
197
204
  }
198
- if (bufferData && hasEmbeds) {
205
+ if (bufferData) {
199
206
  payload.attachments = [{ id: 0, filename: 'image.png' }];
200
- const firstEmbed = payload.embeds[0];
201
- if (firstEmbed && !firstEmbed.image) {
202
- firstEmbed.image = { url: 'attachment://image.png' };
207
+ if (hasEmbeds) {
208
+ const firstEmbed = payload.embeds[0];
209
+ if (firstEmbed && !firstEmbed.image) {
210
+ firstEmbed.image = { url: 'attachment://image.png' };
211
+ }
203
212
  }
204
213
  }
205
214
  const res = await client.channelsMessagesForm(channelId, payload, bufferData ?? undefined);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alemonjs/discord",
3
- "version": "2.1.21",
3
+ "version": "2.1.23",
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": "c6aa5616afe091a37610dad22fbb2d2618d943b8"
69
- }
68
+ "gitHead": "7dab16a2167bbdf5706931b67e7eab2fc67835d6"
69
+ }