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

@@ -8,7 +8,7 @@ import {
} from "@buape/carbon";
import { PollLayoutType } from "discord-api-types/payloads/v10";
import type { RESTAPIPoll } from "discord-api-types/rest/v10";
import { Routes, type APIEmbed } from "discord-api-types/v10";
import { Routes, type APIChannel, type APIEmbed } from "discord-api-types/v10";
import type { ChunkMode } from "../auto-reply/chunk.js";
import { loadConfig } from "../config/config.js";
import type { RetryRunner } from "../infra/retry-policy.js";
@@ -242,6 +242,18 @@ async function resolveChannelId(
return { channelId: dmChannel.id, dm: true };
}
export async function resolveDiscordChannelType(
rest: RequestClient,
channelId: string,
): Promise<number | undefined> {
try {
const channel = (await rest.get(Routes.channel(channelId))) as APIChannel | undefined;
return channel?.type;
} catch {
return undefined;
}
}
// Discord message flag for silent/suppress notifications
export const SUPPRESS_NOTIFICATIONS_FLAG = 1 << 12;
@@ -329,6 +341,15 @@ export function stripUndefinedFields<T extends object>(value: T): T {
return Object.fromEntries(Object.entries(value).filter(([, entry]) => entry !== undefined)) as T;
}
export function toDiscordFileBlob(data: Blob | Uint8Array): Blob {
if (data instanceof Blob) {
return data;
}
const arrayBuffer = new ArrayBuffer(data.byteLength);
new Uint8Array(arrayBuffer).set(data);
return new Blob([arrayBuffer]);
}
async function sendDiscordText(
rest: RequestClient,
channelId: string,
@@ -404,14 +425,7 @@ async function sendDiscordMedia(
const caption = chunks[0] ?? "";
const messageReference = replyTo ? { message_id: replyTo, fail_if_not_exists: false } : undefined;
const flags = silent ? SUPPRESS_NOTIFICATIONS_FLAG : undefined;
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);
const captionComponents = resolveDiscordSendComponents({
components,
text: caption,