refactor: dedupe discord structured send context

This commit is contained in:
Peter Steinberger
2026-04-06 18:37:49 +01:00
parent 0153d102d7
commit 90f33ed5da

View File

@@ -432,18 +432,10 @@ export async function sendStickerDiscord(
stickerIds: string[],
opts: DiscordSendOpts & { content?: string } = {},
): Promise<DiscordSendResult> {
const cfg = opts.cfg ?? loadConfig();
const accountInfo = resolveDiscordAccount({
cfg,
accountId: opts.accountId,
});
const { rest, request, channelId } = await resolveDiscordSendTarget(to, opts);
const content = opts.content?.trim();
const rewrittenContent = content
? rewriteDiscordKnownMentions(content, {
accountId: accountInfo.accountId,
})
: undefined;
const { rest, request, channelId, rewrittenContent } = await resolveDiscordStructuredSendContext(
to,
opts,
);
const stickers = normalizeStickerIds(stickerIds);
const res = (await request(
() =>
@@ -463,18 +455,10 @@ export async function sendPollDiscord(
poll: PollInput,
opts: DiscordSendOpts & { content?: string } = {},
): Promise<DiscordSendResult> {
const cfg = opts.cfg ?? loadConfig();
const accountInfo = resolveDiscordAccount({
cfg,
accountId: opts.accountId,
});
const { rest, request, channelId } = await resolveDiscordSendTarget(to, opts);
const content = opts.content?.trim();
const rewrittenContent = content
? rewriteDiscordKnownMentions(content, {
accountId: accountInfo.accountId,
})
: undefined;
const { rest, request, channelId, rewrittenContent } = await resolveDiscordStructuredSendContext(
to,
opts,
);
if (poll.durationSeconds !== undefined) {
throw new Error("Discord polls do not support durationSeconds; use durationHours");
}
@@ -494,6 +478,30 @@ export async function sendPollDiscord(
return toDiscordSendResult(res, channelId);
}
async function resolveDiscordStructuredSendContext(
to: string,
opts: DiscordSendOpts & { content?: string },
): Promise<{
rest: RequestClient;
request: DiscordClientRequest;
channelId: string;
rewrittenContent?: string;
}> {
const cfg = opts.cfg ?? loadConfig();
const accountInfo = resolveDiscordAccount({
cfg,
accountId: opts.accountId,
});
const { rest, request, channelId } = await resolveDiscordSendTarget(to, opts);
const content = opts.content?.trim();
const rewrittenContent = content
? rewriteDiscordKnownMentions(content, {
accountId: accountInfo.accountId,
})
: undefined;
return { rest, request, channelId, rewrittenContent };
}
type VoiceMessageOpts = {
cfg?: OpenClawConfig;
token?: string;