refactor(discord): dedupe directory and media send paths

This commit is contained in:
Peter Steinberger
2026-02-22 17:54:30 +00:00
parent b3c78e5e05
commit 409a02691f
8 changed files with 253 additions and 91 deletions

View File

@@ -4,7 +4,6 @@ import {
type MessagePayloadObject,
type RequestClient,
} from "@buape/carbon";
import type { APIChannel } from "discord-api-types/v10";
import { ChannelType, Routes } from "discord-api-types/v10";
import { loadConfig } from "../config/config.js";
import { recordChannelActivity } from "../infra/channel-activity.js";
@@ -22,6 +21,8 @@ import {
createDiscordClient,
parseAndResolveRecipient,
resolveChannelId,
resolveDiscordChannelType,
toDiscordFileBlob,
stripUndefinedFields,
SUPPRESS_NOTIFICATIONS_FLAG,
} from "./send.shared.js";
@@ -63,13 +64,7 @@ export async function sendDiscordComponentMessage(
const recipient = await parseAndResolveRecipient(to, opts.accountId);
const { channelId } = await resolveChannelId(rest, recipient, request);
let channelType: number | undefined;
try {
const channel = (await rest.get(Routes.channel(channelId))) as APIChannel | undefined;
channelType = channel?.type;
} catch {
channelType = undefined;
}
const channelType = await resolveDiscordChannelType(rest, channelId);
if (channelType && DISCORD_FORUM_LIKE_TYPES.has(channelType)) {
throw new Error("Discord components are not supported in forum-style channels");
@@ -107,14 +102,7 @@ export async function sendDiscordComponentMessage(
`Component file block expects attachment "${expectedAttachmentName}", but the uploaded file is "${fileName}". Update components.blocks[].file or provide a matching filename.`,
);
}
let fileData: Blob;
if (media.buffer instanceof Blob) {
fileData = media.buffer;
} else {
const arrayBuffer = new ArrayBuffer(media.buffer.byteLength);
new Uint8Array(arrayBuffer).set(media.buffer);
fileData = new Blob([arrayBuffer]);
}
const fileData = toDiscordFileBlob(media.buffer);
files = [{ data: fileData, name: fileName }];
} else if (expectedAttachmentName) {
throw new Error(