mirror of
https://github.com/farcasclaudiu/openclaw.git
synced 2026-06-28 15:01:41 +03:00
Discord: preserve media caption whitespace
This commit is contained in:
@@ -246,6 +246,32 @@ describe("sendMessageDiscord", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("sends media with empty text without content field", async () => {
|
||||||
|
const { rest, postMock } = makeRest();
|
||||||
|
postMock.mockResolvedValue({ id: "msg", channel_id: "789" });
|
||||||
|
const res = await sendMessageDiscord("channel:789", "", {
|
||||||
|
rest,
|
||||||
|
token: "t",
|
||||||
|
mediaUrl: "file:///tmp/photo.jpg",
|
||||||
|
});
|
||||||
|
expect(res.messageId).toBe("msg");
|
||||||
|
const body = postMock.mock.calls[0]?.[1]?.body;
|
||||||
|
expect(body).not.toHaveProperty("content");
|
||||||
|
expect(body).toHaveProperty("files");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves whitespace in media captions", async () => {
|
||||||
|
const { rest, postMock } = makeRest();
|
||||||
|
postMock.mockResolvedValue({ id: "msg", channel_id: "789" });
|
||||||
|
await sendMessageDiscord("channel:789", " spaced ", {
|
||||||
|
rest,
|
||||||
|
token: "t",
|
||||||
|
mediaUrl: "file:///tmp/photo.jpg",
|
||||||
|
});
|
||||||
|
const body = postMock.mock.calls[0]?.[1]?.body;
|
||||||
|
expect(body).toHaveProperty("content", " spaced ");
|
||||||
|
});
|
||||||
|
|
||||||
it("includes message_reference when replying", async () => {
|
it("includes message_reference when replying", async () => {
|
||||||
const { rest, postMock } = makeRest();
|
const { rest, postMock } = makeRest();
|
||||||
postMock.mockResolvedValue({ id: "msg1", channel_id: "789" });
|
postMock.mockResolvedValue({ id: "msg1", channel_id: "789" });
|
||||||
|
|||||||
@@ -361,13 +361,17 @@ async function sendDiscordMedia(
|
|||||||
const media = await loadWebMedia(mediaUrl);
|
const media = await loadWebMedia(mediaUrl);
|
||||||
const chunks = text ? buildDiscordTextChunks(text, { maxLinesPerMessage, chunkMode }) : [];
|
const chunks = text ? buildDiscordTextChunks(text, { maxLinesPerMessage, chunkMode }) : [];
|
||||||
const caption = chunks[0] ?? "";
|
const caption = chunks[0] ?? "";
|
||||||
|
const hasCaption = caption.trim().length > 0;
|
||||||
const messageReference = replyTo ? { message_id: replyTo, fail_if_not_exists: false } : undefined;
|
const messageReference = replyTo ? { message_id: replyTo, fail_if_not_exists: false } : undefined;
|
||||||
const res = (await request(
|
const res = (await request(
|
||||||
() =>
|
() =>
|
||||||
rest.post(Routes.channelMessages(channelId), {
|
rest.post(Routes.channelMessages(channelId), {
|
||||||
body: {
|
body: {
|
||||||
content: caption || undefined,
|
// Only include content when there is actual text; Discord rejects
|
||||||
message_reference: messageReference,
|
// media-only messages that carry an empty or undefined content field
|
||||||
|
// when sent as multipart/form-data. Preserve whitespace in captions.
|
||||||
|
...(hasCaption ? { content: caption } : {}),
|
||||||
|
...(messageReference ? { message_reference: messageReference } : {}),
|
||||||
...(embeds?.length ? { embeds } : {}),
|
...(embeds?.length ? { embeds } : {}),
|
||||||
files: [
|
files: [
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user