Dough Git

commit

Send the info DM as a Components v2 member card

commit
bd9c6f0ab142931cb3a8b44dc727531927801186
author
Clove Twilight <[email protected]>
date
2026-11-16 15:05
Each member who spoke gets a card like their page on the system site:
name, pronouns, status, about, tags and pride in their colour, with an
avatar and an "Open page" button. A footer links back to the message.

Co-Authored-By: Sandrone <[email protected]>

diff --git a/src/discord/editComponents.ts b/src/discord/editComponents.ts
index fad3fd6..9b7b7ad 100644
--- a/src/discord/editComponents.ts
+++ b/src/discord/editComponents.ts
@@ -3,9 +3,14 @@
 // Discord's component type numbers; importing discord.js here would make tests need env access
 export const ComponentType = {
     ActionRow: 1,
+    Button: 2,
+    Section: 9,
     TextDisplay: 10,
+    Thumbnail: 11,
     MediaGallery: 12,
     File: 13,
+    Separator: 14,
+    Container: 17,
 } as const;
 
 // Loose JSON so the existing message's components can pass straight through
diff --git a/src/discord/infoCard.ts b/src/discord/infoCard.ts
new file mode 100644
index 0000000..c739eea
--- /dev/null
+++ b/src/discord/infoCard.ts
@@ -0,0 +1,147 @@
+/* src/discord/infoCard.ts */
+
+import { maxNamedSpeakers } from "../proxy/names.ts";
+import type { Member } from "../proxy/types.ts";
+import { ComponentType } from "./editComponents.ts";
+
+// Same values the system site uses for its member pages and Discord embeds
+const siteUrl = "https://doughmination.co.uk";
+const accentColour = 0xeb459e;
+const maxDescription = 1000;
+const linkButtonStyle = 5;
+
+// deno-lint-ignore no-explicit-any
+type ComponentJson = Record<string, any>;
+
+export interface InfoCardInput {
+    members: Member[] | undefined;
+    username: string;
+    avatarUrl: string;
+    messageUrl: string;
+    ownerId: string;
+    systemName: string | null;
+}
+
+// Discord wants an integer; unset or non-hex colours get the site accent
+function colourOf(color: string | null): number {
+    const hex = color?.trim().replace(/^#/, "") ?? "";
+    const full = hex.length === 3
+        ? hex.split("").map((digit) => digit + digit).join("")
+        : hex;
+
+    return /^[0-9a-fA-F]{6}$/.test(full) ? parseInt(full, 16) : accentColour;
+}
+
+function shorten(text: string): string {
+    return text.length > maxDescription
+        ? `${text.slice(0, maxDescription - 1)}…`
+        : text;
+}
+
+function linkButton(label: string, url: string): ComponentJson {
+    return {
+        type: ComponentType.Button,
+        style: linkButtonStyle,
+        label,
+        url,
+    };
+}
+
+// A Section needs an accessory, so members without an avatar get plain text
+function header(text: string, avatarUrl: string | null): ComponentJson {
+    const display = {
+        type: ComponentType.TextDisplay,
+        content: text,
+    };
+
+    if (!avatarUrl) return display;
+
+    return {
+        type: ComponentType.Section,
+        components: [display],
+        accessory: {
+            type: ComponentType.Thumbnail,
+            media: {
+                url: avatarUrl,
+            },
+        },
+    };
+}
+
+// Mirrors the site's member page: name, pronouns, status, then about, tags, pride
+function memberCard(member: Member): ComponentJson {
+    const top = [`## ${member.display_name || member.name}`];
+    if (member.pronouns) top.push(member.pronouns);
+    if (member.status) {
+        top.push(`${member.status.emoji ?? ""} ${member.status.text}`.trim());
+    }
+
+    const details: string[] = [];
+    if (member.description) details.push(`**About**\n${shorten(member.description)}`);
+    if (member.tags?.length) details.push(`**Tags**\n${member.tags.join(", ")}`);
+    if (member.pride?.length) details.push(`**Pride**\n${member.pride.join(", ")}`);
+
+    const components = [header(top.join("\n"), member.avatar_url)];
+
+    if (details.length > 0) {
+        components.push(
+            {
+                type: ComponentType.Separator,
+            },
+            {
+                type: ComponentType.TextDisplay,
+                content: details.join("\n\n"),
+            },
+        );
+    }
+
+    components.push({
+        type: ComponentType.ActionRow,
+        components: [
+            linkButton("Open page", `${siteUrl}/${encodeURIComponent(member.id)}`),
+        ],
+    });
+
+    return {
+        type: ComponentType.Container,
+        accent_color: colourOf(member.color),
+        components,
+    };
+}
+
+// Messages from before a restart only have the webhook's name and avatar
+function unknownCard(username: string, avatarUrl: string): ComponentJson {
+    return {
+        type: ComponentType.Container,
+        accent_color: accentColour,
+        components: [header(`## ${username}`, avatarUrl)],
+    };
+}
+
+// Capped like the proxy names, which keeps it under Discord's 40 component limit
+export function buildInfoCard(input: InfoCardInput): ComponentJson[] {
+    const shown = input.members?.slice(0, maxNamedSpeakers);
+    const hidden = (input.members?.length ?? 0) - (shown?.length ?? 0);
+
+    const cards = shown
+        ? shown.map(memberCard)
+        : [unknownCard(input.username, input.avatarUrl)];
+
+    const footer = [`Sent by <@${input.ownerId}>`];
+    if (input.systemName) footer.push(input.systemName);
+    if (hidden > 0) footer.unshift(`& ${hidden} more`);
+
+    return [
+        ...cards,
+        {
+            type: ComponentType.TextDisplay,
+            content: `-# ${footer.join(" · ")}`,
+        },
+        {
+            type: ComponentType.ActionRow,
+            components: [
+                linkButton("Jump to message", input.messageUrl),
+            ],
+        },
+    ];
+}
diff --git a/src/discord/reactions.ts b/src/discord/reactions.ts
index c0fd2f1..e14d739 100644
--- a/src/discord/reactions.ts
+++ b/src/discord/reactions.ts
@@ -1,41 +1,23 @@
 /* src/discord/reactions.ts */
 
-import type {
-    MessageReaction,
-    PartialMessageReaction,
-    PartialUser,
-    User,
+import {
+    type APIMessageTopLevelComponent,
+    MessageFlags,
+    type MessageReaction,
+    type PartialMessageReaction,
+    type PartialUser,
+    type User,
 } from "discord.js";
 
 import { ownerUserId } from "../config/env.ts";
-import type { Member } from "../proxy/types.ts";
 import { system } from "../state/system.ts";
+import { buildInfoCard } from "./infoCard.ts";
 import { isProxied } from "./ownership.ts";
 import { sendersOf } from "./senders.ts";
 
 const deleteEmoji = "❌";
 const infoEmoji = "❓";
 
-function memberLine(member: Member): string {
-    const pronouns = member.pronouns ? ` (${member.pronouns})` : "";
-    return `**${member.display_name ?? member.name}**${pronouns}`;
-}
-
-// One line per member when known, else the webhook name it was sent under
-function senderInfo(
-    messageId: string,
-    username: string,
-    url: string,
-): string {
-    const members = sendersOf(messageId);
-    const lines = members ? members.map(memberLine) : [`**${username}**`];
-
-    if (system.name) lines.push(`System: **${system.name}**`);
-    lines.push(`Sent by <@${ownerUserId}>`, url);
-
-    return lines.join("\n");
-}
-
 export async function handleReaction(
     partialReaction: MessageReaction | PartialMessageReaction,
     partialUser: User | PartialUser,
@@ -60,9 +42,23 @@ export async function handleReaction(
         return;
     }
 
+    const components = buildInfoCard({
+        members: sendersOf(message.id),
+        username: message.author.username,
+        avatarUrl: message.author.displayAvatarURL(),
+        messageUrl: message.url,
+        ownerId: ownerUserId,
+        systemName: system.name,
+    });
+
     // DMs can be closed; the info just doesn't arrive then
-    await user.send(senderInfo(message.id, message.author.username, message.url))
-        .catch(() => {});
+    await user.send({
+        flags: MessageFlags.IsComponentsV2,
+        components: components as APIMessageTopLevelComponent[],
+    }).catch((error) => {
+        // Closed DMs are expected; anything else means the card itself is wrong
+        if (error?.code !== 50007) console.error("Info DM failed:", error);
+    });
 
     await reaction.users.remove(user.id).catch(() => {});
 }
diff --git a/src/proxy/types.ts b/src/proxy/types.ts
index 0b02737..8ed79db 100644
--- a/src/proxy/types.ts
+++ b/src/proxy/types.ts
@@ -5,11 +5,21 @@ export interface ProxyTag {
     suffix: string | null;
 }
 
+export interface MemberStatus {
+    text: string;
+    emoji: string | null;
+}
+
 export interface Member {
     id: string;
     name: string;
     display_name: string | null;
     pronouns: string | null;
+    color: string | null;
+    description: string | null;
+    tags?: string[];
+    pride?: string[];
+    status?: MemberStatus | null;
     avatar_url: string | null;
     webhook_avatar_url: string | null;
     proxy_tags: ProxyTag[];
diff --git a/tests/discord/infoCard_test.ts b/tests/discord/infoCard_test.ts
new file mode 100644
index 0000000..be780e0
--- /dev/null
+++ b/tests/discord/infoCard_test.ts
@@ -0,0 +1,90 @@
+/* tests/discord/infoCard_test.ts */
+
+import { assert, assertEquals, assertStringIncludes } from "@std/assert";
+
+import { ComponentType } from "../../src/discord/editComponents.ts";
+import { buildInfoCard } from "../../src/discord/infoCard.ts";
+import { member } from "../fixtures.ts";
+
+const base = {
+    username: "Webhook Name",
+    avatarUrl: "https://cdn.example/avatar.png",
+    messageUrl: "https://discord.com/channels/1/2/3",
+    ownerId: "42",
+    systemName: "Doughmination System",
+};
+
+// deno-lint-ignore no-explicit-any
+function countComponents(components: any[]): number {
+    return components.reduce((total, component) => {
+        const children = [
+            ...(component.components ?? []),
+            ...(component.accessory ? [component.accessory] : []),
+        ];
+        return total + 1 + countComponents(children);
+    }, 0);
+}
+
+Deno.test("member card shows site details in their colour", () => {
+    const clove = {
+        ...member("kzsbyo"),
+        display_name: "Clove",
+        pronouns: "fae/faer",
+        color: "cba6f7",
+        avatar_url: "https://site.example/clove.png",
+        description: "Host",
+        tags: ["Host"],
+        pride: ["Transgender"],
+        status: {
+            text: "Coding again...",
+            emoji: "💻",
+        },
+    };
+
+    const [card] = buildInfoCard({
+        ...base,
+        members: [clove],
+    });
+    const text = JSON.stringify(card);
+
+    assertEquals(card.accent_color, 0xcba6f7);
+    assertEquals(card.components[0].type, ComponentType.Section);
+    assertStringIncludes(text, "## Clove\\nfae/faer\\n💻 Coding again...");
+    assertStringIncludes(text, "**Pride**\\nTransgender");
+    assertStringIncludes(text, "https://doughmination.co.uk/kzsbyo");
+});
+
+Deno.test("member without avatar or details is plain text", () => {
+    const [card] = buildInfoCard({
+        ...base,
+        members: [member("a")],
+    });
+
+    assertEquals(card.accent_color, 0xeb459e);
+    assertEquals(card.components[0].type, ComponentType.TextDisplay);
+});
+
+Deno.test("unknown sender falls back to the webhook name", () => {
+    const [card] = buildInfoCard({
+        ...base,
+        members: undefined,
+    });
+
+    assertStringIncludes(JSON.stringify(card), "## Webhook Name");
+});
+
+Deno.test("big front stays under 40 components and counts the rest", () => {
+    const crowd = Array.from({ length: 10 }, (_, index) => ({
+        ...member(`m${index}`),
+        avatar_url: "https://site.example/a.png",
+        description: "x".repeat(5000),
+    }));
+
+    const components = buildInfoCard({
+        ...base,
+        members: crowd,
+    });
+
+    assert(countComponents(components) <= 40);
+    assertStringIncludes(JSON.stringify(components), "& 7 more");
+});
diff --git a/tests/fixtures.ts b/tests/fixtures.ts
index eb6c17b..e807d91 100644
--- a/tests/fixtures.ts
+++ b/tests/fixtures.ts
@@ -12,6 +12,8 @@ export function member(
         name: id,
         display_name: null,
         pronouns: null,
+        color: null,
+        description: null,
         avatar_url: null,
         webhook_avatar_url: null,
         proxy_tags: [{ prefix, suffix }],