Dough Git

commit

Add d!reproxy to resend a message as another member

commit
aec05dc403c7ea74420ff276c3dd493606c54160
author
Clove Twilight <[email protected]>
date
2026-11-18 06:35
Co-Authored-By: Sandrone <[email protected]>

diff --git a/src/commands/help.ts b/src/commands/help.ts
index e464594..5f0fb09 100644
--- a/src/commands/help.ts
+++ b/src/commands/help.ts
@@ -6,6 +6,7 @@ const helpText = [
     "`d!edit <text>` edits the message you reply to, or your latest proxied message. `d!e` works too.",
     "`d!explain` explains the bot, and plurality. `d!ex` works too,",
     "`d!autoproxy` shows the state for this server, `d!autoproxy on|off` sets it. Add `-g` for every server, like `d!autoproxy -g off`. `d!ap` works too.",
+    "`d!reproxy <member>` resends the message you reply to, or your latest proxied message, as someone else, like `d!reproxy c1`. `d!rp` works too.",
     "-# React ❌ to delete a proxied message, ❓ to see who sent it.",
 ].join("\n");
 
diff --git a/src/commands/index.ts b/src/commands/index.ts
index 993cbd2..bfdd409 100644
--- a/src/commands/index.ts
+++ b/src/commands/index.ts
@@ -7,6 +7,7 @@ import { editCommand } from "./edit.ts";
 import { explainCommand } from "./explain.ts";
 import { helpCommand } from "./help.ts";
 import { parseCommand } from "./parse.ts";
+import { reproxyCommand } from "./reproxy.ts";
 
 type CommandHandler = (
     message: Message,
@@ -21,6 +22,8 @@ const commands: Record<string, CommandHandler> = {
     explain: explainCommand,
     autoproxy: autoproxyCommand,
     ap: autoproxyCommand,
+    reproxy: reproxyCommand,
+    rp: reproxyCommand,
     help: helpCommand
 };
 
diff --git a/src/commands/reproxy.ts b/src/commands/reproxy.ts
new file mode 100644
index 0000000..3878bf5
--- /dev/null
+++ b/src/commands/reproxy.ts
@@ -0,0 +1,35 @@
+/* src/commands/reproxy.ts */
+
+import type { Message } from "discord.js";
+
+import { reproxyProxied } from "../discord/reproxyMessage.ts";
+import { membersByName } from "../proxy/names.ts";
+import { system } from "../state/system.ts";
+
+export async function reproxyCommand(
+    message: Message,
+    args: string[],
+): Promise<void> {
+    if (args.length === 0) {
+        await message.reply("Use `d!reproxy <member>`, like `d!reproxy c1`. Several work too: `d!rp c1 c2`.");
+        return;
+    }
+
+    const { found, missing } = membersByName(system.members, args);
+
+    if (missing.length > 0) {
+        const names = missing.map((name) => `\`${name}\``).join(", ");
+        await message.reply(`No member called ${names}. Use their short name, like \`c1\`.`);
+        return;
+    }
+
+    const problem = await reproxyProxied(message, found);
+
+    if (problem) {
+        await message.reply(problem);
+        return;
+    }
+
+    // Tidy like edit: the command itself disappears
+    await message.delete();
+}
diff --git a/src/discord/editComponents.ts b/src/discord/editComponents.ts
index 9b7b7ad..ff6806a 100644
--- a/src/discord/editComponents.ts
+++ b/src/discord/editComponents.ts
@@ -55,16 +55,25 @@ function rewriteMedia(
     return component;
 }
 
+// Points media back at the attachments, so the components can be sent again
+export function keepMedia(
+    components: ComponentJson[],
+    attachmentNames: string[],
+): ComponentJson[] {
+    const names = new Set(attachmentNames);
+    return components.map((component) => rewriteMedia(component, names));
+}
+
 // Swaps the text block for new text, keeping buttons and attachments as they were
 export function replaceText(
     components: ComponentJson[],
     text: string,
     attachmentNames: string[],
 ): ComponentJson[] {
-    const names = new Set(attachmentNames);
-    const kept = components
-        .filter((component) => component.type !== ComponentType.TextDisplay)
-        .map((component) => rewriteMedia(component, names));
+    const kept = keepMedia(
+        components.filter((component) => component.type !== ComponentType.TextDisplay),
+        attachmentNames,
+    );
 
     if (!text) return kept;
 
diff --git a/src/discord/editMessage.ts b/src/discord/editMessage.ts
index eeb3d96..8d72fd0 100644
--- a/src/discord/editMessage.ts
+++ b/src/discord/editMessage.ts
@@ -12,10 +12,10 @@ import { replaceText } from "./editComponents.ts";
 import { isProxied } from "./ownership.ts";
 import { getWebhook } from "./webhooks.ts";
 
-const searchDepth = 50;
+export const searchDepth = 50;
 
 // The replied-to message if there is one, otherwise the newest proxied message here
-async function findTarget(command: Message): Promise<Message | null> {
+export async function findTarget(command: Message): Promise<Message | null> {
     if (command.reference?.type === MessageReferenceType.Default) {
         const replied = await command.fetchReference().catch(() => null);
         return replied && isProxied(replied) ? replied : null;
diff --git a/src/discord/reproxyMessage.ts b/src/discord/reproxyMessage.ts
new file mode 100644
index 0000000..8752aec
--- /dev/null
+++ b/src/discord/reproxyMessage.ts
@@ -0,0 +1,61 @@
+/* src/discord/reproxyMessage.ts */
+
+import {
+    type APIMessageTopLevelComponent,
+    MessageFlags,
+    type Message,
+} from "discord.js";
+
+import { webhookName } from "../proxy/names.ts";
+import type { Member } from "../proxy/types.ts";
+import { system } from "../state/system.ts";
+import { avatarFor } from "./avatar.ts";
+import { keepMedia } from "./editComponents.ts";
+import { findTarget, searchDepth } from "./editMessage.ts";
+import { rememberSenders } from "./senders.ts";
+import { getWebhook } from "./webhooks.ts";
+
+// Webhooks can't rename a sent message, so it is sent again and the old one removed.
+// Returns an error to show, or null when it went through
+export async function reproxyProxied(
+    command: Message,
+    members: Member[],
+): Promise<string | null> {
+    const target = await findTarget(command);
+    if (!target?.webhookId) {
+        return `No proxied message found. Reply to one, or send one in the last ${searchDepth} messages.`;
+    }
+
+    const webhook = await getWebhook(command);
+    if (!webhook) return "Can't use webhooks in this channel.";
+
+    const classic = !target.flags.has(MessageFlags.IsComponentsV2);
+    const attachmentNames = target.attachments.map((attachment) => attachment.name);
+
+    const sent = await webhook.send({
+        flags: classic ? undefined : MessageFlags.IsComponentsV2,
+        content: classic ? target.content : undefined,
+        components: classic
+            ? undefined
+            : keepMedia(
+                target.components.map((component) => component.toJSON()),
+                attachmentNames,
+            ) as APIMessageTopLevelComponent[],
+        files: target.attachments.map((attachment) => ({
+            attachment: attachment.url,
+            name: attachment.name,
+        })),
+        username: webhookName(members, system.tag),
+        avatarURL: await avatarFor(webhook, members),
+        threadId: command.channel.isThread() ? command.channel.id : undefined,
+        allowedMentions: {
+            parse: ["users"],
+        },
+    });
+
+    rememberSenders(sent.id, members);
+
+    // Delete only after the new copy is sent, so nothing is lost
+    await target.delete();
+    return null;
+}
diff --git a/src/proxy/names.ts b/src/proxy/names.ts
index 3cd90f0..5e34bd1 100644
--- a/src/proxy/names.ts
+++ b/src/proxy/names.ts
@@ -32,6 +32,29 @@ export function webhookName(members: Member[], tag: string | null): string {
         .slice(0, webhookNameLimit);
 }
 
+// Matches the short name, like "c1", not the display name; case doesn't matter
+export function membersByName(
+    members: Member[],
+    names: string[],
+): { found: Member[]; missing: string[] } {
+    const found: Member[] = [];
+    const missing: string[] = [];
+
+    for (const name of names) {
+        const match = members.find(
+            (member) => member.name.toLowerCase() === name.toLowerCase(),
+        );
+
+        if (match) found.push(match);
+        else missing.push(name);
+    }
+
+    return {
+        found,
+        missing,
+    };
+}
+
 // Custom status text is capped at 128 chars
 export function frontingStatus(fronters: Member[]): string {
     if (fronters.length === 0) return "Nobody fronting";
diff --git a/tests/proxy/names_test.ts b/tests/proxy/names_test.ts
index cb2d32e..e9d5e3a 100644
--- a/tests/proxy/names_test.ts
+++ b/tests/proxy/names_test.ts
@@ -2,7 +2,11 @@
 
 import { assertEquals } from "@std/assert";
 
-import { frontingStatus, webhookName } from "../../src/proxy/names.ts";
+import {
+    frontingStatus,
+    membersByName,
+    webhookName,
+} from "../../src/proxy/names.ts";
 import { member } from "../fixtures.ts";
 
 const host = {
@@ -53,3 +57,18 @@ Deno.test("fronting status lists display names", () => {
     );
     assertEquals(frontingStatus([]), "Nobody fronting");
 });
+
+Deno.test("membersByName matches short names, not display names", () => {
+    const named = {
+        ...host,
+        name: "C1",
+    };
+
+    assertEquals(
+        membersByName([named, member("02")], ["c1", "02", "clove (host)"]),
+        {
+            found: [named, member("02")],
+            missing: ["clove (host)"],
+        },
+    );
+});