From 2dd361c071e51546e94baf4a7e7cacbe0d350035 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Thu, 19 Feb 2026 00:28:56 +0000 Subject: [PATCH] refactor(discord): share send target resolution and result mapping --- src/discord/send.outbound.ts | 69 +++++++++++++++++++++--------------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/src/discord/send.outbound.ts b/src/discord/send.outbound.ts index 3a312efa5a0..62208ff2257 100644 --- a/src/discord/send.outbound.ts +++ b/src/discord/send.outbound.ts @@ -55,6 +55,13 @@ type DiscordSendOpts = { silent?: boolean; }; +type DiscordClientRequest = ReturnType["request"]; + +type DiscordChannelMessageResult = { + id?: string | null; + channel_id?: string | null; +}; + /** Discord thread names are capped at 100 characters. */ const DISCORD_THREAD_NAME_LIMIT = 100; @@ -73,6 +80,27 @@ function isForumLikeType(channelType?: number): boolean { return channelType === ChannelType.GuildForum || channelType === ChannelType.GuildMedia; } +function toDiscordSendResult( + result: DiscordChannelMessageResult, + fallbackChannelId: string, +): DiscordSendResult { + return { + messageId: result.id ? String(result.id) : "unknown", + channelId: String(result.channel_id ?? fallbackChannelId), + }; +} + +async function resolveDiscordSendTarget( + to: string, + opts: DiscordSendOpts, +): Promise<{ rest: RequestClient; request: DiscordClientRequest; channelId: string }> { + const cfg = loadConfig(); + const { rest, request } = createDiscordClient(opts, cfg); + const recipient = await parseAndResolveRecipient(to, opts.accountId); + const { channelId } = await resolveChannelId(rest, recipient, request); + return { rest, request, channelId }; +} + export async function sendMessageDiscord( to: string, text: string, @@ -210,10 +238,13 @@ export async function sendMessageDiscord( accountId: accountInfo.accountId, direction: "outbound", }); - return { - messageId: messageId ? String(messageId) : "unknown", - channelId: String(resultChannelId ?? channelId), - }; + return toDiscordSendResult( + { + id: messageId, + channel_id: resultChannelId, + }, + channelId, + ); } let result: { id: string; channel_id: string } | { id: string | null; channel_id: string }; @@ -261,10 +292,7 @@ export async function sendMessageDiscord( accountId: accountInfo.accountId, direction: "outbound", }); - return { - messageId: result.id ? String(result.id) : "unknown", - channelId: String(result.channel_id ?? channelId), - }; + return toDiscordSendResult(result, channelId); } export async function sendStickerDiscord( @@ -272,10 +300,7 @@ export async function sendStickerDiscord( stickerIds: string[], opts: DiscordSendOpts & { content?: string } = {}, ): Promise { - const cfg = loadConfig(); - const { rest, request } = createDiscordClient(opts, cfg); - const recipient = await parseAndResolveRecipient(to, opts.accountId); - const { channelId } = await resolveChannelId(rest, recipient, request); + const { rest, request, channelId } = await resolveDiscordSendTarget(to, opts); const content = opts.content?.trim(); const stickers = normalizeStickerIds(stickerIds); const res = (await request( @@ -288,10 +313,7 @@ export async function sendStickerDiscord( }) as Promise<{ id: string; channel_id: string }>, "sticker", )) as { id: string; channel_id: string }; - return { - messageId: res.id ? String(res.id) : "unknown", - channelId: String(res.channel_id ?? channelId), - }; + return toDiscordSendResult(res, channelId); } export async function sendPollDiscord( @@ -299,10 +321,7 @@ export async function sendPollDiscord( poll: PollInput, opts: DiscordSendOpts & { content?: string } = {}, ): Promise { - const cfg = loadConfig(); - const { rest, request } = createDiscordClient(opts, cfg); - const recipient = await parseAndResolveRecipient(to, opts.accountId); - const { channelId } = await resolveChannelId(rest, recipient, request); + const { rest, request, channelId } = await resolveDiscordSendTarget(to, opts); const content = opts.content?.trim(); if (poll.durationSeconds !== undefined) { throw new Error("Discord polls do not support durationSeconds; use durationHours"); @@ -320,10 +339,7 @@ export async function sendPollDiscord( }) as Promise<{ id: string; channel_id: string }>, "poll", )) as { id: string; channel_id: string }; - return { - messageId: res.id ? String(res.id) : "unknown", - channelId: String(res.channel_id ?? channelId), - }; + return toDiscordSendResult(res, channelId); } type VoiceMessageOpts = { @@ -412,10 +428,7 @@ export async function sendVoiceMessageDiscord( direction: "outbound", }); - return { - messageId: result.id ? String(result.id) : "unknown", - channelId: String(result.channel_id ?? channelId), - }; + return toDiscordSendResult(result, channelId); } catch (err) { if (channelId && rest && token) { throw await buildDiscordSendError(err, {