@alemonjs/discord 2.1.21 → 2.1.22
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 +18 -1
- package/lib/sdk/message/INTERACTION_CREATE.d.ts +1 -0
- package/lib/send.js +32 -23
- package/package.json +1 -1
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}`;
|
|
@@ -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,7 +84,8 @@ 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
|
|
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) {
|
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) =>
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
|
205
|
+
if (bufferData) {
|
|
199
206
|
payload.attachments = [{ id: 0, filename: 'image.png' }];
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
firstEmbed.image
|
|
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);
|