69 lines
2.1 KiB
TypeScript
69 lines
2.1 KiB
TypeScript
import type { RequestClient } from "@buape/carbon";
|
|
|
|
import type { ReplyPayload } from "../../auto-reply/types.js";
|
|
import type { MarkdownTableMode } from "../../config/types.base.js";
|
|
import { convertMarkdownTables } from "../../markdown/tables.js";
|
|
import type { RuntimeEnv } from "../../runtime.js";
|
|
import { chunkDiscordText } from "../chunk.js";
|
|
import { sendMessageDiscord } from "../send.js";
|
|
|
|
export async function deliverDiscordReply(params: {
|
|
replies: ReplyPayload[];
|
|
target: string;
|
|
token: string;
|
|
accountId?: string;
|
|
rest?: RequestClient;
|
|
runtime: RuntimeEnv;
|
|
textLimit: number;
|
|
maxLinesPerMessage?: number;
|
|
replyToId?: string;
|
|
tableMode?: MarkdownTableMode;
|
|
}) {
|
|
const chunkLimit = Math.min(params.textLimit, 2000);
|
|
for (const payload of params.replies) {
|
|
const mediaList = payload.mediaUrls ?? (payload.mediaUrl ? [payload.mediaUrl] : []);
|
|
const rawText = payload.text ?? "";
|
|
const tableMode = params.tableMode ?? "code";
|
|
const text = convertMarkdownTables(rawText, tableMode);
|
|
if (!text && mediaList.length === 0) continue;
|
|
const replyTo = params.replyToId?.trim() || undefined;
|
|
|
|
if (mediaList.length === 0) {
|
|
let isFirstChunk = true;
|
|
for (const chunk of chunkDiscordText(text, {
|
|
maxChars: chunkLimit,
|
|
maxLines: params.maxLinesPerMessage,
|
|
})) {
|
|
const trimmed = chunk.trim();
|
|
if (!trimmed) continue;
|
|
await sendMessageDiscord(params.target, trimmed, {
|
|
token: params.token,
|
|
rest: params.rest,
|
|
accountId: params.accountId,
|
|
replyTo: isFirstChunk ? replyTo : undefined,
|
|
});
|
|
isFirstChunk = false;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
const firstMedia = mediaList[0];
|
|
if (!firstMedia) continue;
|
|
await sendMessageDiscord(params.target, text, {
|
|
token: params.token,
|
|
rest: params.rest,
|
|
mediaUrl: firstMedia,
|
|
accountId: params.accountId,
|
|
replyTo,
|
|
});
|
|
for (const extra of mediaList.slice(1)) {
|
|
await sendMessageDiscord(params.target, "", {
|
|
token: params.token,
|
|
rest: params.rest,
|
|
mediaUrl: extra,
|
|
accountId: params.accountId,
|
|
});
|
|
}
|
|
}
|
|
}
|