Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-discord-duplicate-card-content.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@chat-adapter/discord": patch
---

Fix duplicate content display when sending card messages on Discord
145 changes: 145 additions & 0 deletions packages/adapter-discord/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,77 @@ describe("postMessage", () => {

spy.mockRestore();
});

it("does not include content text when posting a card message", async () => {
const mockResponse = new Response(
JSON.stringify({
id: "msg005",
channel_id: "channel456",
content: "",
timestamp: "2021-01-01T00:00:00.000Z",
author: { id: "test-app-id", username: "bot" },
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
const spy = vi
.spyOn(adapter as any, "discordFetch")
.mockResolvedValue(mockResponse);

const cardMessage = {
card: Card({
title: "Test Card",
children: [Actions([Button({ id: "btn1", label: "Click me" })])],
}),
};

await adapter.postMessage("discord:guild1:channel456", cardMessage);

const calledPayload = spy.mock.calls[0]?.[2] as {
content?: string;
embeds?: unknown[];
components?: unknown[];
};
expect(calledPayload.content).toBeUndefined();
expect(calledPayload.embeds).toBeDefined();
expect(calledPayload.components).toBeDefined();

spy.mockRestore();
});

it("uses card over text when message has both", async () => {
const mockResponse = new Response(
JSON.stringify({
id: "msg006",
channel_id: "channel456",
content: "",
timestamp: "2021-01-01T00:00:00.000Z",
author: { id: "test-app-id", username: "bot" },
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
const spy = vi
.spyOn(adapter as any, "discordFetch")
.mockResolvedValue(mockResponse);

const mixedMessage = {
raw: "Some text that should be ignored",
card: Card({
title: "Card Wins",
children: [Actions([Button({ id: "btn1", label: "Click" })])],
}),
};

await adapter.postMessage("discord:guild1:channel456", mixedMessage);

const calledPayload = spy.mock.calls[0]?.[2] as {
content?: string;
embeds?: unknown[];
};
expect(calledPayload.content).toBeUndefined();
expect(calledPayload.embeds).toBeDefined();

spy.mockRestore();
});
});

// ============================================================================
Expand Down Expand Up @@ -1642,6 +1713,77 @@ describe("editMessage", () => {

spy.mockRestore();
});

it("clears content when editing to a card message", async () => {
const mockResponse = new Response(
JSON.stringify({
id: "msg004",
channel_id: "channel456",
content: "",
timestamp: "2021-01-01T00:00:00.000Z",
author: { id: "test-app-id", username: "bot" },
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
const spy = vi
.spyOn(adapter as any, "discordFetch")
.mockResolvedValue(mockResponse);

const cardMessage = {
card: Card({
title: "Test Card",
children: [Actions([Button({ id: "btn1", label: "Click me" })])],
}),
};

await adapter.editMessage(
"discord:guild1:channel456",
"msg004",
cardMessage
);

const calledPayload = spy.mock.calls[0]?.[2] as {
content?: string;
embeds?: unknown[];
components?: unknown[];
};
expect(calledPayload.content).toBe("");
expect(calledPayload.embeds).toBeDefined();
expect(calledPayload.components).toBeDefined();

spy.mockRestore();
});

it("restores content when editing from card back to text", async () => {
const mockResponse = new Response(
JSON.stringify({
id: "msg004",
channel_id: "channel456",
content: "New text message",
timestamp: "2021-01-01T00:00:00.000Z",
author: { id: "test-app-id", username: "bot" },
}),
{ status: 200, headers: { "Content-Type": "application/json" } }
);
const spy = vi
.spyOn(adapter as any, "discordFetch")
.mockResolvedValue(mockResponse);

await adapter.editMessage("discord:guild1:channel456", "msg004", {
raw: "Updated to plain text",
});

const calledPayload = spy.mock.calls[0]?.[2] as {
content?: string;
embeds?: unknown[];
components?: unknown[];
};
expect(calledPayload.content).toBe("Updated to plain text");
expect(calledPayload.embeds).toBeUndefined();
expect(calledPayload.components).toBeUndefined();

spy.mockRestore();
});
});

// ============================================================================
Expand Down Expand Up @@ -2551,9 +2693,12 @@ describe("postChannelMessage", () => {
await adapter.postChannelMessage("discord:guild1:channel456", cardMessage);

const calledPayload = spy.mock.calls[0]?.[2] as {
content?: string;
embeds?: unknown[];
components?: unknown[];
};
// Should NOT include content text when card is present (avoids duplicate display)
expect(calledPayload.content).toBeUndefined();
expect(calledPayload.embeds).toBeDefined();
expect(Array.isArray(calledPayload.embeds)).toBe(true);
expect((calledPayload.embeds ?? []).length).toBeGreaterThan(0);
Expand Down
11 changes: 5 additions & 6 deletions packages/adapter-discord/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import {
InteractionResponseType as DiscordInteractionResponseType,
verifyKey,
} from "discord-interactions";
import { cardToDiscordPayload, cardToFallbackText } from "./cards";
import { cardToDiscordPayload } from "./cards";
import { DiscordFormatConverter } from "./markdown";
import {
type DiscordActionRow,
Expand Down Expand Up @@ -811,8 +811,7 @@ export class DiscordAdapter implements Adapter<DiscordThreadId, unknown> {
const cardPayload = cardToDiscordPayload(card);
embeds.push(...cardPayload.embeds);
components.push(...cardPayload.components);
// Fallback text (truncated to Discord's limit)
payload.content = this.truncateContent(cardToFallbackText(card));
// Don't include text - Discord shows both text and card if text is present
} else {
// Regular text message (truncated to Discord's limit)
payload.content = this.truncateContent(
Expand Down Expand Up @@ -1157,8 +1156,8 @@ export class DiscordAdapter implements Adapter<DiscordThreadId, unknown> {
const cardPayload = cardToDiscordPayload(card);
embeds.push(...cardPayload.embeds);
components.push(...cardPayload.components);
// Fallback text (truncated to Discord's limit)
payload.content = this.truncateContent(cardToFallbackText(card));
// Clear content so old text doesn't persist alongside the card (Discord PATCH keeps omitted fields)
payload.content = "";
} else {
// Regular text message (truncated to Discord's limit)
payload.content = this.truncateContent(
Expand Down Expand Up @@ -2378,7 +2377,7 @@ export class DiscordAdapter implements Adapter<DiscordThreadId, unknown> {
const cardPayload = cardToDiscordPayload(card);
embeds.push(...cardPayload.embeds);
components.push(...cardPayload.components);
payload.content = this.truncateContent(cardToFallbackText(card));
// Don't include text - Discord shows both text and card if text is present
} else {
payload.content = this.truncateContent(
convertEmojiPlaceholders(
Expand Down
Loading