@alemonjs/discord
Advanced tools
+78
-14
@@ -19,2 +19,40 @@ import { createResult, ResultCode } from 'alemonjs'; | ||
| })); | ||
| const createSelectData = (select) => { | ||
| const meta = select.options ?? {}; | ||
| const kind = meta.kind ?? 'string'; | ||
| const typeMap = { string: 3, user: 5, role: 6, mentionable: 7, channel: 8 }; | ||
| const base = { | ||
| type: typeMap[kind] ?? 3, | ||
| custom_id: meta.customId ?? '', | ||
| placeholder: meta.placeholder, | ||
| min_values: meta.minValues, | ||
| max_values: meta.maxValues, | ||
| disabled: meta.disabled | ||
| }; | ||
| if (kind === 'string') { | ||
| base.options = (select.value ?? []).map(opt => ({ | ||
| label: opt.label, | ||
| value: opt.value, | ||
| description: opt.description, | ||
| default: opt.default, | ||
| emoji: opt.emoji ? { name: opt.emoji } : undefined | ||
| })); | ||
| } | ||
| return { type: 1, components: [base] }; | ||
| }; | ||
| const createEmbedData = (embed) => { | ||
| const v = embed.value ?? {}; | ||
| return { | ||
| title: v.title, | ||
| description: v.description, | ||
| url: v.url, | ||
| color: v.color, | ||
| timestamp: v.timestamp ? new Date(v.timestamp).toISOString() : undefined, | ||
| image: v.image ? { url: v.image } : undefined, | ||
| thumbnail: v.thumbnail ? { url: v.thumbnail } : undefined, | ||
| author: v.author ? { name: v.author.name, url: v.author.url, icon_url: v.author.iconUrl } : undefined, | ||
| footer: v.footer ? { text: v.footer.text, icon_url: v.footer.iconUrl } : undefined, | ||
| fields: v.fields | ||
| }; | ||
| }; | ||
| const resolveImageBuffer = async (item) => { | ||
@@ -79,2 +117,4 @@ if (item.type === 'Image') { | ||
| const buttons = []; | ||
| const selects = []; | ||
| const embeds = []; | ||
| const textParts = []; | ||
@@ -94,2 +134,11 @@ const mdParts = []; | ||
| break; | ||
| case 'Select': | ||
| selects.push(item); | ||
| break; | ||
| case 'Embed': | ||
| embeds.push(item); | ||
| break; | ||
| case 'Modal': | ||
| logger.warn('[discord] Modal 必须通过 interaction callback 发送,channelsMessages 流程已跳过'); | ||
| break; | ||
| case 'Markdown': | ||
@@ -118,6 +167,16 @@ mdParts.push(markdownToDiscordText(item.value, hide)); | ||
| .replace(/^[^\S\n\r]+|[^\S\n\r]+$/g, ''); | ||
| if (hide && !finalContent && images.length <= 0 && buttons.length <= 0) { | ||
| if (hide && !finalContent && images.length <= 0 && buttons.length <= 0 && selects.length <= 0 && embeds.length <= 0) { | ||
| logger.info('[discord] hideUnsupported: 消息内容转换后为空,跳过发送'); | ||
| return []; | ||
| } | ||
| const components = []; | ||
| for (const btn of buttons) { | ||
| if (typeof btn.value !== 'string') { | ||
| components.push(...createButtonsData(btn.value)); | ||
| } | ||
| } | ||
| for (const sel of selects) { | ||
| components.push(createSelectData(sel)); | ||
| } | ||
| const embedData = embeds.length > 0 ? embeds.map(createEmbedData) : undefined; | ||
| if (images.length > 0) { | ||
@@ -131,20 +190,25 @@ let bufferData = null; | ||
| } | ||
| const res = await client.channelsMessagesForm(channelId, { content: finalContent }, bufferData); | ||
| const payload = { content: finalContent }; | ||
| if (components.length > 0) { | ||
| payload.components = components; | ||
| } | ||
| if (embedData) { | ||
| payload.embeds = embedData; | ||
| } | ||
| const res = await client.channelsMessagesForm(channelId, payload, bufferData); | ||
| return [createResult(ResultCode.Ok, '完成', res)]; | ||
| } | ||
| if (buttons.length > 0) { | ||
| let components = null; | ||
| for (const item of buttons) { | ||
| if (typeof item.value !== 'string') { | ||
| components = createButtonsData(item.value); | ||
| break; | ||
| } | ||
| if (components.length > 0 || embedData || finalContent) { | ||
| const payload = { content: finalContent }; | ||
| if (components.length > 0) { | ||
| payload.components = components; | ||
| } | ||
| const res = await client.channelsMessages(channelId, { content: finalContent, components }); | ||
| if (embedData) { | ||
| payload.embeds = embedData; | ||
| } | ||
| const res = components.length > 0 || embedData | ||
| ? await client.channelsMessages(channelId, payload) | ||
| : await client.channelsMessagesForm(channelId, payload); | ||
| return [createResult(ResultCode.Ok, '完成', res)]; | ||
| } | ||
| if (finalContent) { | ||
| const res = await client.channelsMessagesForm(channelId, { content: finalContent }); | ||
| return [createResult(ResultCode.Ok, '完成', res)]; | ||
| } | ||
| return []; | ||
@@ -151,0 +215,0 @@ } |
+1
-1
| { | ||
| "name": "@alemonjs/discord", | ||
| "version": "2.1.14", | ||
| "version": "2.1.15", | ||
| "description": "discord platform connection", | ||
@@ -5,0 +5,0 @@ "author": "lemonade", |
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
AI-detected potential code anomaly
Supply chain riskAI has identified unusual behaviors that may pose a security risk.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
357685
0.69%4311
1.51%