Skip to content
Open
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
79 changes: 79 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,42 @@ 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();
});
});

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

spy.mockRestore();
});

it("does not include content text 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).toBeUndefined();
expect(calledPayload.embeds).toBeDefined();
expect(calledPayload.components).toBeDefined();

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

// ============================================================================
Expand Down Expand Up @@ -2551,9 +2627,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
10 changes: 4 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,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 @@ -2378,7 +2376,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