mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
refactor(discord): share send target resolution and result mapping
This commit is contained in:
@@ -55,6 +55,13 @@ type DiscordSendOpts = {
|
||||
silent?: boolean;
|
||||
};
|
||||
|
||||
type DiscordClientRequest = ReturnType<typeof createDiscordClient>["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<DiscordSendResult> {
|
||||
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<DiscordSendResult> {
|
||||
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, {
|
||||
|
||||
Reference in New Issue
Block a user