commit
DM the owner when someone replies to a proxied message
Discord never pings for replies to webhook messages, so the bot sends a Components v2 card with the reply text and a jump link instead. Co-Authored-By: Sandrone <[email protected]>
diff --git a/src/discord/infoCard.ts b/src/discord/infoCard.ts
index c739eea..ce90aa9 100644
--- a/src/discord/infoCard.ts
+++ b/src/discord/infoCard.ts
@@ -6,12 +6,12 @@ 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;
+export const accentColour = 0xeb459e;
const maxDescription = 1000;
const linkButtonStyle = 5;
// deno-lint-ignore no-explicit-any
-type ComponentJson = Record<string, any>;
+export type ComponentJson = Record<string, any>;
export interface InfoCardInput {
members: Member[] | undefined;
@@ -32,13 +32,13 @@ function colourOf(color: string | null): number {
return /^[0-9a-fA-F]{6}$/.test(full) ? parseInt(full, 16) : accentColour;
}
-function shorten(text: string): string {
+export function shorten(text: string): string {
return text.length > maxDescription
? `${text.slice(0, maxDescription - 1)}…`
: text;
}
-function linkButton(label: string, url: string): ComponentJson {
+export function linkButton(label: string, url: string): ComponentJson {
return {
type: ComponentType.Button,
style: linkButtonStyle,
@@ -48,7 +48,7 @@ function linkButton(label: string, url: string): ComponentJson {
}
// A Section needs an accessory, so members without an avatar get plain text
-function header(text: string, avatarUrl: string | null): ComponentJson {
+export function header(text: string, avatarUrl: string | null): ComponentJson {
const display = {
type: ComponentType.TextDisplay,
content: text,
diff --git a/src/discord/replyCard.ts b/src/discord/replyCard.ts
new file mode 100644
index 0000000..803a04e
--- /dev/null
+++ b/src/discord/replyCard.ts
@@ -0,0 +1,57 @@
+/* src/discord/replyCard.ts */
+
+import { ComponentType } from "./editComponents.ts";
+import {
+ accentColour,
+ type ComponentJson,
+ header,
+ linkButton,
+ shorten,
+} from "./infoCard.ts";
+
+export interface ReplyCardInput {
+ replierName: string;
+ replierAvatarUrl: string;
+ proxiedName: string;
+ content: string;
+ hasAttachments: boolean;
+ replyUrl: string;
+}
+
+// Quote every line so a multi-line reply stays inside the quote
+function quote(text: string): string {
+ return text.split("\n").map((line) => `> ${line}`).join("\n");
+}
+
+export function buildReplyCard(input: ReplyCardInput): ComponentJson[] {
+ const body = input.content
+ ? quote(shorten(input.content))
+ : "*Sent an attachment*";
+
+ return [
+ {
+ type: ComponentType.Container,
+ accent_color: accentColour,
+ components: [
+ header(
+ `### ↪ ${input.replierName} replied to ${input.proxiedName}\n${body}`,
+ input.replierAvatarUrl,
+ ),
+ {
+ type: ComponentType.ActionRow,
+ components: [
+ linkButton("Jump to reply", input.replyUrl),
+ ],
+ },
+ ],
+ },
+ ...(input.content && input.hasAttachments
+ ? [
+ {
+ type: ComponentType.TextDisplay,
+ content: "-# Also sent an attachment",
+ },
+ ]
+ : []),
+ ];
+}
diff --git a/src/discord/replyNotice.ts b/src/discord/replyNotice.ts
new file mode 100644
index 0000000..d5fe43b
--- /dev/null
+++ b/src/discord/replyNotice.ts
@@ -0,0 +1,38 @@
+/* src/discord/replyNotice.ts */
+
+import {
+ type APIMessageTopLevelComponent,
+ type Message,
+ MessageFlags,
+ MessageReferenceType,
+} from "discord.js";
+
+import { ownerUserId } from "../config/env.ts";
+import { client } from "./client.ts";
+import { isProxied } from "./ownership.ts";
+import { buildReplyCard } from "./replyCard.ts";
+
+// Webhooks aren't accounts, so Discord never pings anyone for replies to them
+export async function notifyReply(message: Message): Promise<void> {
+ if (!message.inGuild() || message.webhookId || message.author.bot) return;
+ if (message.author.id === ownerUserId) return;
+ if (message.reference?.type !== MessageReferenceType.Default) return;
+
+ const target = await message.fetchReference().catch(() => null);
+ if (!target || !isProxied(target)) return;
+
+ const owner = await client.users.fetch(ownerUserId);
+ const components = buildReplyCard({
+ replierName: message.member?.displayName ?? message.author.displayName,
+ replierAvatarUrl: message.author.displayAvatarURL(),
+ proxiedName: target.author.username,
+ content: message.content,
+ hasAttachments: message.attachments.size > 0,
+ replyUrl: message.url,
+ });
+
+ await owner.send({
+ flags: MessageFlags.IsComponentsV2,
+ components: components as APIMessageTopLevelComponent[],
+ });
+}
diff --git a/src/main.ts b/src/main.ts
index 2794e7d..a59a5bc 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -11,6 +11,7 @@ import { discordToken } from "./config/env.ts";
import { client } from "./discord/client.ts";
import { proxyMessage } from "./discord/proxyMessage.ts";
import { handleReaction } from "./discord/reactions.ts";
+import { notifyReply } from "./discord/replyNotice.ts";
import { updateStatus } from "./discord/status.ts";
import { loadSettings } from "./state/settings.ts";
@@ -23,6 +24,10 @@ client.on(Events.MessageCreate, (message) => {
proxyMessage(message).catch((error) => {
console.error("Proxy failed:", error);
});
+
+ notifyReply(message).catch((error) => {
+ console.error("Reply notice failed:", error);
+ });
});
client.on(Events.MessageReactionAdd, (reaction, user) => {
diff --git a/tests/discord/replyCard_test.ts b/tests/discord/replyCard_test.ts
new file mode 100644
index 0000000..058326e
--- /dev/null
+++ b/tests/discord/replyCard_test.ts
@@ -0,0 +1,44 @@
+/* tests/discord/replyCard_test.ts */
+
+import { assertEquals, assertStringIncludes } from "@std/assert";
+
+import { buildReplyCard } from "../../src/discord/replyCard.ts";
+
+const base = {
+ replierName: "Ari",
+ replierAvatarUrl: "https://cdn.example/ari.png",
+ proxiedName: "Clove ~ 🌙",
+ content: "",
+ hasAttachments: false,
+ replyUrl: "https://discord.com/channels/1/2/3",
+};
+
+Deno.test("reply text is quoted line by line", () => {
+ const text = JSON.stringify(buildReplyCard({
+ ...base,
+ content: "hi\nthere",
+ }));
+
+ assertStringIncludes(text, "↪ Ari replied to Clove ~ 🌙\\n> hi\\n> there");
+ assertStringIncludes(text, "https://discord.com/channels/1/2/3");
+});
+
+Deno.test("attachment-only reply says so", () => {
+ const components = buildReplyCard({
+ ...base,
+ hasAttachments: true,
+ });
+
+ assertEquals(components.length, 1);
+ assertStringIncludes(JSON.stringify(components), "*Sent an attachment*");
+});
+
+Deno.test("text plus attachment adds a note", () => {
+ const components = buildReplyCard({
+ ...base,
+ content: "look",
+ hasAttachments: true,
+ });
+
+ assertEquals(components.length, 2);
+});