
























@@ -7,120 +7,13 @@ import {
77sendPayloadMediaSequenceOrFallback,
88sendTextMediaPayload,
99} from "openclaw/plugin-sdk/reply-payload";
10-import { readDiscordComponentSpec, type DiscordComponentMessageSpec } from "./components.js";
10+import { normalizeDiscordApprovalPayload } from "./outbound-approval.js";
11+import {
12+resolveDiscordComponentSpec,
13+sendDiscordComponentMessageLazy,
14+} from "./outbound-components.js";
1115import { createDiscordPayloadSendContext } from "./outbound-send-context.js";
121613-type DiscordComponentSendFn = typeof import("./send.components.js").sendDiscordComponentMessage;
14-type DiscordSharedInteractiveModule = typeof import("./shared-interactive.js");
15-16-let discordComponentSendPromise: Promise<DiscordComponentSendFn> | undefined;
17-let discordSharedInteractivePromise: Promise<DiscordSharedInteractiveModule> | undefined;
18-19-async function sendDiscordComponentMessageLazy(
20- ...args: Parameters<DiscordComponentSendFn>
21-): ReturnType<DiscordComponentSendFn> {
22-discordComponentSendPromise ??= import("./send.components.js").then(
23-(module) => module.sendDiscordComponentMessage,
24-);
25-return await (
26-await discordComponentSendPromise
27-)(...args);
28-}
29-30-function loadDiscordSharedInteractive(): Promise<DiscordSharedInteractiveModule> {
31-discordSharedInteractivePromise ??= import("./shared-interactive.js");
32-return discordSharedInteractivePromise;
33-}
34-35-function hasApprovalChannelData(payload: { channelData?: unknown }): boolean {
36-const channelData = payload.channelData;
37-if (!channelData || typeof channelData !== "object" || Array.isArray(channelData)) {
38-return false;
39-}
40-return Boolean((channelData as { execApproval?: unknown }).execApproval);
41-}
42-43-function neutralizeDiscordApprovalMentions(value: string): string {
44-return value
45-.replace(/@everyone/gi, "@\u200beveryone")
46-.replace(/@here/gi, "@\u200bhere")
47-.replace(/<@/g, "<@\u200b")
48-.replace(/<#/g, "<#\u200b");
49-}
50-51-export function normalizeDiscordApprovalPayload<
52-T extends {
53-text?: string;
54-channelData?: unknown;
55-},
56->(payload: T): T {
57-return hasApprovalChannelData(payload) && payload.text
58- ? {
59- ...payload,
60-text: neutralizeDiscordApprovalMentions(payload.text),
61-}
62- : payload;
63-}
64-65-export async function buildDiscordPresentationPayload(params: {
66-payload: Parameters<NonNullable<ChannelOutboundAdapter["renderPresentation"]>>[0]["payload"];
67-presentation: Parameters<
68-NonNullable<ChannelOutboundAdapter["renderPresentation"]>
69->[0]["presentation"];
70-}): Promise<typeof params.payload | null> {
71-const componentSpec = (await loadDiscordSharedInteractive()).buildDiscordPresentationComponents(
72-params.presentation,
73-);
74-if (!componentSpec) {
75-return null;
76-}
77-return {
78- ...params.payload,
79-channelData: {
80- ...params.payload.channelData,
81-discord: {
82- ...(params.payload.channelData?.discord as Record<string, unknown> | undefined),
83-presentationComponents: componentSpec,
84-},
85-},
86-};
87-}
88-89-function resolveDiscordComponentSpec(
90-payload: Parameters<NonNullable<ChannelOutboundAdapter["sendPayload"]>>[0]["payload"],
91-): Promise<DiscordComponentMessageSpec | undefined> {
92-const discordData = payload.channelData?.discord as
93-| { components?: unknown; presentationComponents?: DiscordComponentMessageSpec }
94-| undefined;
95-const rawComponentSpec =
96-discordData?.presentationComponents ?? readDiscordComponentSpec(discordData?.components);
97-if (rawComponentSpec) {
98-return Promise.resolve(
99-rawComponentSpec.text
100- ? rawComponentSpec
101- : {
102- ...rawComponentSpec,
103-text: payload.text?.trim() ? payload.text : undefined,
104-},
105-);
106-}
107-if (!payload.interactive) {
108-return Promise.resolve(undefined);
109-}
110-return loadDiscordSharedInteractive().then((module) => {
111-const interactiveSpec = module.buildDiscordInteractiveComponents(payload.interactive);
112-if (!interactiveSpec) {
113-return undefined;
114-}
115-return interactiveSpec.text
116- ? interactiveSpec
117- : {
118- ...interactiveSpec,
119-text: payload.text?.trim() ? payload.text : undefined,
120-};
121-});
122-}
123-12417export async function sendDiscordOutboundPayload(params: {
12518ctx: Parameters<NonNullable<ChannelOutboundAdapter["sendPayload"]>>[0];
12619fallbackAdapter: ChannelOutboundAdapter;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。