${
diff --git a/src/db/queries.ts b/src/db/queries.ts
index f4c2052a..93312804 100644
--- a/src/db/queries.ts
+++ b/src/db/queries.ts
@@ -4061,10 +4061,10 @@ export const adminRotateSubscriptionConfirmToken = async (
// Webhook endpoints + deliveries (migration 0006).
// -----------------------------------------------------------------------------
-export type WebhookAdapter = "generic" | "slack" | "discord" | "telegram";
+export type WebhookAdapter = "generic" | "slack" | "discord" | "telegram" | "discord_bot";
export const isWebhookAdapter = (v: unknown): v is WebhookAdapter =>
- v === "generic" || v === "slack" || v === "discord" || v === "telegram";
+ v === "generic" || v === "slack" || v === "discord" || v === "telegram" || v === "discord_bot";
export type WebhookEndpoint = {
id: string;
diff --git a/src/lib/discord.ts b/src/lib/discord.ts
index cfc48852..bea0f803 100644
--- a/src/lib/discord.ts
+++ b/src/lib/discord.ts
@@ -2,7 +2,7 @@
// a thin REST client and OpCard -> message rendering. No Discord SDK.
import type { Bindings } from "../index";
import { log } from "./log";
-import type { OpCard } from "./opbot/cards";
+import type { OpButton, OpCard } from "./opbot/cards";
import { encodePayload } from "./opbot/codec";
import {
escapeDiscordMentions,
@@ -141,6 +141,25 @@ export const discordApi = async (
}
};
+/** Discord action rows (β€ 5 rows of β€ 5 buttons) for an OpButton grid. */
+export const buttonRows = (
+ buttons: OpButton[][],
+ commentId: string,
+ extra: string[] = [],
+): unknown[] =>
+ buttons
+ .filter((row) => row.length > 0)
+ .slice(0, 5)
+ .map((row) => ({
+ type: 1,
+ components: row.slice(0, 5).map((b) => ({
+ type: 2,
+ style: STYLE[b.style],
+ label: b.label,
+ custom_id: encodePayload("discord", b.action, commentId, extra),
+ })),
+ }));
+
export const cardToMessage = (
card: OpCard,
extra: string[] = [],
@@ -165,21 +184,9 @@ export const cardToMessage = (
{ name: "Done by", value: escapeDiscordMentions(card.doneBy).slice(0, 1024) },
];
}
- const components = card.buttons
- .filter((row) => row.length > 0)
- .slice(0, 5)
- .map((row) => ({
- type: 1,
- components: row.slice(0, 5).map((b) => ({
- type: 2,
- style: STYLE[b.style],
- label: b.label,
- custom_id: encodePayload("discord", b.action, card.commentId, extra),
- })),
- }));
return {
embeds: [embed],
- components,
+ components: buttonRows(card.buttons, card.commentId, extra),
allowed_mentions: { parse: [] },
};
};
diff --git a/src/lib/webhook-adapters.ts b/src/lib/webhook-adapters.ts
index dcb8fca0..a4266666 100644
--- a/src/lib/webhook-adapters.ts
+++ b/src/lib/webhook-adapters.ts
@@ -33,6 +33,8 @@
*/
import type { CommentStatus } from "../db/queries";
import { getComment, getPost, getUser } from "../db/queries";
+import { buttonRows } from "./discord";
+import { buttonsFor } from "./opbot/cards";
import { moderationKeyboard } from "./telegram";
import type { WebhookEvent, WebhookPayload } from "./webhook";
@@ -246,13 +248,10 @@ export const renderSlackBody = async (
return JSON.stringify({ text });
};
-export const renderDiscordBody = async (
- db: WebhookAdapterDb,
+const discordEmbed = (
+ ctx: Ctx,
payload: WebhookPayload,
- opts: AdapterOpts = {},
-): Promise => {
- const ctx = await loadContext(db, payload, opts);
-
+): Record => {
// Wrap the destination in <β¦> so Discord treats it as a literal URL:
// a bare URL containing ")" (e.g. a "/wiki/Foo_(bar)" page link) would
// otherwise terminate the markdown link early. The URLs are already
@@ -277,7 +276,37 @@ export const renderDiscordBody = async (
embed.fields = [{ name: "Links", value: links.join(" Β· ") }];
}
- return JSON.stringify({ embeds: [embed] });
+ return embed;
+};
+
+export const renderDiscordBody = async (
+ db: WebhookAdapterDb,
+ payload: WebhookPayload,
+ opts: AdapterOpts = {},
+): Promise => {
+ const ctx = await loadContext(db, payload, opts);
+ return JSON.stringify({ embeds: [discordEmbed(ctx, payload)] });
+};
+
+// Bot-posted variant: the same embed plus moderation buttons. The bot token is
+// never part of the body (it is added to the request headers at send time).
+export const renderDiscordBotBody = async (
+ db: WebhookAdapterDb,
+ payload: WebhookPayload,
+ opts: AdapterOpts = {},
+): Promise => {
+ const ctx = await loadContext(db, payload, opts);
+ return JSON.stringify({
+ embeds: [discordEmbed(ctx, payload)],
+ components: buttonRows(
+ buttonsFor(ctx.status, {
+ reported: payload.event === "comment.reported",
+ banLabel: "banr",
+ }),
+ payload.comment_id,
+ ),
+ allowed_mentions: { parse: [] },
+ });
};
// ----------------------------- Telegram ------------------------------------
diff --git a/src/lib/webhook.ts b/src/lib/webhook.ts
index 8c9b54fd..9c80024f 100644
--- a/src/lib/webhook.ts
+++ b/src/lib/webhook.ts
@@ -52,10 +52,12 @@ import {
type WebhookEndpoint,
} from "../db/queries";
import { log } from "./log";
+import { DISCORD_API, discordEnabled } from "./discord";
import { checkOutboundUrl } from "./url-safety";
import {
type AdapterOpts,
renderDiscordBody,
+ renderDiscordBotBody,
renderSlackBody,
renderTelegramBody,
} from "./webhook-adapters";
@@ -90,6 +92,10 @@ type WebhookEnv = {
// Kept here (env secret) rather than in D1 so the token is never stored
// in plaintext alongside the (non-secret) chat id.
TELEGRAM_BOT_TOKEN?: string;
+ // Discord bot credentials. The `discord_bot` adapter posts to a channel id
+ // held in the endpoint's `url` column; the token is added at send time only.
+ DISCORD_BOT_TOKEN?: string;
+ DISCORD_PUBLIC_KEY?: string;
};
// Fixed, trusted Telegram Bot API host. Telegram endpoints target this URL
@@ -136,6 +142,7 @@ const renderBody = async (
if (adapter === "slack") return renderSlackBody(db, payload, opts);
if (adapter === "discord") return renderDiscordBody(db, payload, opts);
if (adapter === "telegram") return renderTelegramBody(db, payload, opts);
+ if (adapter === "discord_bot") return renderDiscordBotBody(db, payload, opts);
return renderGenericBody(payload);
};
@@ -161,6 +168,7 @@ const buildSyntheticEnvEndpoint = (url: string): WebhookEndpoint => {
};
const buildHeaders = async (
+ env: WebhookEnv,
endpoint: WebhookEndpoint,
body: string,
): Promise => {
@@ -168,6 +176,12 @@ const buildHeaders = async (
"content-type": "application/json",
"user-agent": "Garrul-Webhook/2.0",
};
+ // The bot token is added here, at send time, so it never lands in a stored
+ // delivery row. Discord authenticates by token; no HMAC header.
+ if (endpoint.adapter === "discord_bot") {
+ headers.authorization = `Bot ${env.DISCORD_BOT_TOKEN ?? ""}`;
+ return headers;
+ }
if (endpoint.secret) {
const sig = await signWebhookBody(endpoint.secret, body);
headers["x-garrul-signature"] = sig.header;
@@ -194,11 +208,19 @@ const resolveTarget = (
checkUrl: false,
};
}
+ if (endpoint.adapter === "discord_bot") {
+ if (!discordEnabled(env)) return null;
+ return {
+ url: `${DISCORD_API}/channels/${endpoint.url}/messages`,
+ // Fixed, trusted host; the channel id was validated as a snowflake on save.
+ checkUrl: false,
+ };
+ }
return { url: endpoint.url, checkUrl: true };
};
const isDiscordAdapter = (a: WebhookAdapter): boolean =>
- a === "discord";
+ a === "discord" || a === "discord_bot";
// Discord's retry_after: JSON body seconds (may be fractional), else the
// Retry-After header in seconds. Returns ms, or undefined when absent.
@@ -225,7 +247,16 @@ const postOnce = async (
retryAfterMs?: number;
}> => {
const target = resolveTarget(env, endpoint);
- if (!target) return { ok: false, status: 0, error: "telegram:no_token" };
+ if (!target) {
+ return {
+ ok: false,
+ status: 0,
+ error:
+ endpoint.adapter === "discord_bot"
+ ? "discord:not_configured"
+ : "telegram:no_token",
+ };
+ }
if (target.checkUrl) {
const safe = checkOutboundUrl(target.url, {
allowHttp: allowHttpFor(endpoint, env),
@@ -236,7 +267,7 @@ const postOnce = async (
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
try {
- const headers = await buildHeaders(endpoint, body);
+ const headers = await buildHeaders(env, endpoint, body);
const res = await fetch(target.url, {
method: "POST",
headers,
@@ -280,7 +311,10 @@ const postOnce = async (
// only ever placed in the telegram target URL, but fetch error messages can
// echo that URL back, so we strip the exact secret defensively.
const scrubToken = (s: string, env: WebhookEnv): string =>
- env.TELEGRAM_BOT_TOKEN ? s.split(env.TELEGRAM_BOT_TOKEN).join("***") : s;
+ [env.TELEGRAM_BOT_TOKEN, env.DISCORD_BOT_TOKEN].reduce(
+ (acc, t) => (t ? acc.split(t).join("***") : acc),
+ s,
+ );
const isDevEnv = (env: WebhookEnv): boolean => env.ENV === "dev";
diff --git a/src/routes/admin.ts b/src/routes/admin.ts
index 9233f10c..309c0db9 100644
--- a/src/routes/admin.ts
+++ b/src/routes/admin.ts
@@ -135,6 +135,7 @@ import {
import { renderUserDetail } from "../admin-ui/pages/user-detail";
import { renderUsers } from "../admin-ui/pages/users";
import { renderOperator } from "../admin-ui/pages/operator";
+import { SNOWFLAKE_RE, discordEnabled, validateDiscordChannel } from "../lib/discord";
import { renderTelegram } from "../admin-ui/pages/telegram";
import { issueTelegramLinkToken } from "./telegram";
import { renderSettings } from "../admin-ui/pages/settings";
@@ -144,6 +145,7 @@ import {
canonicalReactionKinds,
FLAG_KEYS,
isReactionKindsKey,
+ loadDiscordSettings,
loadNumbers,
loadSettings,
NUMBER_KEYS,
@@ -1226,10 +1228,12 @@ const isValidEvent = (v: unknown): v is (typeof VALID_EVENTS)[number] =>
const isValidTelegramChatId = (v: string): boolean =>
/^-?\d{1,20}$/.test(v) || /^@[A-Za-z][A-Za-z0-9_]{4,31}$/.test(v);
-const parseWebhookBody = (
+const parseWebhookBody = async (
body: WebhookBody,
env: Bindings,
-): { ok: true; fields: WebhookFields } | { ok: false; error: string } => {
+): Promise<
+ { ok: true; fields: WebhookFields } | { ok: false; error: string }
+> => {
if (typeof body.url !== "string" || body.url.length === 0) {
return { ok: false, error: "url_required" };
}
@@ -1247,6 +1251,21 @@ const parseWebhookBody = (
if (!isValidTelegramChatId(body.url)) {
return { ok: false, error: "chat_id_invalid" };
}
+ } else if (adapter === "discord_bot") {
+ // `url` holds a channel id; the real target is the fixed Discord API host.
+ if (!SNOWFLAKE_RE.test(body.url)) {
+ return { ok: false, error: "channel_id_invalid" };
+ }
+ const { guildId } = await loadDiscordSettings(env);
+ if (!discordEnabled(env) || !env.DISCORD_BOT_TOKEN || !guildId) {
+ return { ok: false, error: "discord_not_configured" };
+ }
+ const ch = await validateDiscordChannel(
+ env.DISCORD_BOT_TOKEN,
+ body.url,
+ guildId,
+ );
+ if (!ch.ok) return { ok: false, error: ch.error };
} else {
// allowHttp only in dev β production endpoints must be https to make
// the signing+secret guarantees meaningful end-to-end.
@@ -1302,7 +1321,7 @@ admin.post("/api/webhooks", async (c) => {
if (user instanceof Response) return user;
const body = await c.req.json().catch(() => null);
if (!body) return c.json({ error: "invalid_body" }, 400);
- const parsed = parseWebhookBody(body, c.env);
+ const parsed = await parseWebhookBody(body, c.env);
if (!parsed.ok) return c.json({ error: parsed.error }, 400);
// On create there is nothing to preserve, so "absent" collapses to "unsigned".
const created = await createWebhookEndpoint(c.env.DB, {
@@ -1334,7 +1353,7 @@ admin.patch("/api/webhooks/:id", async (c) => {
if (!existing) return c.json({ error: "not_found" }, 404);
const body = await c.req.json().catch(() => null);
if (!body) return c.json({ error: "invalid_body" }, 400);
- const parsed = parseWebhookBody(body, c.env);
+ const parsed = await parseWebhookBody(body, c.env);
if (!parsed.ok) return c.json({ error: parsed.error }, 400);
await updateWebhookEndpoint(c.env.DB, id, parsed.fields);
await adminInsertAudit(c.env.DB, {
diff --git a/tests/discord-bot-adapter.test.ts b/tests/discord-bot-adapter.test.ts
new file mode 100644
index 00000000..f8072c36
--- /dev/null
+++ b/tests/discord-bot-adapter.test.ts
@@ -0,0 +1,167 @@
+/**
+ * The discord_bot webhook adapter, against REAL SQLite with a stubbed fetch:
+ * rendered body, admin save validation, delivery request, token hygiene.
+ */
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { buttonsFor } from "../src/lib/opbot/cards";
+import { dispatchWebhook, runWebhookRetries } from "../src/lib/webhook";
+import { renderDiscordBotBody } from "../src/lib/webhook-adapters";
+import { MOD_ID, adminHarness } from "./helpers/admin-sqlite";
+
+const CHANNEL = "666666666666666666";
+const GUILD = "777777777777777777";
+const TOKEN = "fake-bot-token-xyz";
+const POST_URL = `https://discord.com/api/v10/channels/${CHANNEL}/messages`;
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+});
+
+const setup = () => {
+ const h = adminHarness();
+ const env = h.env as unknown as Record;
+ const enable = () => {
+ env.DISCORD_BOT_TOKEN = TOKEN;
+ env.DISCORD_PUBLIC_KEY = "ab".repeat(32);
+ h.sqlite
+ .prepare("INSERT OR REPLACE INTO settings (key, value, updated_at) VALUES ('discord_guild_id', ?, 0)")
+ .run(GUILD);
+ };
+ h.sqlite.prepare("INSERT INTO posts (slug, title, url, created_at) VALUES ('p', 'Post', NULL, 1)").run();
+ h.sqlite
+ .prepare(
+ `INSERT INTO comments (id, post_slug, parent_id, user_id, body_md, body_html,
+ renderer_version, status, ip_hash, user_agent, created_at, depth)
+ VALUES ('c1', 'p', NULL, ?, 'hello @everyone', '
hello
', 1, 'pending', NULL, NULL, 1, 1)`,
+ )
+ .run(MOD_ID);
+ return { ...h, env, enable };
+};
+
+const PAYLOAD = {
+ event: "comment.posted" as const,
+ comment_id: "c1",
+ post_slug: "p",
+ user_id: MOD_ID,
+ ts: 1,
+};
+
+const addEndpoint = (h: ReturnType, adapter = "discord_bot") =>
+ h.sqlite
+ .prepare(
+ `INSERT INTO webhook_endpoints (id, url, adapter, enabled, fail_count, created_at, updated_at)
+ VALUES ('e1', ?, ?, 1, 0, 1, 1)`,
+ )
+ .run(CHANNEL, adapter);
+
+describe("renderDiscordBotBody", () => {
+ it("is the embed plus moderation buttons and no mention parsing", async () => {
+ const h = setup();
+ const body = JSON.parse(await renderDiscordBotBody(h.env.DB as never, PAYLOAD, {}));
+ expect(body.embeds).toHaveLength(1);
+ expect(body.allowed_mentions).toEqual({ parse: [] });
+ const expected = buttonsFor("pending", { reported: false, banLabel: "banr" });
+ expect(body.components).toHaveLength(expected.length);
+ const first = body.components[0];
+ expect(first.type).toBe(1);
+ expect(first.components[0]).toMatchObject({ type: 2 });
+ expect(first.components[0].label).toBe(expected[0]?.[0]?.label);
+ expect(first.components[0].custom_id).toContain("c1");
+ expect(JSON.stringify(body)).not.toContain("hello @everyone");
+ });
+});
+
+describe("saving a discord_bot endpoint", () => {
+ const save = (h: ReturnType, url: string) =>
+ h.request("/admin/api/webhooks", { method: "POST", body: { url, adapter: "discord_bot" } });
+
+ it("rejects a non-snowflake id and an unconfigured feature", async () => {
+ const h = setup();
+ const bad = await save(h, "not-an-id");
+ expect(bad.status).toBe(400);
+ const off = await save(h, CHANNEL);
+ expect(off.status).toBe(400);
+ expect(await off.json()).toEqual({ error: "discord_not_configured" });
+ });
+
+ it("maps channel validation failures and accepts a good channel", async () => {
+ const h = setup();
+ h.enable();
+ const channel = (body: unknown, status = 200) =>
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => new Response(JSON.stringify(body), { status })),
+ );
+ channel({}, 404);
+ expect(await (await save(h, CHANNEL)).json()).toEqual({ error: "channel_not_found" });
+ channel({ type: 0, guild_id: "888888888888888888" });
+ expect(await (await save(h, CHANNEL)).json()).toEqual({ error: "channel_wrong_guild" });
+ channel({ type: 2, guild_id: GUILD });
+ expect(await (await save(h, CHANNEL)).json()).toEqual({ error: "channel_wrong_type" });
+ channel({ type: 0, guild_id: GUILD });
+ const ok = await save(h, CHANNEL);
+ expect(ok.status).toBe(200);
+ expect(h.sqlite.prepare("SELECT adapter, url FROM webhook_endpoints").get()).toEqual({
+ adapter: "discord_bot",
+ url: CHANNEL,
+ });
+ });
+});
+
+describe("delivery", () => {
+ it("posts with the bot token header and never stores the token", async () => {
+ const h = setup();
+ h.enable();
+ addEndpoint(h);
+ const fetchMock = vi.fn(async () => new Response("", { status: 500 }));
+ vi.stubGlobal("fetch", fetchMock);
+ await dispatchWebhook(h.env as never, PAYLOAD);
+ const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
+ expect(url).toBe(POST_URL);
+ const headers = init.headers as Record;
+ expect(headers.authorization).toBe(`Bot ${TOKEN}`);
+ expect(headers["x-garrul-signature"]).toBeUndefined();
+ const row = h.sqlite.prepare("SELECT payload FROM webhook_deliveries").get() as { payload: string };
+ expect(row.payload).not.toContain(TOKEN);
+ expect(JSON.parse(row.payload).components).toBeDefined();
+ });
+
+ it("fails with discord:not_configured when the feature is off", async () => {
+ const h = setup();
+ addEndpoint(h);
+ h.sqlite
+ .prepare(
+ `INSERT INTO webhook_deliveries (id, endpoint_id, event, payload, status, attempts, next_attempt_at, created_at)
+ VALUES ('d1', 'e1', 'comment.posted', '{}', 'pending', 0, 1, 1)`,
+ )
+ .run();
+ const fetchMock = vi.fn();
+ vi.stubGlobal("fetch", fetchMock);
+ await runWebhookRetries(h.env as never);
+ expect(fetchMock).not.toHaveBeenCalled();
+ expect(h.sqlite.prepare("SELECT last_error FROM webhook_deliveries").get()).toEqual({
+ last_error: "discord:not_configured",
+ });
+ });
+
+ it("keeps the token out of logged error strings", async () => {
+ const h = setup();
+ h.enable();
+ addEndpoint(h);
+ const lines: string[] = [];
+ const grab = (l: unknown) => lines.push(String(l));
+ vi.spyOn(console, "log").mockImplementation(grab);
+ vi.spyOn(console, "warn").mockImplementation(grab);
+ vi.spyOn(console, "error").mockImplementation(grab);
+ vi.stubGlobal(
+ "fetch",
+ vi.fn(async () => {
+ throw new Error(`boom Bot ${TOKEN}`);
+ }),
+ );
+ await dispatchWebhook(h.env as never, PAYLOAD);
+ expect(lines.length).toBeGreaterThan(0);
+ expect(lines.join("\n")).not.toContain(TOKEN);
+ });
+});
diff --git a/tests/webhook-retries-realdb.test.ts b/tests/webhook-retries-realdb.test.ts
index 571e10c4..dec2ee24 100644
--- a/tests/webhook-retries-realdb.test.ts
+++ b/tests/webhook-retries-realdb.test.ts
@@ -113,7 +113,7 @@ describe("Discord 429 handling", () => {
return { row, ep, before };
};
- it.each(["discord"])("%s: reschedules at max(backoff, retry_after), one attempt", async (adapter) => {
+ it.each(["discord", "discord_bot"])("%s: reschedules at max(backoff, retry_after), one attempt", async (adapter) => {
const { row, before } = await run(adapter, 0);
expect(row.status).toBe("pending");
expect(row.attempts).toBe(1);
@@ -121,7 +121,7 @@ describe("Discord 429 handling", () => {
expect(row.next_attempt_at).toBeGreaterThanOrEqual(before + (RETRY_SCHEDULE_MS[1] as number));
});
- it.each(["discord"])("%s: a final 429 never raises fail_count", async (adapter) => {
+ it.each(["discord", "discord_bot"])("%s: a final 429 never raises fail_count", async (adapter) => {
const { row, ep } = await run(adapter, RETRY_SCHEDULE_MS.length - 1);
expect(row.status).toBe("giveup");
expect(ep).toEqual({ enabled: 1, fail_count: 9 });
From 7bb43b005fbe1c2b77ca5957b870ba28f249fb95 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Fri, 13 Nov 2026 23:50:57 -0500
Subject: [PATCH 15/36] feat(telegram): ban with reason, /unban, and ban state
in /user
---
src/lib/telegram.ts | 30 ++++++--
src/routes/telegram.ts | 114 ++++++++++++++++++++++++++++--
tests/opbot-codec.test.ts | 6 +-
tests/telegram.test.ts | 144 ++++++++++++++++++++++++++++++++++++++
4 files changed, 284 insertions(+), 10 deletions(-)
diff --git a/src/lib/telegram.ts b/src/lib/telegram.ts
index 075616ef..510d4cf7 100644
--- a/src/lib/telegram.ts
+++ b/src/lib/telegram.ts
@@ -52,6 +52,7 @@ export type TgMessage = {
chat: TgChat;
from?: TgUser;
text?: string;
+ reply_to_message?: TgMessage;
};
export type TgCallbackQuery = {
@@ -121,11 +122,18 @@ const TG_HANDLED: readonly OpAction[] = [
"delete",
"restore",
"ban",
+ "banr",
"resolve",
"rv",
"nx",
];
+// The bot's own user id is the numeric part of its token (":").
+export const botIdFromToken = (token: string): number | null => {
+ const n = Number(token.split(":")[0]);
+ return Number.isInteger(n) && n > 0 ? n : null;
+};
+
export const decodeCallback = (
data: string | undefined,
): { action: TgAction; commentId: string } | null => {
@@ -153,10 +161,16 @@ export const toInlineKeyboard = (
extra?: string[],
): { inline_keyboard: InlineButton[][] } => ({
inline_keyboard: rows.map((r) =>
- r.map((b) => ({
- text: b.label,
- callback_data: encodePayload("telegram", b.action, commentId, extra),
- })),
+ r.flatMap((b) => {
+ const button = (action: OpButton["action"], text: string) => ({
+ text,
+ callback_data: encodePayload("telegram", action, commentId, extra),
+ });
+ // Telegram-only: a typed ban reason sits right after plain Ban.
+ return b.action === "ban"
+ ? [button("ban", b.label), button("banr", "β Ban with reason")]
+ : [button(b.action, b.label)];
+ }),
),
});
@@ -214,7 +228,13 @@ export const sendMessage = (
chatId: string | number,
text: string,
opts: {
- replyMarkup?: { inline_keyboard: InlineButton[][] };
+ replyMarkup?:
+ | { inline_keyboard: InlineButton[][] }
+ | {
+ force_reply: true;
+ selective: true;
+ input_field_placeholder?: string;
+ };
disablePreview?: boolean;
} = {},
): Promise =>
diff --git a/src/routes/telegram.ts b/src/routes/telegram.ts
index 554ec54e..5b3c82ca 100644
--- a/src/routes/telegram.ts
+++ b/src/routes/telegram.ts
@@ -28,10 +28,10 @@ import {
import { t } from "../i18n";
import type { Bindings } from "../index";
import { log } from "../lib/log";
-import { hasRole, requiredRole, runOpAction } from "../lib/opbot/actions";
+import { hasRole, requiredRole, runOpAction, unbanUser } from "../lib/opbot/actions";
import { buttonsFor, type OpCard, renderQueueCard } from "../lib/opbot/cards";
import { decodePayload } from "../lib/opbot/codec";
-import { resolveTelegramActor } from "../lib/opbot/identity";
+import { type OpActor, resolveTelegramActor } from "../lib/opbot/identity";
import { consumeLinkToken, issueLinkToken } from "../lib/opbot/link-token";
import {
readComment,
@@ -43,6 +43,7 @@ import {
import { checkRateLimit } from "../lib/ratelimit";
import {
answerCallbackQuery,
+ botIdFromToken,
decodeCallback,
editMessageText,
SECRET_HEADER,
@@ -184,6 +185,18 @@ const handleCallback = async (
return;
}
const extra = decodePayload(cq.data)?.extra ?? [];
+ if (decoded.action === "banr") {
+ await answerCallbackQuery(token, cq.id);
+ if (cq.message) {
+ await sendMessage(
+ token,
+ cq.message.chat.id,
+ `Ban reason for comment ${escapeTg(decoded.commentId)} β send the reason as a reply to this message.`,
+ { replyMarkup: { force_reply: true, selective: true } },
+ );
+ }
+ return;
+ }
if (decoded.action === "rv" || decoded.action === "nx") {
await answerCallbackQuery(token, cq.id);
const next = await nextPendingAfter(
@@ -301,6 +314,57 @@ const parseCommand = (text: string): { name: string; arg: string } | null => {
return { name, arg };
};
+// A reply to one of the bot's force_reply prompts. "Reply to" prompts join
+// this regex in a later task.
+const PROMPT_RE = /^(Ban reason for) comment ([0-9A-HJKMNP-TV-Z]{26})\b/;
+
+const handlePromptAnswer = async (
+ env: Bindings,
+ token: string,
+ msg: TgMessage,
+ prompt: RegExpMatchArray,
+ text: string,
+): Promise => {
+ const chatId = msg.chat.id;
+ const who = msg.from
+ ? await resolveTelegramActor(env.DB, String(msg.from.id))
+ : ({ ok: false, reason: "not_linked" } as const);
+ if (!who.ok) {
+ await sendMessage(
+ token,
+ chatId,
+ t(who.reason === "not_linked" ? "telegram.not_linked" : "telegram.not_authorized"),
+ );
+ return;
+ }
+ const { actor } = who;
+ if (!hasRole(actor, requiredRole("banr"))) {
+ await sendMessage(token, chatId, t("telegram.not_authorized"));
+ return;
+ }
+ const res = await runOpAction({
+ env,
+ executionCtx: { waitUntil() {} },
+ actor,
+ action: "ban",
+ commentId: prompt[2] as string,
+ reason: text.slice(0, 500),
+ });
+ await sendMessage(
+ token,
+ chatId,
+ res.ok
+ ? `${escapeTg(res.doneBy)}`
+ : t(
+ res.error === "comment_not_found"
+ ? "telegram.comment_not_found"
+ : res.error === "not_authorized"
+ ? "telegram.not_authorized"
+ : "telegram.ban_refused",
+ ),
+ );
+};
+
const handleMessage = async (
env: Bindings,
token: string,
@@ -308,6 +372,18 @@ const handleMessage = async (
): Promise => {
const text = (msg.text ?? "").trim();
const chatId = msg.chat.id;
+
+ // Prompt answers come first: only a reply to one of the bot's own prompts
+ // counts, and the actor is re-resolved from the sender, never the prompt.
+ const replied = msg.reply_to_message;
+ const botId = botIdFromToken(token);
+ if (replied?.from?.id !== undefined && replied.from.id === botId) {
+ const prompt = (replied.text ?? "").match(PROMPT_RE);
+ if (prompt) {
+ if (text) await handlePromptAnswer(env, token, msg, prompt, text);
+ return;
+ }
+ }
const cmd = parseCommand(text);
// Linking is the one command that works before a link exists.
@@ -355,6 +431,9 @@ const handleMessage = async (
case "user":
await sendMessage(token, chatId, await renderUserText(env, cmd.arg));
break;
+ case "unban":
+ await sendMessage(token, chatId, await runUnban(env, who.actor, cmd.arg));
+ break;
default:
await sendMessage(token, chatId, commandHelpText());
}
@@ -366,6 +445,26 @@ const handleMessage = async (
// HTML); like the slack/discord adapters, the fixed framing is composed inline
// rather than via t() β only the gate messages use the string table.
+const runUnban = async (
+ env: Bindings,
+ actor: OpActor,
+ arg: string,
+): Promise => {
+ const id = arg.trim();
+ if (!id) return "Usage: /unban <id>";
+ if (!hasRole(actor, requiredRole("unban"))) return t("telegram.not_authorized");
+ const res = await unbanUser({ env, actor, userId: id });
+ if (!res.ok) {
+ return res.error === "not_found"
+ ? "No such user."
+ : res.error === "target_is_staff"
+ ? "You can't unban a moderator or admin with a Discord role grant."
+ : t("telegram.not_authorized");
+ }
+ const user = await getUser(env.DB, id);
+ return `Unbanned ${escapeTg(user?.name ?? id)}.`;
+};
+
const commandHelpText = (): string =>
[
"Garrul operator bot",
@@ -445,13 +544,20 @@ const renderUserText = async (env: Bindings, arg: string): Promise => {
if (!id) return "Usage: /user <id>";
const r = await readUser(env, id);
if (!r) return "No such user.";
- const { user } = r;
+ const { user, ban } = r;
return [
`User${escapeTg(id)}`,
`Name: ${escapeTg(user.name)}`,
- `Role: ${escapeTg(user.role)}${user.is_banned ? " Β· β banned" : ""}`,
+ `Role: ${escapeTg(user.role)}`,
`Provider: ${escapeTg(user.provider)}`,
`Joined: ${relativeAge(user.created_at, Date.now())} ago`,
+ !ban.banned
+ ? "Not banned"
+ : ban.purged
+ ? "Banned β reason no longer available"
+ : ban.reason
+ ? `Banned β ${escapeTg(ban.reason)}`
+ : "Banned β no reason given",
].join("\n");
};
diff --git a/tests/opbot-codec.test.ts b/tests/opbot-codec.test.ts
index 9f3f0803..b7704675 100644
--- a/tests/opbot-codec.test.ts
+++ b/tests/opbot-codec.test.ts
@@ -64,7 +64,11 @@ describe("opbot codec", () => {
describe("decodeCallback allowlist", () => {
it("only decodes actions the Telegram route handles", () => {
expect(decodeCallback(`g:reply:${U}`)).toBeNull();
- expect(decodeCallback(`g:banr:${U}`)).toBeNull();
+ expect(decodeCallback(`g:rc:${U}`)).toBeNull();
+ expect(decodeCallback(`g:banr:${U}`)).toEqual({
+ action: "banr",
+ commentId: U,
+ });
expect(decodeCallback(`g:sr:${U}:${P}`)).toBeNull();
expect(decodeCallback(`g:approve:${U}`)).toEqual({
action: "approve",
diff --git a/tests/telegram.test.ts b/tests/telegram.test.ts
index 25b92b96..79768c65 100644
--- a/tests/telegram.test.ts
+++ b/tests/telegram.test.ts
@@ -970,3 +970,147 @@ describe("queue review", () => {
expect(lastEdit()).toBeUndefined();
});
});
+
+describe("POST /telegram/webhook β ban reason, /unban and /user ban state", () => {
+ const ADMIN = "01HADMIN00000000000000000B";
+ const MOD = "01HMOD0000000000000000000C";
+ const AUTHOR = "01HAUTHOR000000000000000AB";
+ const BOT_ID = 123; // from the "123:ABC" test token
+ const PROMPT = (id: string) =>
+ `Ban reason for comment ${id} β send the reason as a reply to this message.`;
+ const replies = () => tgCalls.filter((c) => c.method === "sendMessage");
+ const lastText = () => String(replies().at(-1)?.body.text);
+ const answer = (tgUserId: string, text: string, promptText: string, fromId = BOT_ID) =>
+ post(
+ { ...mkEnv(), SESSIONS: openKv() } as unknown as Bindings,
+ {
+ update_id: 20,
+ message: {
+ message_id: 30,
+ chat: { id: 555 },
+ from: { id: Number(tgUserId) },
+ text,
+ reply_to_message: { message_id: 29, chat: { id: 555 }, from: { id: fromId }, text: promptText },
+ },
+ },
+ SECRET,
+ );
+ const say = (tgUserId: string, text: string) =>
+ post(
+ mkEnv(),
+ { update_id: 21, message: { message_id: 31, chat: { id: 555 }, from: { id: Number(tgUserId) }, text } },
+ SECRET,
+ );
+ const isBanned = (id: string) =>
+ (sqlite.prepare("SELECT is_banned FROM users WHERE id = ?").get(id) as { is_banned: number }).is_banned;
+ const banReason = () =>
+ (sqlite.prepare("SELECT reason FROM audit_log WHERE action = 'ban'").get() as { reason: string | null } | undefined)
+ ?.reason;
+
+ beforeEach(async () => {
+ seedUser(ADMIN, "admin", "Boss");
+ seedUser(MOD, "mod");
+ await linkOperator("42", ADMIN);
+ await linkOperator("43", MOD);
+ });
+
+ it("ban reason: an admin banr tap sends a force_reply prompt", async () => {
+ const id = await seedComment("approved");
+ await post(
+ mkEnv(),
+ {
+ update_id: 22,
+ callback_query: {
+ id: "cb",
+ from: { id: 42 },
+ message: { message_id: 10, chat: { id: 555 }, text: "alert" },
+ data: encodeCallback("banr", id),
+ },
+ },
+ SECRET,
+ );
+ expect(lastText()).toBe(
+ `Ban reason for comment ${id} β send the reason as a reply to this message.`,
+ );
+ expect(replies().at(-1)?.body.reply_markup).toEqual({ force_reply: true, selective: true });
+ expect(tgCalls.find((c) => c.method === "answerCallbackQuery")?.body.text).toBeUndefined();
+ });
+
+ it("ban reason: a mod banr tap is refused with a toast and no prompt", async () => {
+ const id = await seedComment("approved");
+ await post(
+ mkEnv(),
+ {
+ update_id: 22,
+ callback_query: { id: "cb", from: { id: 43 }, message: { message_id: 10, chat: { id: 555 } }, data: encodeCallback("banr", id) },
+ },
+ SECRET,
+ );
+ expect(replies()).toHaveLength(0);
+ expect(tgCalls.find((c) => c.method === "answerCallbackQuery")?.body.text).toBe(
+ "Your account doesn't have moderation access.",
+ );
+ });
+
+ it("ban reason: an admin's reply bans the author with the trimmed, capped text", async () => {
+ const id = await seedComment("approved");
+ await answer("42", ` ${"x".repeat(600)} `, PROMPT(id));
+ expect(isBanned(AUTHOR)).toBe(1);
+ expect(banReason()).toBe("x".repeat(500));
+ expect(lastText()).toBe("Banned by Boss");
+ });
+
+ it("ban reason: a reply to a message not from the bot is ignored as a prompt answer", async () => {
+ const id = await seedComment("approved");
+ await answer("42", "spam farm", PROMPT(id), 999);
+ expect(isBanned(AUTHOR)).toBe(0);
+ expect(auditCount("ban")).toBe(0);
+ });
+
+ it("ban reason: a mod's reply is refused and nothing is banned", async () => {
+ const id = await seedComment("approved");
+ await answer("43", "spam farm", PROMPT(id));
+ expect(lastText()).toBe("Your account doesn't have moderation access.");
+ expect(isBanned(AUTHOR)).toBe(0);
+ expect(auditCount("ban")).toBe(0);
+ });
+
+ it("ban reason: the staff and self guards still run on a prompt answer", async () => {
+ const id = await seedComment("approved");
+ sqlite.prepare("UPDATE comments SET user_id = ? WHERE id = ?").run(ADMIN, id);
+ await answer("42", "oops", PROMPT(id));
+ expect(isBanned(ADMIN)).toBe(0);
+ expect(lastText()).toBe("Not banned: that is you, or the last admin who can still sign in.");
+ });
+
+ it("unban: an admin lifts a ban and it is audited; a mod is refused", async () => {
+ sqlite.prepare("UPDATE users SET is_banned = 1 WHERE id = ?").run(AUTHOR);
+ await say("43", `/unban ${AUTHOR}`);
+ expect(lastText()).toBe("Your account doesn't have moderation access.");
+ expect(isBanned(AUTHOR)).toBe(1);
+ await say("42", "/unban");
+ expect(lastText()).toBe("Usage: /unban <id>");
+ await say("42", "/unban 01HNOPE");
+ expect(lastText()).toBe("No such user.");
+ await say("42", `/unban ${AUTHOR}`);
+ expect(lastText()).toBe("Unbanned Author.");
+ expect(isBanned(AUTHOR)).toBe(0);
+ expect(auditCount("unban")).toBe(1);
+ });
+
+ it("/user shows the ban reason, no reason, purged audit, or not banned", async () => {
+ await say("42", `/user ${AUTHOR}`);
+ expect(lastText()).toContain("\nNot banned");
+ expect(lastText()).not.toContain("β");
+ const id = await seedComment("approved");
+ await answer("42", "a reason", PROMPT(id));
+ await say("42", `/user ${AUTHOR}`);
+ expect(lastText()).toContain("Banned β a <b> reason");
+ sqlite.prepare("UPDATE audit_log SET reason = NULL WHERE action = 'ban'").run();
+ await say("42", `/user ${AUTHOR}`);
+ expect(lastText()).toContain("Banned β no reason given");
+ sqlite.prepare("DELETE FROM audit_log").run();
+ await say("42", `/user ${AUTHOR}`);
+ expect(lastText()).toContain("Banned β reason no longer available");
+ });
+});
From fd4da0462080510b5fbdcfd659f0e9b8eb8f5df6 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Fri, 13 Nov 2026 23:52:08 -0500
Subject: [PATCH 16/36] fix(telegram): break the cards/readouts import cycle
that bypassed the webhook mock
---
src/lib/opbot/cards.ts | 10 +++++++++-
src/lib/opbot/readouts.ts | 11 +++--------
2 files changed, 12 insertions(+), 9 deletions(-)
diff --git a/src/lib/opbot/cards.ts b/src/lib/opbot/cards.ts
index ecb94bb1..fe76b40a 100644
--- a/src/lib/opbot/cards.ts
+++ b/src/lib/opbot/cards.ts
@@ -6,7 +6,6 @@ import {
} from "../../db/queries";
import type { Bindings } from "../../index";
import type { OpAction } from "./codec";
-import { relativeAge } from "./readouts";
export type OpButton = {
action: OpAction;
@@ -25,6 +24,15 @@ export type OpCard = {
buttons: OpButton[][];
};
+export const relativeAge = (since: number, now: number): string => {
+ const ms = Math.max(0, now - since);
+ const mins = Math.floor(ms / 60000);
+ if (mins < 60) return `${mins}m`;
+ const hrs = Math.floor(mins / 60);
+ if (hrs < 24) return `${hrs}h`;
+ return `${Math.floor(hrs / 24)}d`;
+};
+
export const buttonsFor = (
status: CommentStatus | null | undefined,
opts: { reported: boolean; queue?: boolean; banLabel?: "ban" | "banr" },
diff --git a/src/lib/opbot/readouts.ts b/src/lib/opbot/readouts.ts
index a05c0f29..fc9225b9 100644
--- a/src/lib/opbot/readouts.ts
+++ b/src/lib/opbot/readouts.ts
@@ -14,14 +14,9 @@ import {
import type { Bindings } from "../../index";
import { banState } from "./actions";
-export const relativeAge = (since: number, now: number): string => {
- const ms = Math.max(0, now - since);
- const mins = Math.floor(ms / 60000);
- if (mins < 60) return `${mins}m`;
- const hrs = Math.floor(mins / 60);
- if (hrs < 24) return `${hrs}h`;
- return `${Math.floor(hrs / 24)}d`;
-};
+// relativeAge lives in cards.ts: cards must not import this file (cycle through
+// actions -> moderation -> webhook -> webhook-adapters -> cards).
+export { relativeAge } from "./cards";
export const readQueue = async (env: Bindings) => {
const [stats, oldest, reports] = await Promise.all([
From 3e02ffbbbdf71672cc66345c20962ad9efeeb1d0 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Fri, 13 Nov 2026 23:52:43 -0500
Subject: [PATCH 17/36] feat(admin): Discord settings, linking and command
registration page
---
src/admin-ui/icons.ts | 2 +
src/admin-ui/layout.ts | 1 +
src/admin-ui/pages/discord.ts | 193 +++++++++++++++++++++++++++++
src/db/queries.ts | 4 +
src/routes/admin.ts | 155 ++++++++++++++++++++++-
tests/admin-discord.test.ts | 223 ++++++++++++++++++++++++++++++++++
6 files changed, 577 insertions(+), 1 deletion(-)
create mode 100644 src/admin-ui/pages/discord.ts
create mode 100644 tests/admin-discord.test.ts
diff --git a/src/admin-ui/icons.ts b/src/admin-ui/icons.ts
index bb28b7d2..2d2aafb2 100644
--- a/src/admin-ui/icons.ts
+++ b/src/admin-ui/icons.ts
@@ -33,6 +33,8 @@ const ICON_PATHS: Record = {
copy: '',
telegram:
'',
+ discord:
+ '',
chevron: '',
};
diff --git a/src/admin-ui/layout.ts b/src/admin-ui/layout.ts
index 90404376..df81f8ff 100644
--- a/src/admin-ui/layout.ts
+++ b/src/admin-ui/layout.ts
@@ -162,6 +162,7 @@ export const layout = (
: []),
{ href: "/admin/operator", label: "Operator", icon: "operator" },
{ href: "/admin/telegram", label: "Telegram", icon: "telegram" },
+ { href: "/admin/discord", label: "Discord", icon: "discord" },
{ href: "/admin/settings", label: "Settings", icon: "settings" },
]
: [];
diff --git a/src/admin-ui/pages/discord.ts b/src/admin-ui/pages/discord.ts
new file mode 100644
index 00000000..88669eed
--- /dev/null
+++ b/src/admin-ui/pages/discord.ts
@@ -0,0 +1,193 @@
+import { escapeHtml } from "../escape";
+
+export type DiscordPageData = {
+ /** DISCORD_BOT_TOKEN and DISCORD_PUBLIC_KEY are both set. */
+ configured: boolean;
+ botTokenSet: boolean;
+ publicKeySet: boolean;
+ clientIdSet: boolean;
+ /** `${PUBLIC_BASE_URL}/discord/interactions`, or null when the base URL is unset. */
+ endpointUrl: string | null;
+ settings: { guildId: string; modRoleId: string; adminRoleId: string; digestChannelId: string };
+ /** The viewing admin's existing link, if any. */
+ link: { linked_at: number } | null;
+};
+
+const formatTs = (ts: number): string =>
+ new Date(ts).toISOString().slice(0, 16).replace("T", " ");
+
+const pill = (set: boolean, missing = "spam"): string =>
+ set ? 'set' : `missing`;
+
+const setupCard = (d: DiscordPageData): string => `
+
+
Bot configuration
+
+ The Discord bot is configured with Worker secrets, not in this panel.
+ Paste the endpoint URL below into the Discord developer portal as the
+ Interactions Endpoint URL.
+
+
+
+
DISCORD_BOT_TOKEN
${pill(d.botTokenSet)}
+
DISCORD_PUBLIC_KEY
${pill(d.publicKeySet)}
+
DISCORD_CLIENT_ID
${pill(d.clientIdSet, "pending")}
+
Application id, needed to register commands.
+
+
+
Interactions endpoint URL:
+ ${
+ d.endpointUrl
+ ? `${escapeHtml(d.endpointUrl)}`
+ : `Set PUBLIC_BASE_URL to see the endpoint URL.`
+ }
+
+ ${
+ d.configured
+ ? ""
+ : `
Set DISCORD_BOT_TOKEN and
+ DISCORD_PUBLIC_KEY via wrangler secret put
+ to enable the bot.
+ Connect Discord to moderate comments from your server. Alerts are posted by
+ a webhook endpoint with the Discord bot adapter;
+ this page sets up the server, links your account and registers slash commands.
+
",
+ renderer_version: 1,
+ status,
+ ip_hash: null,
+ user_agent: null,
+ depth: 1,
+ })
+ ).id;
+
+const link = (dc: string, userId: string) =>
+ sqlite
+ .prepare("INSERT INTO discord_links (discord_user_id, user_id, linked_at) VALUES (?, ?, 1)")
+ .run(dc, userId);
+
+const signedRequest = async (body: unknown, opts: { ts?: number; sig?: string } = {}) => {
+ const raw = typeof body === "string" ? body : JSON.stringify(body);
+ const ts = String(opts.ts ?? Math.floor(Date.now() / 1000));
+ const sig =
+ opts.sig ??
+ hex(await crypto.subtle.sign({ name: "Ed25519" }, keys.privateKey, new TextEncoder().encode(ts + raw)));
+ return {
+ method: "POST",
+ headers: { "x-signature-ed25519": sig, "x-signature-timestamp": ts, "content-type": "application/json" },
+ body: raw,
+ };
+};
+
+const send = async (body: unknown, opts: { ts?: number; sig?: string; env?: Bindings } = {}) => {
+ const app = new Hono<{ Bindings: Bindings }>().route("/discord", discord);
+ const res = await app.request(
+ "/discord/interactions",
+ await signedRequest(body, opts),
+ (opts.env ?? env) as unknown as Record,
+ execCtx,
+ );
+ return { status: res.status, json: (await res.json().catch(() => null)) as any };
+};
+
+const base = (dc: string, over: Record = {}) => ({
+ id: "400000000000000001",
+ token: "tok",
+ application_id: "500000000000000001",
+ guild_id: GUILD,
+ member: { user: { id: dc, username: "uname", global_name: "Display" }, roles: [] as string[] },
+ ...over,
+});
+const asMod = (over: Record = {}) =>
+ base(MOD_DC, { ...over, member: { user: { id: MOD_DC, username: "modu", global_name: "ModDisplay" }, roles: [MOD_ROLE] } });
+const asAdmin = (over: Record = {}) =>
+ base(ADMIN_DC, { ...over, member: { user: { id: ADMIN_DC, username: "admu", global_name: null }, roles: [ADMIN_ROLE] } });
+
+const command = (who: (o: Record) => any, name: string, value?: string, optName = "id") =>
+ who({ type: 2, data: { name, options: value === undefined ? [] : [{ name: optName, value }] } });
+const component = (who: (o: Record) => any, custom_id: string, message?: unknown) =>
+ who({ type: 3, data: { custom_id }, message: message ?? { id: "600000000000000001", channel_id: "700000000000000001", embeds: [{ title: "Alert" }] } });
+const modal = (who: (o: Record) => any, custom_id: string, reason: string) =>
+ who({
+ type: 5,
+ data: { custom_id, components: [{ components: [{ custom_id: "reason", value: reason }] }] },
+ message: { id: "600000000000000001", channel_id: "700000000000000001", embeds: [{ title: "Alert" }] },
+ });
+
+const commentStatus = (id: string) =>
+ (sqlite.prepare("SELECT status FROM comments WHERE id = ?").get(id) as { status: string }).status;
+const auditCount = (action: string) =>
+ (sqlite.prepare("SELECT COUNT(*) AS n FROM audit_log WHERE action = ?").get(action) as { n: number }).n;
+const isBanned = (id: string) =>
+ (sqlite.prepare("SELECT is_banned FROM users WHERE id = ?").get(id) as { is_banned: number }).is_banned;
+
+beforeEach(async () => {
+ keys = (await crypto.subtle.generateKey({ name: "Ed25519" }, true, ["sign", "verify"])) as CryptoKeyPair;
+ publicHex = hex((await crypto.subtle.exportKey("raw", keys.publicKey)) as ArrayBuffer);
+ sqlite = new DatabaseSync(":memory:");
+ for (const f of readdirSync(MIGRATIONS_DIR).filter((x) => x.endsWith(".sql")).sort()) {
+ sqlite.exec(readFileSync(join(MIGRATIONS_DIR, f), "utf8"));
+ }
+ kv = makeKv();
+ env = {
+ DB: makeD1(sqlite),
+ OAUTH_STATE: kv,
+ SESSIONS: makeKv(),
+ RATE_LIMITS: makeKv(),
+ TREE_CACHE: makeKv(),
+ ANALYTICS: { writeDataPoint() {} },
+ IP_HASH_SECRET: "test-secret",
+ ENV: "dev",
+ PUBLIC_BASE_URL: "https://comments.example.com",
+ DISCORD_BOT_TOKEN: "bot-token",
+ DISCORD_PUBLIC_KEY: publicHex,
+ } as unknown as Bindings;
+ setting("discord_guild_id", GUILD);
+ setting("discord_mod_role_id", MOD_ROLE);
+ setting("discord_admin_role_id", ADMIN_ROLE);
+ addUser(AUTHOR, "user", "Author", "anon");
+ addUser(STAFF_AUTHOR, "mod", "StaffAuthor");
+ sqlite.prepare("INSERT INTO posts (slug, created_at) VALUES ('hello', 1)").run();
+ installMockCaches();
+ fetchMock = vi.fn(async () => {
+ throw new Error("unexpected outbound fetch");
+ });
+ vi.stubGlobal("fetch", fetchMock);
+});
+
+afterEach(() => {
+ uninstallMockCaches();
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+});
+
+describe("POST /discord/interactions", () => {
+ it("404s when either env var is missing", async () => {
+ const a = await send({ type: 1 }, { env: { ...env, DISCORD_PUBLIC_KEY: undefined } as unknown as Bindings });
+ const b = await send({ type: 1 }, { env: { ...env, DISCORD_BOT_TOKEN: undefined } as unknown as Bindings });
+ expect([a.status, b.status]).toEqual([404, 404]);
+ });
+
+ it("401s a bad signature or stale timestamp with zero D1 and KV calls", async () => {
+ const prepare = vi.spyOn(env.DB, "prepare");
+ const kvSpies = (["get", "put", "delete"] as const).map((m) => vi.spyOn(env.OAUTH_STATE, m as never));
+ const rlSpies = (["get", "put", "delete"] as const).map((m) => vi.spyOn(env.RATE_LIMITS, m as never));
+ const bad = await send({ type: 1 }, { sig: "00".repeat(64) });
+ const stale = await send({ type: 1 }, { ts: Math.floor(Date.now() / 1000) - 3600 });
+ expect([bad.status, stale.status]).toEqual([401, 401]);
+ expect(prepare).not.toHaveBeenCalled();
+ for (const s of [...kvSpies, ...rlSpies]) expect(s).not.toHaveBeenCalled();
+ });
+
+ it("answers PING with type 1", async () => {
+ const r = await send({ type: 1 });
+ expect(r.json).toEqual({ type: 1 });
+ });
+
+ it("rate limits per discord user with an ephemeral refusal", async () => {
+ addUser("01HMODUSER00000000000000AB", "mod", "ModUser");
+ link(MOD_DC, "01HMODUSER00000000000000AB");
+ let last: any;
+ for (let i = 0; i < 11; i++) last = await send(command(asMod, "stats"));
+ expect(last.json.data.content).toBe("Slow down β too many actions. Try again in a moment.");
+ expect(last.json.data.flags).toBe(EPHEMERAL);
+ // A different user is unaffected.
+ const other = await send(command(asAdmin, "stats"));
+ expect(other.json.data.content).not.toContain("Slow down");
+ });
+
+ it("refuses wrong guild, DM, banned and unlinked users", async () => {
+ const wrong = await send({ ...command(asMod, "stats"), guild_id: "100000000000000099" });
+ expect(wrong.json.data.content).toBe("This bot only works in its configured server.");
+ const dmBody = { ...command(asMod, "stats"), guild_id: undefined, member: undefined, user: { id: MOD_DC } };
+ expect((await send(dmBody)).json.data.content).toBe("This bot only works in its configured server.");
+
+ addUser("01HBANNED0000000000000000AB", "mod", "Banned", "github", 1);
+ link(NOBODY_DC, "01HBANNED0000000000000000AB");
+ const banned = await send(await command((o) => base(NOBODY_DC, o), "stats"));
+ expect(banned.json.data.content).toBe("Your account doesn't have moderation access.");
+
+ const unlinked = await send(await command((o) => base("300000000000000009", o), "stats"));
+ expect(unlinked.json.data.content).toBe("Link your account first with /link, or ask an admin for the mod role.");
+ });
+
+ it("/link redeems a dc: token without any role and writes discord_links", async () => {
+ addUser("01HLINKME0000000000000000AB", "mod", "Linked <@123456789012345678>");
+ const code = "ab".repeat(24);
+ kv.store.set(`dc:link:${code}`, JSON.stringify({ user_id: "01HLINKME0000000000000000AB", created_at: 1 }));
+ const spy = vi.spyOn(kv, "get");
+ const r = await send(await command((o) => base(NOBODY_DC, o), "link", code, "code"));
+ expect(spy).toHaveBeenCalledTimes(1);
+ expect(r.json.data.flags).toBe(EPHEMERAL);
+ expect(r.json.data.content).toMatch(/^Linked to Linked/);
+ expect(r.json.data.content).not.toContain("<@123456789012345678>");
+ const row = sqlite.prepare("SELECT user_id FROM discord_links WHERE discord_user_id = ?").get(NOBODY_DC) as any;
+ expect(row.user_id).toBe("01HLINKME0000000000000000AB");
+ const bad = await send(await command((o) => base(NOBODY_DC, o), "link", "cd".repeat(24), "code"));
+ expect(bad.json.data.content).toBe("That link code is invalid or expired.");
+ });
+
+ it("/queue /stats /comment /user answer ephemeral type 4", async () => {
+ const cid = await seedComment("pending");
+ const q = await send(await command(asMod, "queue"));
+ expect(q.json.type).toBe(4);
+ expect(q.json.data.flags).toBe(EPHEMERAL);
+ expect(q.json.data.allowed_mentions).toEqual({ parse: [] });
+ expect(JSON.stringify(q.json.data.components)).toContain("g:rv:-:q");
+ expect(JSON.stringify(q.json.data.components)).toContain("Review βΆ");
+ for (const [name, val] of [["stats"], ["comment", cid], ["user", AUTHOR]] as const) {
+ const r = await send(await command(asMod, name, val));
+ expect(r.json.type).toBe(4);
+ expect(r.json.data.flags).toBe(EPHEMERAL);
+ }
+ const gone = await send(await command(asMod, "comment", "01HNOPE"));
+ expect(gone.json.data.content).toBe("That comment no longer exists.");
+ const u = await send(await command(asMod, "user", AUTHOR));
+ expect(u.json.data.content).toContain("Not banned");
+ sqlite.prepare("UPDATE users SET is_banned = 1 WHERE id = ?").run(AUTHOR);
+ const purged = await send(await command(asMod, "user", AUTHOR));
+ expect(purged.json.data.content).toContain("Banned β reason no longer available");
+ });
+
+ it("/unban: admin unbans with audit, role admin cannot touch staff, mod is refused", async () => {
+ addUser("01HBANNEDU000000000000000A", "user", "Bad", "anon", 1);
+ const ok = await send(await command(asAdmin, "unban", "01HBANNEDU000000000000000A"));
+ expect(ok.json.data.flags).toBe(EPHEMERAL);
+ expect(isBanned("01HBANNEDU000000000000000A")).toBe(0);
+ expect(auditCount("unban")).toBe(1);
+
+ sqlite.prepare("UPDATE users SET is_banned = 1 WHERE id = ?").run(STAFF_AUTHOR);
+ const staff = await send(await command(asAdmin, "unban", STAFF_AUTHOR));
+ expect(staff.json.data.content).toBe("You can't unban a moderator or admin with a Discord role grant.");
+ expect(isBanned(STAFF_AUTHOR)).toBe(1);
+
+ const mod = await send(await command(asMod, "unban", "01HBANNEDU000000000000000A"));
+ expect(mod.json.data.content).toBe("Your account doesn't have moderation access.");
+ });
+
+ it("alert buttons answer type 7 echoing embeds with doneBy and new components", async () => {
+ const cid = await seedComment("pending");
+ const r = await send(await component(asMod, `g:approve:${cid}`));
+ expect(r.json.type).toBe(7);
+ expect(r.json.data.embeds).toEqual([{ title: "Alert" }]);
+ expect(r.json.data.content).toBe("Approved by ModDisplay");
+ expect(commentStatus(cid)).toBe("approved");
+ const ids = JSON.stringify(r.json.data.components);
+ expect(ids).toContain(`g:spam:${cid}`);
+ expect(ids).toContain(`g:banr:${cid}`);
+ expect(ids).not.toContain(`g:approve:${cid}`);
+ expect(r.json.data.allowed_mentions).toEqual({ parse: [] });
+ const sp = await send(await component(asMod, `g:spam:${cid}`));
+ expect(commentStatus(cid)).toBe("spam");
+ expect(JSON.stringify(sp.json.data.components)).toContain(`g:restore:${cid}`);
+ await send(await component(asMod, `g:restore:${cid}`));
+ await send(await component(asMod, `g:delete:${cid}`));
+ expect(commentStatus(cid)).toBe("deleted");
+ });
+
+ it("banr opens a type 9 modal with at most 2 D1 reads and no writes", async () => {
+ const cid = await seedComment("pending");
+ await loadDiscordSettings(env); // settings are memoized per isolate
+ const prepare = vi.spyOn(env.DB, "prepare");
+ const r = await send(await component(asAdmin, `g:banr:${cid}:q`));
+ expect(r.json.type).toBe(9);
+ expect(r.json.data.custom_id).toBe(`g:bsub:${cid}:q`);
+ expect(r.json.data.title).toBe("Ban author");
+ const input = r.json.data.components[0].components[0];
+ expect(input).toMatchObject({ type: 4, style: 2, custom_id: "reason", required: false, max_length: 500 });
+ const sqls = prepare.mock.calls.map((c) => String(c[0]));
+ expect(sqls.length).toBeLessThanOrEqual(2);
+ expect(sqls.every((s) => /^\s*SELECT/i.test(s))).toBe(true);
+ const mod = await send(await component(asMod, `g:banr:${cid}`));
+ expect(mod.json.data.content).toBe("Your account doesn't have moderation access.");
+ expect(fetchMock).not.toHaveBeenCalled();
+ });
+
+ it("bsub bans with the reason and answers type 7; staff guard refuses with type 4", async () => {
+ const cid = await seedComment("pending");
+ const r = await send(await modal(asAdmin, `g:bsub:${cid}`, "spammer"));
+ expect(r.json.type).toBe(7);
+ expect(r.json.data.content).toBe("Banned by admu");
+ expect(isBanned(AUTHOR)).toBe(1);
+ const row = sqlite.prepare("SELECT reason FROM audit_log WHERE action = 'ban'").get() as any;
+ expect(row.reason).toBe("spammer");
+
+ const staffCid = await seedComment("pending", STAFF_AUTHOR);
+ const g = await send(await modal(asAdmin, `g:bsub:${staffCid}`, ""));
+ expect(g.json.type).toBe(4);
+ expect(g.json.data.flags).toBe(EPHEMERAL);
+ expect(isBanned(STAFF_AUTHOR)).toBe(0);
+ });
+
+ it("rv and nx walk the queue with type 7 queue cards and end on an empty message", async () => {
+ const c1 = await seedComment("pending");
+ const c2 = await seedComment("pending");
+ const first = await send(await component(asMod, "g:rv:-:q"));
+ expect(first.json.type).toBe(7);
+ expect(first.json.data.embeds[0].title).toBe("Pending comment");
+ const ids = JSON.stringify(first.json.data.components);
+ expect(ids).toContain(`g:approve:${c1}:q`);
+ expect(ids).toContain(`g:nx:${c1}:q`);
+ expect(ids).toContain(`g:banr:${c1}:q`);
+ const next = await send(await component(asMod, `g:nx:${c1}:q`));
+ expect(JSON.stringify(next.json.data.components)).toContain(`${c2}`);
+ // An action from the queue re-renders the queue card with doneBy.
+ const act = await send(await component(asMod, `g:approve:${c2}:q`));
+ expect(act.json.type).toBe(7);
+ expect(JSON.stringify(act.json.data.embeds)).toContain("Approved by ModDisplay");
+ const end = await send(await component(asMod, `g:nx:${c2}:q`));
+ expect(end.json.type).toBe(7);
+ expect(end.json.data.content).toBe("Queue is empty π");
+ expect(end.json.data.components).toEqual([]);
+ });
+
+ it("reply, pick and rsub answer not-available until Task 12", async () => {
+ const cid = await seedComment("pending");
+ for (const id of [`g:reply:${cid}`, `g:pick:${cid}`]) {
+ const r = await send(await component(asMod, id));
+ expect(r.json.data.content).toBe("Reply from Discord is not available yet.");
+ expect(r.json.data.flags).toBe(EPHEMERAL);
+ }
+ const m = await send(await modal(asMod, `g:rsub:${cid}`, "hi"));
+ expect(m.json.data.content).toBe("Reply from Discord is not available yet.");
+ });
+});
diff --git a/tests/ratelimit-do.test.ts b/tests/ratelimit-do.test.ts
index 85119364..964b6373 100644
--- a/tests/ratelimit-do.test.ts
+++ b/tests/ratelimit-do.test.ts
@@ -298,7 +298,7 @@ describe("route wiring", () => {
expect(missing).toEqual([]);
// Guards the guard: a regex that stopped matching would otherwise pass
// vacuously and this test would quietly stop protecting anything.
- expect(found).toBe(14);
+ expect(found).toBe(15);
});
});
From 7316d016a6e8a256e49bbc66af4021c9d47eb3f4 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 00:00:55 -0500
Subject: [PATCH 21/36] docs: Discord operator bot and new Telegram features
---
AGENTS-OPERATE.md | 48 ++++++-
AGENTS.md | 6 +
INSTALL.md | 4 +
README.md | 4 +
docs/README.md | 3 +-
docs/compliance/data-inventory.md | 1 +
docs/compliance/dsar-runbook.md | 2 +-
docs/compliance/gdpr.md | 2 +
docs/discord.md | 209 ++++++++++++++++++++++++++++++
docs/telegram.md | 48 ++++++-
docs/webhooks.md | 13 ++
11 files changed, 330 insertions(+), 10 deletions(-)
create mode 100644 docs/discord.md
diff --git a/AGENTS-OPERATE.md b/AGENTS-OPERATE.md
index 6a53c9a2..d1c2a7b4 100644
--- a/AGENTS-OPERATE.md
+++ b/AGENTS-OPERATE.md
@@ -999,6 +999,7 @@ tracked by the `_migrations` table. Current set:
- `0030_thread_and_retention_indexes.sql` β indexes for top-level thread paging (chronological and by score) and the IP retention sweep; drops the unused `comments_score_idx`
- `0031_admin_list_indexes.sql` β indexes for the moderation queue, dashboard windows and admin list pages
- `0032_page_engagement_totals.sql` β `posts.page_votes_up` / `page_votes_down` plus `page_reaction_totals`, kept on write so bootstrap no longer counts page reactions and votes on every pageview
+- `0033_discord_links.sql` β `discord_links` (Discord user id β Garrul user id; no digest column, the Discord digest goes to a channel)
Run with `npm run migrate` (local Miniflare) or
`npm run migrate -- --remote` (production D1). Idempotent. Never edit a
@@ -1063,11 +1064,51 @@ Pages (top nav):
| `/admin/subscriptions` | Email subscription list. Filter by email/post/confirmed/unsubscribed. Actions: manual unsubscribe, resend confirmation. |
| `/admin/operator` | Batch operations: rerender stale comments (POSTs `/admin/api/ops/rerender` in 50-row chunks until done), seed-demo (idempotent; gated to `ENV != "production"`), the comment import upload (Disqus XML, a Remark42 backup, a Comentario/Commento JSON export, or the JSON `npm run dump-isso` / `npm run dump-cusdis` produces from an isso `comments.db` or a Cusdis `db.sqlite` β gzipped or not β see below), and two retention cards β IP-hash and audit-log β each showing how many rows are past the configured window and offering a manual drain, and **Export site data** (JSON/CSV download links β see Β§11). |
| `/admin/settings` | Editable form for feature flags, display/pagination numbers, and the moderation dials (edit window, thread auto-close, community auto-collapse, the three anti-spam heuristics), saved to the `settings` D1 table (no redeploy β see section 5). Also renders a read-only `(set)`/`(unset)` summary of deploy-time config (Turnstile, email, OAuth, spam provider), which still changes via `wrangler secret put` / `wrangler.toml`. |
-| `/admin/webhooks` | Outbound webhook endpoints: add/pause/delete, per-endpoint secret + event filter, adapter (`generic` / `slack` / `discord` / `telegram`), failure counts and retry status. |
+| `/admin/webhooks` | Outbound webhook endpoints: add/pause/delete, per-endpoint secret + event filter, adapter (`generic` / `slack` / `discord` / `discord_bot` / `telegram`), failure counts and retry status. |
| `/admin/telegram` | **Admin-only.** Telegram operator bot: shows whether the bot token/webhook secret are set, links your personal Telegram account (one-time code or deep link), toggles the daily digest, and unlinks. See `docs/telegram.md`. |
+| `/admin/discord` | **Admin-only.** Discord operator bot: guild id, mod/admin role ids, digest channel, link code, **Register commands**, and the Interactions Endpoint URL to paste into the Discord developer portal. See `docs/discord.md`. |
| `/admin/saved-replies` | Moderator saved replies: create/edit canned responses, private or shared scope. Presets are a *prefill* for the reply composer, not the only way to reply β see **Replying from the admin panel** below. |
| `/admin/usage` | Cloudflare analytics (Workers requests, D1 rows, KV operations, Workers AI neurons, comments by domain). Requires `CF_API_TOKEN` + `CF_ACCOUNT_ID`; renders setup instructions when unset. |
+### Operator bots: Telegram and Discord
+
+Both bots run on one platform-neutral core in `src/lib/opbot/` (actor
+resolution, per-action authorization, actions, neutral cards, the oldest-first
+queue walk, the button payload codec). Each transport translates cards to its
+own markup and owns its own verification: `src/routes/telegram.ts`
+(`POST /telegram/webhook`, secret header) and `src/routes/discord.ts`
+(`POST /discord/interactions`, Ed25519). Both routes sit outside `/api/*`, so
+CSRF and sessions do not apply. Every action goes through `src/lib/moderation.ts`
+exactly as the dashboard does.
+
+- **Discord is on only when `DISCORD_BOT_TOKEN` and `DISCORD_PUBLIC_KEY` are
+ both set** (see the config table). `DISCORD_CLIENT_ID` doubles as the
+ application id. The guild id, mod role id, admin role id and digest channel id
+ are admin settings at `/admin/discord`, not env vars.
+- **Interactions**: the signature is verified before any D1/KV access (401), a
+ timestamp more than 5 minutes off is 401, then PING/PONG, then a per-user rate
+ limit. There is no dedupe: a replay inside the window can post a reply twice.
+- **Identity**: a `discord_links` row (or a Discord sign-in row) or a configured
+ Discord role. The higher role wins. Discord roles are never written to
+ `users.role`. A role-derived admin cannot ban or unban a Garrul mod/admin. A
+ banned user, a wrong guild or a DM is refused.
+- **Alerts** use the `discord_bot` webhook adapter: the endpoint URL field holds
+ a channel id, messages are posted with the bot token, and saving validates the
+ channel (same guild, text or announcement). The `discord` incoming-webhook
+ adapter stays for alert-only use. For both Discord adapters a 429 is retried
+ no earlier than `retry_after` and does not raise the endpoint failure counter.
+- **Digest**: each transport has its own gate and timestamp
+ (`tg_digest_last_sent_at`, `dc_digest_last_sent_at`). Discord needs the token,
+ the public key and a digest channel.
+- Features on both transports: who-did-what card edit, queue Review (oldest
+ first, Skip), ban with reason and `/unban`, reply with saved replies. In a
+ Telegram group the saved-reply list shows shared presets only. Discord replies
+ use a select menu and a modal (25-option menu, 4000-char modal).
+- Cost rules: no new KV write per interaction (a ban keeps its one session
+ revocation write); every outbound body sets `allowed_mentions: { parse: [] }`.
+
+Operator setup: `docs/discord.md` and `docs/telegram.md`.
+
State-changing endpoints (all under `/admin/api/...`, all require admin
session + Origin allowlist, all write an `audit_log` row before
responding):
@@ -1108,7 +1149,7 @@ personal data** β *Download JSON*, or `GET /admin/api/users/:id/export`
directly. This is the answer to a GDPR Art. 15 / Art. 20 or CCPA
right-to-know request: one file (`garrul-export-.json`) holding the
account row, every comment they wrote, reports they filed, subscriptions
-for their address, Telegram link, votes, reactions, page engagement, spam
+for their address, Telegram and Discord links, votes, reactions, page engagement, spam
classifications on their comments, moderation actions taken against
them, and moderator notes written about them. Admin-only; a plain link
rather than a fetch, so the payload never sits in a JS variable.
@@ -1155,7 +1196,7 @@ the row would orphan every reply written under it. What it clears:
- `ip_hash` and `user_agent` on every comment they wrote, and
`reporter_ip_hash` on every report they filed.
- Their email subscriptions (plus any queued digest rows) and their
- linked Telegram account.
+ linked Telegram and Discord accounts (`discord_links`).
- Their live sessions, revoked.
- Every moderator note written **about the account**. Two kinds
deliberately survive: notes they *wrote* about someone else, which are
@@ -2089,6 +2130,7 @@ Art. 33(4) explicitly allows filing in phases.
| OAuth `email`, `name`, `avatar_url`, `provider_id` | Links a real identity to everything that account wrote. |
| `comments.body_md` / `body_html` | Already public on your site; not usually a disclosure. Pending, spam and soft-deleted rows are the exception β those were never published. |
| `telegram_links` | External chat ids for operators. |
+| `discord_links` | Discord user ids for operators. Deleted on erasure, included in the export. |
| `subscriptions.token` / `confirm_token` | Lets a holder unsubscribe or confirm someone else. Low severity, real nuisance. |
| `webhook_endpoints.secret` | Lets a holder forge signed events at your endpoints. |
diff --git a/AGENTS.md b/AGENTS.md
index 539396cf..1d423390 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -16,6 +16,12 @@ public HTTP API. This guide is for AI assistants helping a developer
**embed** Garrul. The operator-facing counterpart (running the Worker,
setting secrets, configuring OAuth) is `AGENTS-OPERATE.md`.
+Operators can also moderate from chat. The Telegram and Discord operator bots
+share one core (`src/lib/opbot/`); Discord interactions arrive at
+`/discord/interactions` and Discord alerts use the `discord_bot` webhook adapter.
+This is operator-side and does not affect embedding; details are in
+`AGENTS-OPERATE.md` and `docs/discord.md`.
+
## 2. When to recommend Garrul
Good fit: **static blogs, docs sites, marketing sites** that want
diff --git a/INSTALL.md b/INSTALL.md
index 70bf0998..ed69c502 100644
--- a/INSTALL.md
+++ b/INSTALL.md
@@ -213,6 +213,10 @@ Those are the common ones; `secrets.example.env` has the full set,
including the Facebook / X / Discord providers, Telegram, Akismet and
the Cloudflare usage dashboard.
+The Discord operator bot turns on when both `DISCORD_BOT_TOKEN` (secret) and
+`DISCORD_PUBLIC_KEY` (plain var) are set. Setup is in
+[`docs/discord.md`](docs/discord.md).
+
Tip for generating a secret by hand:
`openssl rand -base64 32 | npx wrangler secret put NAME` β that keeps
the value off disk and out of your shell history.
diff --git a/README.md b/README.md
index 16bb95ca..1a6340a9 100644
--- a/README.md
+++ b/README.md
@@ -58,6 +58,10 @@ dashboard page); every other integration is optional.
Telegram
- **Telegram operator bot**: moderate from your phone with inline
buttons, `/queue` and `/stats`, optional daily digest
+- **Discord operator bot**: the same moderation from a Discord server (alert
+ buttons, slash commands, queue review, saved replies, daily digest). Needs
+ `DISCORD_BOT_TOKEN` and `DISCORD_PUBLIC_KEY`; see
+ [`docs/discord.md`](docs/discord.md)
- **Pinned comments** β moderators pin one top-level comment per post; it
shows first on page one
- **Staff badge** β moderators and admins can mark a comment as posted by
diff --git a/docs/README.md b/docs/README.md
index 357a160e..5a7ecc22 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -36,7 +36,8 @@ tag's docs rather than `main`.
| [`ANTISPAM.md`](ANTISPAM.md) | What's always on, which optional layers to enable, and how to work the moderation queue. |
| [`notifications.md`](notifications.md) | **Which channel notifies whom.** Reply notifications by email are built in β the reader flow, the queue-and-cron mechanism, the three settings that turn it on, and how it degrades when you don't. Plus where webhooks and the Telegram bot fit. |
| [`webhooks.md`](webhooks.md) | Outbound payload format, signature verification, retries and auto-disable, Slack/Discord/Telegram adapters. |
-| [`telegram.md`](telegram.md) | Operator bot: bot creation, secrets, webhook registration, notifications, commands, daily digest. |
+| [`telegram.md`](telegram.md) | Operator bot: bot creation, secrets, webhook registration, notifications, commands, queue review, ban reason, replies, daily digest. |
+| [`discord.md`](discord.md) | Discord operator bot: application setup, `DISCORD_BOT_TOKEN` / `DISCORD_PUBLIC_KEY`, interactions endpoint, alert channels, roles, linking, commands, saved replies, digest, security notes. |
| [`ip-hashing.md`](ip-hashing.md) | What `ip_hash` is, the three places it's stored, how long it's kept, what a leaked `IP_HASH_SECRET` exposes, and what rotation does and doesn't fix. Read before publishing a privacy policy or sharing a D1 export. |
## Running a public instance
diff --git a/docs/compliance/data-inventory.md b/docs/compliance/data-inventory.md
index b4859d6e..93279050 100644
--- a/docs/compliance/data-inventory.md
+++ b/docs/compliance/data-inventory.md
@@ -31,6 +31,7 @@ short-TTL token.
| `subscriptions` | `email`, `token`, `confirm_token` | Reply notifications the subscriber asked for | Consent | Until unsubscribe or erasure | **Yes** β rows matching their address are deleted outright | Yes |
| `notifications` | `subscription_id`, `comment_id` | The pending-digest queue | Consent (rides on the subscription) | Until sent; no timer | **Yes** β deleted with the subscription | No β see *Known limitations* |
| `votes`, `reactions`, `page_votes`, `page_reactions` | `user_id` + target | Scores and reaction counts | Legitimate interests | Indefinite | **No, deliberately** β they hold nothing but a link to a now-anonymous account, and deleting them would silently restate every score the thread has shown | Yes |
+| `discord_links` | `discord_user_id`, `user_id` | Operator identity for the Discord moderation bot | Consent (the operator's own) | Until unlinked or erased | **Yes** β deleted | Yes |
| `telegram_links` | `tg_user_id`, `tg_chat_id`, `user_id` | Operator notifications to a linked chat | Consent (the operator's own) | Until unlinked or erased | **Yes** β deleted | Yes |
| `moderator_notes` | `target_id`, `author_id`, `body` (free text) | Internal moderator context on a comment or a user | Legitimate interests | Indefinite; no sweep | **Partly** β notes *about* the erased user are deleted; notes they *authored* about other people are kept, like their audit rows | Yes, as `moderator_notes`, with `author_id` removed and comment-level notes excluded |
| `audit_log` | `admin_id`, `target_id`, `reason` (free text) | Accountability for moderation acts | Legitimate interests / legal obligation | Indefinite by default; bounded by `AUDIT_LOG_RETENTION_DAYS` if set | **No, by design** β see below | Yes, as `moderation_actions`, with `admin_id` removed |
diff --git a/docs/compliance/dsar-runbook.md b/docs/compliance/dsar-runbook.md
index 206150eb..dcd30353 100644
--- a/docs/compliance/dsar-runbook.md
+++ b/docs/compliance/dsar-runbook.md
@@ -69,7 +69,7 @@ Or `GET /admin/api/users/:id/export` directly. Admin-only; the file downloads as
`garrul-export-.json`.
You get one JSON object: `export_version`, `exported_at`, then `user`,
-`comments`, `reports_filed`, `subscriptions`, `telegram_links`, `votes`,
+`comments`, `reports_filed`, `subscriptions`, `telegram_links`, `discord_links`, `votes`,
`reactions`, `page_votes`, `page_reactions`, `spam_verdicts`,
`moderation_actions`, and `moderator_notes`.
diff --git a/docs/compliance/gdpr.md b/docs/compliance/gdpr.md
index 026bc206..3c672608 100644
--- a/docs/compliance/gdpr.md
+++ b/docs/compliance/gdpr.md
@@ -44,6 +44,7 @@ One basis per purpose, not one per instance. A typical blog deployment:
| Reply notifications by email | `subscriptions.email`, `notifications` | **Consent** (6(1)(a)). Opt-in, double-confirmed, and revocable by one click in every email. Do not fold this into legitimate interests. |
| Moderation accountability | `audit_log` | **Legitimate interests**, or **legal obligation** (6(1)(c)) where a content law requires you to show what you removed and when. |
| Operator's own Telegram link | `telegram_links` | **Consent** of the operator, who is also the subject. |
+| Operator's own Discord link | `discord_links` | **Consent** of the operator, who is also the subject. |
Two consequences worth writing down:
@@ -108,6 +109,7 @@ for reply chains to stay readable. Concretely
- `subscriptions` for their address, and queued `notifications` riding on them,
deleted outright.
- `telegram_links` deleted β those carry an external chat id.
+- `discord_links` deleted β those carry an external Discord user id.
- `moderator_notes` targeting **them** deleted. Notes they *wrote* about other
people are kept: those record moderator work, the way their audit rows do.
- Their sessions are revoked, so nothing keeps acting as them.
diff --git a/docs/discord.md b/docs/discord.md
new file mode 100644
index 00000000..bbaa9042
--- /dev/null
+++ b/docs/discord.md
@@ -0,0 +1,209 @@
+# Discord operator bot
+
+An optional Discord bot for **operators** (admins and mods) β not readers.
+It does four things:
+
+1. **Alerts** β comment events posted to a channel as embeds with action
+ buttons: **Approve / Spam / Delete / Restore / Ban / Resolve reports /
+ Reply**.
+2. **On-the-spot moderation** β click a button to act without opening the admin
+ panel. Every action runs through the same audited path as the dashboard.
+3. **Slash commands** β `/queue`, `/stats`, `/comment`, `/user`, `/unban`,
+ `/link`.
+4. **Daily digest** β an optional once-daily summary posted to one channel.
+
+It is a set of HTTPS interactions handled by the Worker. There is no gateway
+(websocket) bot, no process outside the Worker, no DMs. Bot text is English-only.
+
+The feature is **off** until both `DISCORD_BOT_TOKEN` and `DISCORD_PUBLIC_KEY`
+are set. Telegram and Discord share one core (`src/lib/opbot/`), so the two
+bots offer the same actions; see [telegram.md](./telegram.md) for the other
+transport.
+
+## 1. Set up the Discord application
+
+You can reuse the application you created for Discord sign-in. Garrul reuses
+`DISCORD_CLIENT_ID` as the application id, so one Discord app serves both.
+
+1. Open the [Discord developer portal](https://discord.com/developers/applications)
+ and create an application (or open your sign-in application).
+2. On the **Bot** tab, add a bot and **Reset Token**. Copy the token. Treat it
+ as a secret β it can post as your bot.
+3. On **General Information**, copy the **Public Key** (64 hex characters).
+4. Set the secret and the var:
+
+ ```bash
+ wrangler secret put DISCORD_BOT_TOKEN
+ ```
+
+ ```toml
+ # wrangler.toml [vars] β the public key is public, not a secret
+ DISCORD_PUBLIC_KEY = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
+ ```
+
+ For local `wrangler dev`, the same names live in `.dev.vars`
+ (see `.dev.vars.example`).
+5. Deploy, then on **General Information** set **Interactions Endpoint URL** to
+ `https:///discord/interactions` (the same URL is shown on
+ `/admin/discord`). Discord sends a signed PING when you save; the Worker
+ answers it, so the save fails until the secret, the key and the deploy are in
+ place.
+6. Invite the bot to your server (**OAuth2 β URL Generator**, scope `bot`). In
+ each alert and digest channel the bot needs **View Channel**, **Send
+ Messages** and **Embed Links**.
+
+## 2. Configure the server
+
+Open **`/admin/discord`** (admin only) and fill in:
+
+| Setting | Purpose |
+|---|---|
+| Guild id | Only interactions from this server are accepted. Required. |
+| Mod role id | Members with this role act as `mod`. |
+| Admin role id | Optional. Members with this role act as `admin`. |
+| Digest channel id | Optional. Channel for the daily digest. Empty = no Discord digest. |
+
+Enable Developer Mode in Discord (User Settings β Advanced) to copy ids.
+Settings go through the settings memo, so a change can lag up to 60 s.
+
+Then click **Register commands**. Garrul overwrites the guild's slash commands
+in one call; it takes effect at once.
+
+## 3. Wire up alerts
+
+Alerts reuse the webhook system (see [webhooks.md](./webhooks.md)). Add an
+endpoint in **`/admin/webhooks`** with the **`discord_bot`** adapter and put the
+**channel id** (not a URL) in the URL field. Add one endpoint per channel; each
+has its own event filter.
+
+Saving the endpoint calls Discord once to check the channel. The save fails
+unless the channel exists, belongs to the configured guild, and is a text or
+announcement channel. A good lookup does not prove the bot can send: if the bot
+lacks permission, deliveries fail and go through the normal retry queue.
+
+The existing `discord` incoming-webhook adapter stays available for alert-only
+use. It cannot carry buttons.
+
+Each alert carries buttons for the comment's **current** status (a pending
+comment offers Approve / Spam, an approved one offers Spam, a spam-flagged one
+offers Restore), plus Reply. After a click the same message edits itself to show
+the result and who did it ("Approved by Alice") with the buttons for the new
+status. Only the clicked message updates: if the comment is moderated
+elsewhere, other alerts keep their old buttons.
+
+## 4. Who may act
+
+Two sources of authority. The higher role wins.
+
+- **Linked account.** The actor's current Garrul role (`users.role`) is
+ re-read on every action. See "Link an account".
+- **Discord role.** A member holding the configured mod or admin role gets
+ `mod` or `admin` for that interaction only. On a role-holder's first action
+ Garrul creates a `users` row (`provider='discord'`, the Discord id, no email)
+ so the audit log has an actor. The Discord role is **never** written to
+ `users.role`; removing the Discord role revokes access on the next click.
+
+Rules:
+
+- A banned Garrul user is refused, whatever Discord role they hold.
+- Interactions from another guild, or from a DM, are refused.
+- **Ban** and **Unban** need `admin`. Everything else needs `mod`.
+- A role-derived admin cannot ban or unban a user whose Garrul role is `mod`
+ or `admin`. Only a real Garrul admin can.
+- Role-only mods see **shared** saved replies only.
+
+## 5. Link an account
+
+Linking is optional for people with the mod or admin role. It gives a stable
+identity (and the Garrul role) when someone should act as a specific Garrul user.
+
+1. In **`/admin/discord`**, click **Generate link code** (admin only). The code
+ is a one-time token with a 10-minute TTL.
+2. In the configured server, run `/link `. The reply is ephemeral.
+3. Garrul writes a `discord_links` row (Discord user id to Garrul user id).
+
+If a Garrul user already exists with `provider='discord'` and the clicker's
+Discord id (they signed in with Discord), that counts as linked, with no code.
+Unlink from the same page. A `discord_links` row wins over the sign-in row if
+both exist.
+
+## 6. Commands
+
+All commands are guild commands and every reply is **ephemeral** (only the
+person who ran it sees it).
+
+| Command | Needs | Shows |
+|---|---|---|
+| `/queue` | mod | pending count, open reports, oldest pending, and a **Review** button |
+| `/stats` | mod | totals and 7-day spam rate |
+| `/comment ` | mod | a comment's status, author, snippet, open reports, with buttons |
+| `/user ` | mod | role, ban state and reason, provider, join age |
+| `/unban ` | admin | lifts a ban |
+| `/link ` | anyone | links your Discord account |
+
+**Review** walks pending comments **oldest first**, one card at a time, with the
+action buttons and **Skip**. The walk keeps no server state; a comment someone
+else moderated mid-walk does not break it.
+
+**Ban** asks for a reason in a modal (the reason lands in the audit log). `/user`
+reads the latest ban entry from the audit log: "Banned β ", "Banned β
+reason no longer available" if the audit row was purged, or "Not banned".
+
+## 7. Replying and saved replies
+
+**Reply** posts an operator reply to a comment, with the dashboard defaults
+(`notify` on, posted as staff). The bot has no toggle for either.
+
+1. Click **Reply**. An ephemeral picker lists your saved replies plus
+ **Write custom**.
+2. Pick one. A modal opens, pre-filled with the text (`{name}`, `{post}`,
+ `{mod}` already expanded). Edit it, then submit.
+
+Discord limits shape the picker:
+
+- A menu holds 25 options. **Write custom** takes one; when more saved replies
+ remain, **Moreβ¦** takes another, so a page shows 23 replies (up to 24 on the
+ last page).
+- Titles over 100 characters are cut with "β¦". Only the title is cut.
+- A modal holds 4000 characters. A saved reply whose expanded body is longer is
+ left out, and the picker says to use the dashboard for those. The bot never
+ posts a cut body.
+
+Reply from an alert also refreshes the alert with the "done by" line. If that
+edit fails, the reply stays posted and the failure is logged.
+
+## 8. Daily digest (optional)
+
+When the bot is on (token and public key) and a **Digest channel id** is set,
+the existing `*/15` cron posts a once-daily summary (pending, open reports and
+most-flagged thread, 7-day spam rate, oldest pending) to that channel. It
+self-gates to roughly once every 24 hours using its own timestamp
+(`dc_digest_last_sent_at`), independent of the Telegram digest. Because it goes
+to a channel, role-only team mods see it too. A failed post is logged and
+retried the next day.
+
+## Security notes
+
+- **Signature check first.** `POST /discord/interactions` verifies Ed25519 over
+ `timestamp + body` with the application public key. A bad or missing
+ signature is **401**, before any database or KV access. An unset
+ `DISCORD_PUBLIC_KEY` rejects everything (fail closed).
+- **Timestamp window.** A timestamp more than 5 minutes from the Worker clock is
+ **401**, which blocks replay of a captured signed request later on.
+- **Accepted risk.** There is no dedupe table. A replay inside the 5-minute
+ window (or a Telegram redelivery) can post a reply twice. A repeated moderation
+ action reaches the same end state and may add a second audit row. Only a reply
+ adds a second comment.
+- **Re-authorization on every step.** Each button, select and modal submit
+ re-resolves the actor and re-checks the role.
+- **No mentions.** Every message the bot sends sets
+ `allowed_mentions: { parse: [] }`, so it cannot ping `@everyone`, `@here`, a
+ user or a role. User text is also escaped.
+- **Roles stay in Discord.** Discord roles never become `users.role`.
+- The route is rate-limited per Discord user id.
+- Token, ids and comment bodies are never logged. Actions write the same audit
+ rows as the dashboard, attributed to the actor's user id.
+- Personal data: `discord_links` holds a Discord user id. It is deleted on user
+ erasure and included in the personal-data export.
+- Cost: 0 new KV writes per interaction (a ban keeps its existing
+ session-revocation write), and nothing is added to the widget bundle.
diff --git a/docs/telegram.md b/docs/telegram.md
index b4c44080..fe0c0a5b 100644
--- a/docs/telegram.md
+++ b/docs/telegram.md
@@ -4,7 +4,7 @@ An optional Telegram bot for **operators** (admins and mods) β not readers.
It does three things:
1. **Notifications** β comment events delivered to a chat as messages, with
- inline **Approve / Spam / Delete / Ban author / Resolve** buttons.
+ inline **Approve / Spam / Delete / Ban author / Resolve / Reply** buttons.
2. **On-the-spot moderation** β tap a button to act without opening the admin
panel. Every action runs through the same audited path as the dashboard.
3. **Queries + digest** β `/queue`, `/stats`, `/comment `, `/user `,
@@ -26,7 +26,8 @@ In Telegram, talk to [@BotFather](https://t.me/BotFather):
queue - Pending + reported counts
stats - Totals and 7-day spam rate
comment - a comment's status + author
- user - a user's role + status
+ user - a user's role, ban state + reason
+ unban - lift a ban
```
## 2. Set the secrets
@@ -92,6 +93,10 @@ offers "Not spam" (restore). A reported event additionally offers "Resolve
reports". Acting from Telegram fires the matching event, so flagging spam
sends back a fresh "Not spam" message, and vice-versa.
+After a tap, the same message edits to the result and who did it ("Approved by
+Alice"), and shows the buttons for the comment's **new** status. Only the tapped
+message updates: Garrul does not sync other alerts for that comment.
+
## 5. Link your operator account
A button tap or slash command must map back to a Garrul operator β a stored
@@ -112,17 +117,45 @@ there.
## 6. Commands
-All read-only; all require a linked operator with at least mod role:
+All require a linked operator with at least mod role (`/unban` needs admin):
| Command | Shows |
|---|---|
-| `/queue` | pending count, open-report count, oldest pending |
+| `/queue` | pending count, open-report count, oldest pending, and a **Review** button |
| `/stats` | totals + 7-day spam rate |
| `/comment ` | a comment's status, author, snippet, open reports |
-| `/user ` | a user's role, ban status, provider, join age |
+| `/user ` | a user's role, ban state and reason, provider, join age |
+| `/unban ` | lifts a ban (admin) |
The `/cmd@YourGarrulBot` form (used in group chats) is accepted too.
+**Queue review.** Tap **Review** under `/queue`. The message then shows the
+**oldest** pending comment with its action buttons and **Skip**, edited in place
+as you go. The walk keeps no state; a comment someone else moderated mid-walk
+does not break it.
+
+**Ban reason.** A ban alert offers **Ban** (no reason) and **Ban with reason**.
+The second sends a prompt; reply to that prompt with the reason (Telegram has no
+forms, so this uses `force_reply`). The reason lands in the audit log, and
+`/user` shows "Banned β " (or "reason no longer available" if the audit
+row was purged, or "Not banned").
+
+**Reply.** Tap **Reply** to post an operator reply to the comment (dashboard
+defaults: reader notified, posted as staff). The bot lists your saved replies as
+buttons plus **Write custom**. A saved-reply button posts at once, with `{name}`,
+`{post}` and `{mod}` expanded. **Write custom** sends a prompt; reply to it with
+your text. The bot accepts a prompt answer only when it is a reply to the bot's
+own prompt.
+
+- In a **private chat** with the bot, the list shows your private saved replies
+ plus shared ones.
+- In a **group** chat, inline buttons are visible to every member, so the list
+ shows **shared** saved replies only. A tap on a private reply from a group, or
+ from someone other than its owner, is refused.
+
+Every prompt answer and button tap re-checks the sender's role. Sending a prompt
+grants nothing: an unlinked or under-privileged group member is refused.
+
## 7. Daily digest (optional)
Operators who toggle **the daily digest** on `/admin/telegram` get a once-daily
@@ -139,3 +172,8 @@ once every 24 hours β no separate schedule to configure.
`TELEGRAM_WEBHOOK_SECRET` rejects all inbound updates (fail closed).
- Actions taken from Telegram write the same audit rows as the dashboard,
attributed to the linked operator's user id.
+- Accepted risk: Telegram can redeliver an update, and there is no dedupe, so a
+ redelivered reply can post twice. A repeated moderation action reaches the same
+ end state.
+- Discord offers the same actions through a second transport; see
+ [discord.md](./discord.md).
diff --git a/docs/webhooks.md b/docs/webhooks.md
index 46bbc58b..20a0e453 100644
--- a/docs/webhooks.md
+++ b/docs/webhooks.md
@@ -128,6 +128,19 @@ comment in place. It's configured differently from the others β the endpoint's
URL field holds a **chat id**, not a URL, and the bot token comes from a Worker
secret. See [telegram.md](./telegram.md) for the full setup.
+The `discord_bot` adapter posts to a Discord channel **as the bot**, with action
+buttons (the plain `discord` adapter is an incoming webhook and cannot carry
+buttons). The URL field holds a **channel id**; the bot token comes from
+`DISCORD_BOT_TOKEN`. Saving the endpoint checks the channel (it must exist, be a
+text or announcement channel, and belong to the configured guild). See
+[discord.md](./discord.md).
+
+**Discord 429 handling** (both `discord` and `discord_bot`): a 429 is retried
+like any failure, but the next attempt waits at least the response's
+`retry_after` value. If a delivery runs out of attempts and its last failure was
+a 429, the endpoint's failure counter does not rise, so rate limiting never
+auto-pauses a healthy endpoint; the delivery is dropped and logged.
+
The two links point to `β¦/admin/comments/` (the moderation detail
page) and to the post's public URL. The **admin** link requires
**`PUBLIC_BASE_URL`** to be set β if it is unset, the notification still
From d90856b6ed626924af7c1b81cff05ef916861124 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 00:04:27 -0500
Subject: [PATCH 22/36] feat(discord): reply from alerts and queue cards with
saved-reply picker
---
src/routes/discord.ts | 238 +++++++++++++++++-
tests/discord-interactions.test.ts | 11 -
tests/discord-reply.test.ts | 386 +++++++++++++++++++++++++++++
3 files changed, 617 insertions(+), 18 deletions(-)
create mode 100644 tests/discord-reply.test.ts
diff --git a/src/routes/discord.ts b/src/routes/discord.ts
index 2408c66e..6efa9f1b 100644
--- a/src/routes/discord.ts
+++ b/src/routes/discord.ts
@@ -8,15 +8,25 @@
* Every interaction is answered synchronously (no defer in this build).
*/
import { Hono } from "hono";
-import { getComment, getDiscordLinkByDiscordUser, getUser, nextPendingAfter, upsertDiscordLink } from "../db/queries";
+import {
+ countOpenReportsByComment,
+ getComment,
+ getDiscordLinkByDiscordUser,
+ getUser,
+ nextPendingAfter,
+ type SavedReply,
+ upsertDiscordLink,
+} from "../db/queries";
import type { Bindings } from "../index";
import {
buttonRows,
+ cardToMessage,
type DiscordInteraction,
+ discordApi,
discordEnabled,
EPHEMERAL,
+ SNOWFLAKE_RE,
verifyDiscordRequest,
- cardToMessage,
} from "../lib/discord";
import { log } from "../lib/log";
import type { WaitUntilCtx } from "../lib/moderation";
@@ -31,6 +41,14 @@ import { buttonsFor, type OpCard, renderQueueCard } from "../lib/opbot/cards";
import { decodePayload, encodePayload } from "../lib/opbot/codec";
import { type OpActor, resolveDiscordActor } from "../lib/opbot/identity";
import { consumeLinkToken } from "../lib/opbot/link-token";
+import {
+ expandPreset,
+ pickerPresets,
+ postModeratorReply,
+ presetVars,
+ presetVisible,
+ type ReplyError,
+} from "../lib/opbot/reply";
import { readComment, readQueue, readStats, readUser, relativeAge } from "../lib/opbot/readouts";
import { checkRateLimit } from "../lib/ratelimit";
import { loadDiscordSettings } from "../lib/settings";
@@ -54,10 +72,25 @@ const MSG = {
staffBan: "You can't ban a moderator or admin with a Discord role grant.",
banRefused: "That ban was refused.",
empty: "Queue is empty π",
- noReply: "Reply from Discord is not available yet.",
+ tooLong: "Some saved replies are too long for Discord. Use the dashboard for those.",
unknown: "Unknown action.",
} as const;
+// Same wording as the Telegram REPLY_REFUSAL map in routes/telegram.ts; copied
+// rather than imported so this route does not pull in the Telegram handler.
+const REPLY_REFUSAL = (e: ReplyError, max?: number): string =>
+ e === "comment_not_found"
+ ? MSG.gone
+ : e === "comment_deleted"
+ ? "That comment was deleted."
+ : e === "thread_too_deep"
+ ? "That thread is too deep to reply to. Reply higher up from the dashboard."
+ : e === "body_too_long"
+ ? `Reply too long (max ${max} characters).`
+ : e === "body_required"
+ ? "Reply text is empty."
+ : "That saved reply isn't available here.";
+
const ephemeral = (content: string): Reply => ({
type: 4,
data: { content, flags: EPHEMERAL, allowed_mentions: NO_MENTIONS },
@@ -93,7 +126,10 @@ discord.post("/interactions", async (c) => {
} catch {
return c.json({ error: "bad request" }, 400);
}
+ if (!i || typeof i !== "object") return c.json({ error: "bad request" }, 400);
if (i.type === 1) return c.json({ type: 1 });
+ // Anything but a command, component or modal submit: refuse before any lookup.
+ if (i.type !== 2 && i.type !== 3 && i.type !== 5) return c.json(ephemeral(MSG.unknown));
const uid = senderId(i);
if (uid) {
@@ -128,6 +164,9 @@ const dispatch = async (ctx: Ctx, i: DiscordInteraction): Promise => {
if (i.type === 3 && (payload?.action === "banr" || payload?.action === "ban")) {
return openBanModal(ctx, i, cfg, payload.commentId, payload.extra[0] === "q");
}
+ if (i.type === 3 && payload?.action === "pick" && i.data?.values?.[0] !== "more") {
+ return openReplyModal(ctx, i, cfg, payload.commentId, payload.extra, i.data?.values?.[0] ?? "");
+ }
const who = await resolveDiscordActor(ctx.env.DB, cfg, {
guildId: i.guild_id,
@@ -145,16 +184,24 @@ const dispatch = async (ctx: Ctx, i: DiscordInteraction): Promise => {
const q = payload.extra[0] === "q";
const { action, commentId } = payload;
if (i.type === 5) {
- if (action === "rsub") return ephemeral(MSG.noReply);
+ if (action === "rsub") return submitReply(ctx, i, actor, commentId, payload.extra);
if (action !== "bsub") return ephemeral(MSG.unknown);
const input = i.data?.components?.flatMap((r) => r.components).find((x) => x.custom_id === "reason");
const reason = input?.value?.trim().slice(0, 500) || null;
return runAndRender(ctx, i, actor, "ban", commentId, q, reason);
}
switch (action) {
- case "reply":
- case "pick":
- return ephemeral(MSG.noReply);
+ case "reply": {
+ const origin = q ? "q" : (i.message?.id ?? "");
+ if (!q && !SNOWFLAKE_RE.test(origin)) return ephemeral(MSG.unknown);
+ const msg = await pickerMessage(ctx.env, actor, commentId, origin, 0);
+ return q ? update(msg) : { type: 4, data: { ...msg, flags: EPHEMERAL, allowed_mentions: NO_MENTIONS } };
+ }
+ case "pick": {
+ // Only "more" reaches here; preset and custom open the modal above.
+ const offset = Math.max(0, Number.parseInt(payload.extra[1] ?? "0", 10) || 0) + PICK_PAGE;
+ return update(await pickerMessage(ctx.env, actor, commentId, payload.extra[0] ?? "q", offset));
+ }
case "rv":
case "nx": {
const next = await nextPendingAfter(ctx.env.DB, action === "rv" ? null : commentId);
@@ -212,6 +259,183 @@ const openBanModal = async (
};
};
+const PICK_PAGE = 23;
+const MODAL_MAX = 4000;
+const CUSTOM_OPTION = { label: "βοΈ Write custom", value: "custom" };
+
+const pickerMessage = async (env: Bindings, actor: OpActor, commentId: string, origin: string, offset: number) => {
+ const presets = await pickerPresets(env.DB, actor.user.id, { chat: "private" });
+ const vars = await presetVars(env, commentId, actor.user);
+ // Select options cannot be disabled, so a preset that cannot prefill is left out.
+ const fits = presets.filter((p) => expandPreset(p.body_md, vars).length <= MODAL_MAX);
+ const hasMore = fits.length - offset > PICK_PAGE + 1;
+ const shown = fits.slice(offset, offset + (hasMore ? PICK_PAGE : PICK_PAGE + 1));
+ const options = [
+ ...shown.map((p) => ({
+ label: p.title.length > 100 ? `${p.title.slice(0, 99)}β¦` : p.title || "(untitled)",
+ value: p.id,
+ })),
+ CUSTOM_OPTION,
+ ...(hasMore ? [{ label: "Moreβ¦", value: "more" }] : []),
+ ];
+ return {
+ content: ["Pick a saved reply, or write your own.", ...(fits.length < presets.length ? [MSG.tooLong] : [])].join("\n"),
+ embeds: [],
+ components: [
+ {
+ type: 1,
+ components: [
+ {
+ type: 3,
+ custom_id: encodePayload("discord", "pick", commentId, [origin, String(offset)]),
+ placeholder: "Saved replies",
+ options,
+ },
+ ],
+ },
+ ],
+ };
+};
+
+type ActorRow = { id: string; name: string; role: string; is_banned: number; erased_at: number | null; linked: number };
+
+// Modal-opening path: at most 2 D1 reads, no writes. Mirrors the actor
+// precedence of resolveDiscordActor without creating a role-holder row; the
+// submit re-authorizes through the full resolution.
+const openReplyModal = async (
+ ctx: Ctx,
+ i: DiscordInteraction,
+ cfg: { guildId: string; modRoleId: string; adminRoleId: string },
+ commentId: string,
+ extra: string[],
+ value: string,
+): Promise => {
+ const uid = senderId(i) as string;
+ if (!cfg.guildId || i.guild_id !== cfg.guildId) return refusal("wrong_guild");
+ const { results } = await ctx.env.DB.prepare(
+ `SELECT u.id, u.name, u.role, u.is_banned, u.erased_at,
+ (u.id = (SELECT user_id FROM discord_links WHERE discord_user_id = ?1)) AS linked
+ FROM users u
+ WHERE u.id = (SELECT user_id FROM discord_links WHERE discord_user_id = ?1)
+ OR (u.provider = 'discord' AND u.provider_id = ?1)`,
+ )
+ .bind(uid)
+ .all();
+ const live = (results ?? []).filter((r) => !r.erased_at);
+ if (live.some((r) => r.is_banned)) return refusal("banned");
+ const user = live.find((r) => r.linked) ?? live[0] ?? null;
+ const roles = i.member?.roles ?? [];
+ const granted = [cfg.adminRoleId, cfg.modRoleId].some((id) => id && roles.includes(id));
+ if (!user && !granted) return refusal("not_linked");
+ if (!granted && user?.role !== "mod" && user?.role !== "admin") return refusal("not_authorized");
+
+ const presetId = value === "custom" ? null : value;
+ let prefill = "";
+ if (presetId) {
+ const r = await ctx.env.DB.prepare(
+ `SELECT sr.*, a.name AS author_name, c.post_slug, p.title AS post_title
+ FROM saved_replies sr
+ LEFT JOIN comments c ON c.id = ?2
+ LEFT JOIN users a ON a.id = c.user_id
+ LEFT JOIN posts p ON p.slug = c.post_slug
+ WHERE sr.id = ?1`,
+ )
+ .bind(presetId, commentId)
+ .first();
+ if (!r || !presetVisible(r, user?.id ?? "", { chat: "private" })) {
+ return ephemeral(REPLY_REFUSAL("saved_reply_not_visible"));
+ }
+ prefill = expandPreset(r.body_md, {
+ name: r.author_name ?? undefined,
+ post: r.post_slug ? r.post_title || r.post_slug : undefined,
+ mod: user?.name ?? displayName(i),
+ });
+ if (prefill.length > MODAL_MAX) return ephemeral(MSG.tooLong);
+ }
+ return {
+ type: 9,
+ data: {
+ custom_id: encodePayload("discord", "rsub", commentId, [extra[0] ?? "q", ...(presetId ? [presetId] : [])]),
+ title: "Reply to comment",
+ components: [
+ {
+ type: 1,
+ components: [
+ {
+ type: 4,
+ custom_id: "body",
+ style: 2,
+ label: "Reply",
+ required: true,
+ max_length: MODAL_MAX,
+ ...(prefill && { value: prefill }),
+ },
+ ],
+ },
+ ],
+ },
+ };
+};
+
+const submitReply = async (
+ ctx: Ctx,
+ i: DiscordInteraction,
+ actor: OpActor,
+ commentId: string,
+ extra: string[],
+): Promise => {
+ const { env } = ctx;
+ const [origin = "", presetId] = extra;
+ const text = i.data?.components?.flatMap((r) => r.components).find((x) => x.custom_id === "body")?.value ?? "";
+ // postModeratorReply refuses a preset the actor cannot see (same rule as presetVisible, private chat).
+ const res = await postModeratorReply({
+ env,
+ executionCtx: ctx.executionCtx,
+ reqUrl: env.PUBLIC_BASE_URL ?? "https://invalid.local/",
+ actor: actor.user,
+ commentId,
+ bodyMd: text,
+ savedReplyId: presetId,
+ });
+ if (!res.ok) return ephemeral(REPLY_REFUSAL(res.error, res.max));
+ const doneBy = `Reply posted by ${actor.user.name}`;
+ if (origin === "q") return update(await queueMessage(env, commentId, doneBy));
+
+ const channel = i.channel_id ?? i.message?.channel_id ?? "";
+ if (env.DISCORD_BOT_TOKEN && SNOWFLAKE_RE.test(origin) && SNOWFLAKE_RE.test(channel)) {
+ const job = patchAlert(env, env.DISCORD_BOT_TOKEN, channel, origin, commentId, doneBy).catch((err) =>
+ log.error("discord.reply_patch", { error: String(err) }),
+ );
+ if (ctx.executionCtx) ctx.executionCtx.waitUntil(job);
+ else await job;
+ }
+ return update({ content: "Reply posted β ", embeds: [], components: [] });
+};
+
+// Embeds are omitted so the alert keeps its card; a failed PATCH never undoes the reply.
+const patchAlert = async (
+ env: Bindings,
+ token: string,
+ channel: string,
+ messageId: string,
+ commentId: string,
+ doneBy: string,
+): Promise => {
+ const [comment, counts] = await Promise.all([
+ getComment(env.DB, commentId),
+ countOpenReportsByComment(env.DB, [commentId]),
+ ]);
+ const res = await discordApi(token, "PATCH", `/channels/${channel}/messages/${messageId}`, {
+ content: escapeDiscordMentions(doneBy),
+ components: buttonRows(
+ buttonsFor(comment?.status, { reported: (counts[commentId] ?? 0) > 0, banLabel: "banr" }),
+ commentId,
+ ),
+ allowed_mentions: NO_MENTIONS,
+ });
+ if (!res.ok) log.warn("discord.reply_patch_failed", { status: res.status });
+};
+
const queueMessage = async (env: Bindings, commentId: string, doneBy?: string) => {
const comment = await getComment(env.DB, commentId);
if (!comment) return { content: MSG.gone, embeds: [], components: [] };
diff --git a/tests/discord-interactions.test.ts b/tests/discord-interactions.test.ts
index 853770c7..b4c5f94e 100644
--- a/tests/discord-interactions.test.ts
+++ b/tests/discord-interactions.test.ts
@@ -349,15 +349,4 @@ describe("POST /discord/interactions", () => {
expect(end.json.data.content).toBe("Queue is empty π");
expect(end.json.data.components).toEqual([]);
});
-
- it("reply, pick and rsub answer not-available until Task 12", async () => {
- const cid = await seedComment("pending");
- for (const id of [`g:reply:${cid}`, `g:pick:${cid}`]) {
- const r = await send(await component(asMod, id));
- expect(r.json.data.content).toBe("Reply from Discord is not available yet.");
- expect(r.json.data.flags).toBe(EPHEMERAL);
- }
- const m = await send(await modal(asMod, `g:rsub:${cid}`, "hi"));
- expect(m.json.data.content).toBe("Reply from Discord is not available yet.");
- });
});
diff --git a/tests/discord-reply.test.ts b/tests/discord-reply.test.ts
new file mode 100644
index 00000000..a515245c
--- /dev/null
+++ b/tests/discord-reply.test.ts
@@ -0,0 +1,386 @@
+/**
+ * POST /discord/interactions against REAL SQLite (every migration applied).
+ * Requests are Ed25519-signed with a generated key pair. fireWebhook and the
+ * global fetch are mocked: nothing in this route may reach the network.
+ */
+import { readdirSync, readFileSync } from "node:fs";
+import { join } from "node:path";
+import { DatabaseSync } from "node:sqlite";
+import { Hono } from "hono";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("../src/lib/webhook", async (importOriginal) => ({
+ ...(await importOriginal()),
+ fireWebhook: vi.fn(),
+}));
+
+import { insertComment, insertSavedReply } from "../src/db/queries";
+import { MAX_REPLY_DEPTH } from "../src/lib/tree";
+import type { Bindings } from "../src/index";
+import { loadDiscordSettings } from "../src/lib/settings";
+import { discord } from "../src/routes/discord";
+import { installMockCaches, uninstallMockCaches } from "./helpers/mock-caches";
+import { makeD1, makeKv } from "./helpers/admin-sqlite";
+
+const MIGRATIONS_DIR = join(__dirname, "../src/db/migrations");
+const GUILD = "100000000000000001";
+const MOD_ROLE = "200000000000000001";
+const ADMIN_ROLE = "200000000000000002";
+const MOD_DC = "300000000000000001";
+const NOBODY_DC = "300000000000000003";
+const AUTHOR = "01HAUTHOR000000000000000AB";
+const STAFF_AUTHOR = "01HSTAFF0000000000000000AB";
+const EPHEMERAL = 64;
+const MOD_USER = "01HMODUSER0000000000000AAA";
+const OTHER_MOD = "01HOTHERMOD000000000000AAA";
+const ALERT_MSG = "600000000000000001";
+const ALERT_CH = "700000000000000001";
+
+const hex = (b: ArrayBuffer) =>
+ [...new Uint8Array(b)].map((x) => x.toString(16).padStart(2, "0")).join("");
+
+let keys: CryptoKeyPair;
+let publicHex: string;
+let sqlite: DatabaseSync;
+let kv: ReturnType;
+let env: Bindings;
+let fetchMock: ReturnType;
+
+const pending: Promise[] = [];
+const execCtx = {
+ waitUntil(p: Promise) {
+ pending.push(p);
+ },
+ passThroughOnException() {},
+} as unknown as ExecutionContext;
+const settle = () => Promise.all(pending.splice(0));
+
+const setting = (key: string, value: string) =>
+ sqlite.prepare("INSERT OR REPLACE INTO settings (key, value, updated_at) VALUES (?, ?, 1)").run(key, value);
+
+const addUser = (id: string, role: "user" | "mod" | "admin", name: string, provider = "github", banned = 0) =>
+ sqlite
+ .prepare(
+ "INSERT INTO users (id, provider, provider_id, name, role, is_admin, is_banned, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
+ )
+ .run(id, provider, id, name, role, role === "admin" ? 1 : 0, banned, 1_700_000_000_000);
+
+const seedComment = async (status: "pending" | "approved" = "pending", userId = AUTHOR) =>
+ (
+ await insertComment(env.DB, {
+ post_slug: "hello",
+ parent_id: null,
+ user_id: userId,
+ body_md: "a comment",
+ body_html: "
a comment
",
+ renderer_version: 1,
+ status,
+ ip_hash: null,
+ user_agent: null,
+ depth: 1,
+ })
+ ).id;
+
+const link = (dc: string, userId: string) =>
+ sqlite
+ .prepare("INSERT INTO discord_links (discord_user_id, user_id, linked_at) VALUES (?, ?, 1)")
+ .run(dc, userId);
+
+const signedRequest = async (body: unknown, opts: { ts?: number; sig?: string } = {}) => {
+ const raw = typeof body === "string" ? body : JSON.stringify(body);
+ const ts = String(opts.ts ?? Math.floor(Date.now() / 1000));
+ const sig =
+ opts.sig ??
+ hex(await crypto.subtle.sign({ name: "Ed25519" }, keys.privateKey, new TextEncoder().encode(ts + raw)));
+ return {
+ method: "POST",
+ headers: { "x-signature-ed25519": sig, "x-signature-timestamp": ts, "content-type": "application/json" },
+ body: raw,
+ };
+};
+
+const send = async (body: unknown, opts: { ts?: number; sig?: string; env?: Bindings } = {}) => {
+ const app = new Hono<{ Bindings: Bindings }>().route("/discord", discord);
+ const res = await app.request(
+ "/discord/interactions",
+ await signedRequest(body, opts),
+ (opts.env ?? env) as unknown as Record,
+ execCtx,
+ );
+ return { status: res.status, json: (await res.json().catch(() => null)) as any };
+};
+
+const base = (dc: string, over: Record = {}) => ({
+ id: "400000000000000001",
+ token: "tok",
+ application_id: "500000000000000001",
+ guild_id: GUILD,
+ member: { user: { id: dc, username: "uname", global_name: "Display" }, roles: [] as string[] },
+ ...over,
+});
+const asMod = (over: Record = {}) =>
+ base(MOD_DC, { ...over, member: { user: { id: MOD_DC, username: "modu", global_name: "ModDisplay" }, roles: [MOD_ROLE] } });
+
+
+
+beforeEach(async () => {
+ keys = (await crypto.subtle.generateKey({ name: "Ed25519" }, true, ["sign", "verify"])) as CryptoKeyPair;
+ publicHex = hex((await crypto.subtle.exportKey("raw", keys.publicKey)) as ArrayBuffer);
+ sqlite = new DatabaseSync(":memory:");
+ for (const f of readdirSync(MIGRATIONS_DIR).filter((x) => x.endsWith(".sql")).sort()) {
+ sqlite.exec(readFileSync(join(MIGRATIONS_DIR, f), "utf8"));
+ }
+ kv = makeKv();
+ env = {
+ DB: makeD1(sqlite),
+ OAUTH_STATE: kv,
+ SESSIONS: makeKv(),
+ RATE_LIMITS: makeKv(),
+ TREE_CACHE: makeKv(),
+ ANALYTICS: { writeDataPoint() {} },
+ IP_HASH_SECRET: "test-secret",
+ ENV: "dev",
+ PUBLIC_BASE_URL: "https://comments.example.com",
+ DISCORD_BOT_TOKEN: "bot-token",
+ DISCORD_PUBLIC_KEY: publicHex,
+ } as unknown as Bindings;
+ setting("discord_guild_id", GUILD);
+ setting("discord_mod_role_id", MOD_ROLE);
+ setting("discord_admin_role_id", ADMIN_ROLE);
+ addUser(MOD_USER, "mod", "Mod One");
+ addUser(OTHER_MOD, "mod", "Mod Two");
+ link(MOD_DC, MOD_USER);
+ addUser(AUTHOR, "user", "Author", "anon");
+ addUser(STAFF_AUTHOR, "mod", "StaffAuthor");
+ sqlite.prepare("INSERT INTO posts (slug, created_at) VALUES ('hello', 1)").run();
+ installMockCaches();
+ fetchMock = vi.fn(async () => {
+ throw new Error("unexpected outbound fetch");
+ });
+ vi.stubGlobal("fetch", fetchMock);
+});
+
+afterEach(() => {
+ uninstallMockCaches();
+ vi.unstubAllGlobals();
+ vi.restoreAllMocks();
+});
+
+
+const component = (who: (o: Record) => any, custom_id: string) =>
+ who({ type: 3, data: { custom_id }, message: { id: ALERT_MSG, channel_id: ALERT_CH, embeds: [{ title: "Alert" }] } });
+const preset = (owner: string, title: string, body_md: string, scope: "private" | "shared" = "shared") =>
+ insertSavedReply(env.DB, { owner_id: owner, title, body_md, scope });
+const select = (who: (o: Record) => any, custom_id: string, value: string) =>
+ send(who({
+ type: 3,
+ channel_id: ALERT_CH,
+ data: { custom_id, values: [value], component_type: 3 },
+ message: { id: "800000000000000001", channel_id: ALERT_CH, flags: EPHEMERAL },
+ }));
+const submit = (who: (o: Record) => any, custom_id: string, text: string) =>
+ send(who({
+ type: 5,
+ channel_id: ALERT_CH,
+ data: { custom_id, components: [{ components: [{ custom_id: "body", value: text }] }] },
+ message: { id: "800000000000000001", channel_id: ALERT_CH, flags: EPHEMERAL },
+ }));
+const options = (r: any) => r.json.data.components[0].components[0].options as { label: string; value: string }[];
+const replyCount = (parent: string) =>
+ (sqlite.prepare("SELECT COUNT(*) AS n FROM comments WHERE parent_id = ?").get(parent) as { n: number }).n;
+const alertTap = (cid: string) =>
+ send({
+ ...asMod({ type: 3, data: { custom_id: `g:reply:${cid}` } }),
+ channel_id: ALERT_CH,
+ message: { id: ALERT_MSG, channel_id: ALERT_CH, embeds: [{ title: "Alert" }] },
+ });
+
+describe("Discord reply flow", () => {
+ it("alert Reply tap answers an ephemeral select with the alert message id as origin", async () => {
+ const cid = await seedComment("approved");
+ await preset(MOD_USER, "Thanks", "thanks {name}");
+ const r = await alertTap(cid);
+ expect(r.json.type).toBe(4);
+ expect(r.json.data.flags).toBe(EPHEMERAL);
+ expect(r.json.data.allowed_mentions).toEqual({ parse: [] });
+ expect(r.json.data.components[0].components[0].custom_id).toBe(`g:pick:${cid}:${ALERT_MSG}:0`);
+ });
+
+ it("queue Reply tap updates the card in place with origin q", async () => {
+ const cid = await seedComment("pending");
+ const r = await send(await component(asMod, `g:reply:${cid}:q`));
+ expect(r.json.type).toBe(7);
+ expect(r.json.data.components[0].components[0].custom_id).toBe(`g:pick:${cid}:q:0`);
+ });
+
+ it("lists own private plus shared presets, never another mod's private, with long labels cut", async () => {
+ const cid = await seedComment("approved");
+ await preset(MOD_USER, "t".repeat(150), "x", "private");
+ await preset(OTHER_MOD, "Theirs", "x", "private");
+ await preset(OTHER_MOD, "Shared one", "x", "shared");
+ const opts = options(await alertTap(cid));
+ const labels = opts.map((o) => o.label);
+ expect(labels).toContain("Shared one");
+ expect(labels).not.toContain("Theirs");
+ const cut = labels.find((l) => l.startsWith("ttt")) as string;
+ expect(cut).toBe(`${"t".repeat(99)}β¦`);
+ expect(opts.at(-1)).toEqual({ label: "βοΈ Write custom", value: "custom" });
+ expect(opts.some((o) => o.value === "more")).toBe(false);
+ });
+
+ it("pages 30 presets as 25 options then 8, More⦠moves the offset by 23 and opens no modal", async () => {
+ const cid = await seedComment("approved");
+ for (let n = 0; n < 30; n++) await preset(MOD_USER, `P${n}`, "x");
+ const p0 = await alertTap(cid);
+ const o0 = options(p0);
+ expect(o0).toHaveLength(25);
+ expect(o0.at(-2)).toEqual({ label: "βοΈ Write custom", value: "custom" });
+ expect(o0.at(-1)).toEqual({ label: "Moreβ¦", value: "more" });
+ const more = await select(asMod, `g:pick:${cid}:${ALERT_MSG}:0`, "more");
+ expect(more.json.type).toBe(7);
+ expect(more.json.data.components[0].components[0].custom_id).toBe(`g:pick:${cid}:${ALERT_MSG}:23`);
+ const o1 = options(more);
+ expect(o1).toHaveLength(8);
+ expect(o1.at(-1)?.value).toBe("custom");
+ const ids0 = new Set(o0.map((o) => o.value));
+ expect(o1.some((o) => ids0.has(o.value) && o.value.length > 6)).toBe(false);
+ });
+
+ it("leaves out presets whose expanded body exceeds 4000 and says so", async () => {
+ const cid = await seedComment("approved");
+ await preset(MOD_USER, "Big", "{mod}".repeat(700));
+ await preset(MOD_USER, "Small", "ok");
+ const r = await alertTap(cid);
+ const labels = options(r).map((o) => o.label);
+ expect(labels).toContain("Small");
+ expect(labels).not.toContain("Big");
+ expect(r.json.data.content).toContain("Some saved replies are too long for Discord. Use the dashboard for those.");
+ });
+
+ it("preset pick opens a prefilled modal within 2 D1 reads and no writes; custom opens it empty", async () => {
+ const cid = await seedComment("approved");
+ const p = await preset(MOD_USER, "Hi", "Hi {name} on {post} from {mod}");
+ await loadDiscordSettings(env); // settings are memoized, not part of the bound
+ const prepare = vi.spyOn(env.DB, "prepare");
+ const r = await select(asMod, `g:pick:${cid}:${ALERT_MSG}:0`, p.id);
+ const sql = prepare.mock.calls.map((c) => String(c[0]).trim());
+ expect(sql.length).toBeLessThanOrEqual(2);
+ for (const s of sql) expect(s.toUpperCase().startsWith("SELECT")).toBe(true);
+ expect(r.json.type).toBe(9);
+ expect(r.json.data.custom_id).toBe(`g:rsub:${cid}:${ALERT_MSG}:${p.id}`);
+ expect(r.json.data.title).toBe("Reply to comment");
+ const input = r.json.data.components[0].components[0];
+ expect(input).toMatchObject({ type: 4, custom_id: "body", style: 2, required: true, max_length: 4000 });
+ expect(input.value).toBe("Hi Author on hello from Mod One");
+ const c = await select(asMod, `g:pick:${cid}:${ALERT_MSG}:0`, "custom");
+ expect(c.json.type).toBe(9);
+ expect(c.json.data.custom_id).toBe(`g:rsub:${cid}:${ALERT_MSG}`);
+ expect(c.json.data.components[0].components[0].value ?? "").toBe("");
+ });
+
+ it("modal open refuses another mod's private preset and non-staff with no modal", async () => {
+ const cid = await seedComment("approved");
+ const theirs = await preset(OTHER_MOD, "Secret", "secret body", "private");
+ const r = await select(asMod, `g:pick:${cid}:${ALERT_MSG}:0`, theirs.id);
+ expect(r.json.type).toBe(4);
+ expect(JSON.stringify(r.json)).not.toContain("secret body");
+ const nobody = await select(base.bind(null, NOBODY_DC) as any, `g:pick:${cid}:${ALERT_MSG}:0`, "custom");
+ expect(nobody.json.type).toBe(4);
+ });
+
+ it("alert submit posts the reply, updates the picker and PATCHes the alert in waitUntil", async () => {
+ const cid = await seedComment("pending");
+ const p = await preset(MOD_USER, "Hi", "Hi");
+ fetchMock.mockImplementation(async () => new Response("{}", { status: 200 }));
+ const r = await submit(asMod, `g:rsub:${cid}:${ALERT_MSG}:${p.id}`, "Thanks for the comment");
+ expect(r.json.type).toBe(7);
+ expect(r.json.data.content).toBe("Reply posted β ");
+ expect(r.json.data.components).toEqual([]);
+ expect(r.json.data.allowed_mentions).toEqual({ parse: [] });
+ expect(replyCount(cid)).toBe(1);
+ const audit = sqlite.prepare("SELECT meta FROM audit_log WHERE action = 'comment.reply'").get() as { meta: string };
+ expect(JSON.parse(audit.meta).saved_reply_id).toBe(p.id);
+ await settle();
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit];
+ expect(url).toBe(`https://discord.com/api/v10/channels/${ALERT_CH}/messages/${ALERT_MSG}`);
+ expect(init.method).toBe("PATCH");
+ expect((init.headers as Record).authorization).toBe("Bot bot-token");
+ const body = JSON.parse(init.body as string);
+ expect(body.content).toContain("Reply posted by Mod One");
+ expect("embeds" in body).toBe(false);
+ expect(body.allowed_mentions).toEqual({ parse: [] });
+ expect(JSON.stringify(body.components)).toContain(`g:banr:${cid}`);
+ expect(JSON.stringify(body.components)).toContain(`g:approve:${cid}`);
+ });
+
+ it("a failed alert PATCH leaves the reply posted", async () => {
+ const cid = await seedComment("approved");
+ fetchMock.mockImplementation(async () => new Response("{}", { status: 500 }));
+ const r = await submit(asMod, `g:rsub:${cid}:${ALERT_MSG}`, "hello");
+ expect(r.json.type).toBe(7);
+ await settle();
+ expect(fetchMock).toHaveBeenCalledTimes(1);
+ expect(replyCount(cid)).toBe(1);
+ });
+
+ it("queue submit re-renders the queue card with done-by and Next and makes no PATCH", async () => {
+ const cid = await seedComment("pending");
+ const r = await submit(asMod, `g:rsub:${cid}:q`, "hello");
+ expect(r.json.type).toBe(7);
+ expect(JSON.stringify(r.json.data.embeds)).toContain("Reply posted by Mod One");
+ expect(JSON.stringify(r.json.data.components)).toContain(`g:nx:${cid}:q`);
+ await settle();
+ expect(fetchMock).not.toHaveBeenCalled();
+ expect(replyCount(cid)).toBe(1);
+ });
+
+ it("refusals answer an ephemeral plain message and post nothing", async () => {
+ const deleted = await seedComment("approved");
+ sqlite.prepare("UPDATE comments SET status = 'deleted' WHERE id = ?").run(deleted);
+ const deep = await seedComment("approved");
+ sqlite.prepare("UPDATE comments SET depth = ? WHERE id = ?").run(MAX_REPLY_DEPTH, deep);
+ const ok = await seedComment("approved");
+ const theirs = await preset(OTHER_MOD, "Secret", "s", "private");
+ const cases: [string, string, string][] = [
+ [`g:rsub:${deleted}:${ALERT_MSG}`, "hi", "That comment was deleted."],
+ [`g:rsub:${deep}:${ALERT_MSG}`, "hi", "That thread is too deep to reply to. Reply higher up from the dashboard."],
+ [`g:rsub:${ok}:${ALERT_MSG}`, "x".repeat(20000), "Reply too long (max "],
+ [`g:rsub:${ok}:${ALERT_MSG}`, " ", "Reply text is empty."],
+ [`g:rsub:${ok}:${ALERT_MSG}:${theirs.id}`, "hi", "That saved reply isn't available here."],
+ [`g:rsub:01HNOSUCHCOMMENT00000000AA:${ALERT_MSG}`, "hi", "That comment no longer exists."],
+ ];
+ for (const [id, text, msg] of cases) {
+ const r = await submit(asMod, id, text);
+ expect(r.json.type).toBe(4);
+ expect(r.json.data.flags).toBe(EPHEMERAL);
+ expect(r.json.data.content).toContain(msg);
+ }
+ await settle();
+ expect(fetchMock).not.toHaveBeenCalled();
+ expect(replyCount(deleted) + replyCount(deep) + replyCount(ok)).toBe(0);
+ });
+
+ it("submit re-authorizes: a linked non-staff user cannot reply", async () => {
+ const cid = await seedComment("approved");
+ const r = await submit((o) => base(NOBODY_DC, o), `g:rsub:${cid}:${ALERT_MSG}`, "hi");
+ expect(r.json.type).toBe(4);
+ expect(replyCount(cid)).toBe(0);
+ });
+});
+
+describe("interaction envelope guards", () => {
+ it("400s a JSON body that is not an object", async () => {
+ for (const raw of ["null", "42", '"x"']) {
+ const r = await send(raw);
+ expect(r.status).toBe(400);
+ }
+ });
+
+ it("answers Unknown action for other interaction types before any D1 read", async () => {
+ const prepare = vi.spyOn(env.DB, "prepare");
+ const r = await send(asMod({ type: 4, data: { name: "x" } }));
+ expect(r.json.data.content).toBe("Unknown action.");
+ expect(prepare).not.toHaveBeenCalled();
+ });
+});
From 641431b56a63605dfde803a01f32917cb01b4109 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 00:17:46 -0500
Subject: [PATCH 23/36] fix(discord): let Discord-sign-in admins open the ban
modal
---
src/routes/discord.ts | 74 +++++++++++++++++-------------
tests/discord-interactions.test.ts | 17 +++++++
2 files changed, 60 insertions(+), 31 deletions(-)
diff --git a/src/routes/discord.ts b/src/routes/discord.ts
index 6efa9f1b..e17abe32 100644
--- a/src/routes/discord.ts
+++ b/src/routes/discord.ts
@@ -11,7 +11,6 @@ import { Hono } from "hono";
import {
countOpenReportsByComment,
getComment,
- getDiscordLinkByDiscordUser,
getUser,
nextPendingAfter,
type SavedReply,
@@ -222,19 +221,13 @@ const dispatch = async (ctx: Ctx, i: DiscordInteraction): Promise => {
const openBanModal = async (
ctx: Ctx,
i: DiscordInteraction,
- cfg: { guildId: string; adminRoleId: string },
+ cfg: { guildId: string; modRoleId: string; adminRoleId: string },
commentId: string,
q: boolean,
): Promise => {
- const uid = senderId(i) as string;
- if (!cfg.guildId || i.guild_id !== cfg.guildId) return refusal("wrong_guild");
- let allowed = Boolean(cfg.adminRoleId) && (i.member?.roles ?? []).includes(cfg.adminRoleId);
- if (!allowed) {
- const link = await getDiscordLinkByDiscordUser(ctx.env.DB, uid);
- const user = link ? await getUser(ctx.env.DB, link.user_id) : null;
- allowed = Boolean(user && !user.erased_at && !user.is_banned && user.role === "admin");
- }
- if (!allowed) return ephemeral(MSG.noAccess);
+ const who = await lightResolveActor(ctx.env.DB, cfg, i);
+ if ("reason" in who) return refusal(who.reason);
+ if (effectiveRole(who) !== "admin") return ephemeral(MSG.noAccess);
return {
type: 9,
data: {
@@ -298,10 +291,43 @@ const pickerMessage = async (env: Bindings, actor: OpActor, commentId: string, o
};
type ActorRow = { id: string; name: string; role: string; is_banned: number; erased_at: number | null; linked: number };
+type LightActor = { user: ActorRow | null; grant: "mod" | "admin" | null };
-// Modal-opening path: at most 2 D1 reads, no writes. Mirrors the actor
-// precedence of resolveDiscordActor without creating a role-holder row; the
+// Modal-opening path: one D1 read, no writes. Mirrors the actor precedence of
+// resolveDiscordActor (erased rows ignored, any banned live row refuses, link
+// row wins over the provider row) without creating a role-holder row; the
// submit re-authorizes through the full resolution.
+const lightResolveActor = async (
+ db: D1Database,
+ cfg: { guildId: string; modRoleId: string; adminRoleId: string },
+ i: DiscordInteraction,
+): Promise => {
+ if (!cfg.guildId || i.guild_id !== cfg.guildId) return { reason: "wrong_guild" };
+ const uid = senderId(i) as string;
+ const { results } = await db
+ .prepare(
+ `SELECT u.id, u.name, u.role, u.is_banned, u.erased_at,
+ (u.id = (SELECT user_id FROM discord_links WHERE discord_user_id = ?1)) AS linked
+ FROM users u
+ WHERE u.id = (SELECT user_id FROM discord_links WHERE discord_user_id = ?1)
+ OR (u.provider = 'discord' AND u.provider_id = ?1)`,
+ )
+ .bind(uid)
+ .all();
+ const live = (results ?? []).filter((r) => !r.erased_at);
+ if (live.some((r) => r.is_banned)) return { reason: "banned" };
+ const roles = i.member?.roles ?? [];
+ const has = (id: string) => Boolean(id) && roles.includes(id);
+ return {
+ user: live.find((r) => r.linked) ?? live[0] ?? null,
+ grant: has(cfg.adminRoleId) ? "admin" : has(cfg.modRoleId) ? "mod" : null,
+ };
+};
+
+/** Max of the role grant and the user row's role; null when neither reaches mod. */
+const effectiveRole = (a: LightActor): "mod" | "admin" | null =>
+ a.grant === "admin" || a.user?.role === "admin" ? "admin" : a.grant === "mod" || a.user?.role === "mod" ? "mod" : null;
+
const openReplyModal = async (
ctx: Ctx,
i: DiscordInteraction,
@@ -310,24 +336,10 @@ const openReplyModal = async (
extra: string[],
value: string,
): Promise => {
- const uid = senderId(i) as string;
- if (!cfg.guildId || i.guild_id !== cfg.guildId) return refusal("wrong_guild");
- const { results } = await ctx.env.DB.prepare(
- `SELECT u.id, u.name, u.role, u.is_banned, u.erased_at,
- (u.id = (SELECT user_id FROM discord_links WHERE discord_user_id = ?1)) AS linked
- FROM users u
- WHERE u.id = (SELECT user_id FROM discord_links WHERE discord_user_id = ?1)
- OR (u.provider = 'discord' AND u.provider_id = ?1)`,
- )
- .bind(uid)
- .all();
- const live = (results ?? []).filter((r) => !r.erased_at);
- if (live.some((r) => r.is_banned)) return refusal("banned");
- const user = live.find((r) => r.linked) ?? live[0] ?? null;
- const roles = i.member?.roles ?? [];
- const granted = [cfg.adminRoleId, cfg.modRoleId].some((id) => id && roles.includes(id));
- if (!user && !granted) return refusal("not_linked");
- if (!granted && user?.role !== "mod" && user?.role !== "admin") return refusal("not_authorized");
+ const who = await lightResolveActor(ctx.env.DB, cfg, i);
+ if ("reason" in who) return refusal(who.reason);
+ if (!effectiveRole(who)) return refusal(who.user ? "not_authorized" : "not_linked");
+ const { user } = who;
const presetId = value === "custom" ? null : value;
let prefill = "";
diff --git a/tests/discord-interactions.test.ts b/tests/discord-interactions.test.ts
index b4c5f94e..0a8ca415 100644
--- a/tests/discord-interactions.test.ts
+++ b/tests/discord-interactions.test.ts
@@ -312,6 +312,23 @@ describe("POST /discord/interactions", () => {
expect(fetchMock).not.toHaveBeenCalled();
});
+ it("banr: a Discord-sign-in admin (no link, no role grant) opens the modal; a banned one is refused", async () => {
+ const cid = await seedComment("pending");
+ const OAUTH_DC = "300000000000000009";
+ const asOauth = (over: Record = {}) =>
+ base(OAUTH_DC, { ...over, member: { user: { id: OAUTH_DC, username: "oa" }, roles: [] as string[] } });
+ sqlite
+ .prepare(
+ "INSERT INTO users (id, provider, provider_id, name, role, is_admin, is_banned, created_at) VALUES ('01HOAUTHADMIN000000000000A', 'discord', ?, 'OA', 'admin', 1, 0, 1)",
+ )
+ .run(OAUTH_DC);
+ const ok = await send(await component(asOauth, `g:banr:${cid}`));
+ expect(ok.json.type).toBe(9);
+ sqlite.prepare("UPDATE users SET is_banned = 1 WHERE id = '01HOAUTHADMIN000000000000A'").run();
+ const banned = await send(await component(asOauth, `g:banr:${cid}`));
+ expect(banned.json.type).toBe(4);
+ });
+
it("bsub bans with the reason and answers type 7; staff guard refuses with type 4", async () => {
const cid = await seedComment("pending");
const r = await send(await modal(asAdmin, `g:bsub:${cid}`, "spammer"));
From 31564ed6a26a2e8d9cb7aa6ef95aea279e114169 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 00:21:42 -0500
Subject: [PATCH 24/36] fix(opbot): bust the tree cache for the Worker's own
origin
---
src/lib/opbot/actions.ts | 4 +++-
src/routes/discord.ts | 7 ++++---
src/routes/telegram.ts | 17 ++++++++++++-----
tests/discord-interactions.test.ts | 13 +++++++++++++
4 files changed, 32 insertions(+), 9 deletions(-)
diff --git a/src/lib/opbot/actions.ts b/src/lib/opbot/actions.ts
index 055b6492..86e3bc74 100644
--- a/src/lib/opbot/actions.ts
+++ b/src/lib/opbot/actions.ts
@@ -56,6 +56,8 @@ export const runOpAction = async (args: {
action: keyof typeof VERB;
commentId: string;
reason?: string | null;
+ /** Origin the tree-cache key derives from; defaults to PUBLIC_BASE_URL. */
+ reqUrl?: string;
}): Promise<
| { ok: true; status: CommentStatus; doneBy: string; reported: boolean }
| { ok: false; error: ActionError }
@@ -86,7 +88,7 @@ export const runOpAction = async (args: {
const res = await moderateComment({
env,
executionCtx: args.executionCtx,
- reqUrl: env.PUBLIC_BASE_URL ?? "https://localhost",
+ reqUrl: args.reqUrl ?? env.PUBLIC_BASE_URL ?? "https://localhost",
adminId,
commentId,
action,
diff --git a/src/routes/discord.ts b/src/routes/discord.ts
index e17abe32..125dec18 100644
--- a/src/routes/discord.ts
+++ b/src/routes/discord.ts
@@ -56,7 +56,7 @@ import { escapeDiscordMentions } from "../lib/webhook-adapters";
export const discord = new Hono<{ Bindings: Bindings }>();
type Reply = Record;
-type Ctx = { env: Bindings; executionCtx: WaitUntilCtx };
+type Ctx = { env: Bindings; executionCtx: WaitUntilCtx; reqUrl: string };
const NO_MENTIONS = { parse: [] as [] };
@@ -141,7 +141,7 @@ discord.post("/interactions", async (c) => {
}
try {
- return c.json(await dispatch({ env: c.env, executionCtx: c.executionCtx }, i));
+ return c.json(await dispatch({ env: c.env, executionCtx: c.executionCtx, reqUrl: c.req.url }, i));
} catch (err) {
log.error("discord.handler", { error: String(err) });
return c.json(ephemeral("Something went wrong. Try again."));
@@ -403,7 +403,7 @@ const submitReply = async (
const res = await postModeratorReply({
env,
executionCtx: ctx.executionCtx,
- reqUrl: env.PUBLIC_BASE_URL ?? "https://invalid.local/",
+ reqUrl: ctx.reqUrl,
actor: actor.user,
commentId,
bodyMd: text,
@@ -473,6 +473,7 @@ const runAndRender = async (
const res = await runOpAction({
env: ctx.env,
executionCtx: ctx.executionCtx,
+ reqUrl: ctx.reqUrl,
actor,
action,
commentId,
diff --git a/src/routes/telegram.ts b/src/routes/telegram.ts
index 601e8921..1076be0d 100644
--- a/src/routes/telegram.ts
+++ b/src/routes/telegram.ts
@@ -150,9 +150,10 @@ telegram.post("/webhook", async (c) => {
token,
update.callback_query,
c.executionCtx,
+ c.req.url,
);
} else if (update.message) {
- await handleMessage(c.env, token, update.message, c.executionCtx);
+ await handleMessage(c.env, token, update.message, c.executionCtx, c.req.url);
}
} catch (err) {
log.error("telegram.handler", { error: String(err) });
@@ -167,6 +168,7 @@ const handleCallback = async (
token: string,
cq: TgCallbackQuery,
executionCtx: { waitUntil(p: Promise): void },
+ reqUrl: string,
): Promise => {
const decoded = decodeCallback(cq.data);
if (!decoded) {
@@ -200,7 +202,7 @@ const handleCallback = async (
decoded.action === "sr" ||
decoded.action === "rc"
) {
- await handleReplyTap(env, token, cq, decoded, extra, actor, executionCtx);
+ await handleReplyTap(env, token, cq, decoded, extra, actor, executionCtx, reqUrl);
return;
}
if (decoded.action === "banr") {
@@ -265,6 +267,7 @@ const handleCallback = async (
const res = await runOpAction({
env,
executionCtx,
+ reqUrl,
actor,
action,
commentId: decoded.commentId,
@@ -344,6 +347,7 @@ const handleReplyTap = async (
extra: string[],
actor: OpActor,
executionCtx: { waitUntil(p: Promise): void },
+ reqUrl: string,
): Promise => {
await answerCallbackQuery(token, cq.id);
const msg = cq.message;
@@ -390,7 +394,7 @@ const handleReplyTap = async (
const res = await postModeratorReply({
env,
executionCtx,
- reqUrl: env.PUBLIC_BASE_URL ?? "https://invalid.local/",
+ reqUrl,
actor: actor.user,
commentId: cid,
bodyMd: expandPreset(preset.body_md, await presetVars(env, cid, actor.user)),
@@ -431,6 +435,7 @@ const handlePromptAnswer = async (
prompt: RegExpMatchArray,
text: string,
executionCtx: { waitUntil(p: Promise): void },
+ reqUrl: string,
): Promise => {
const chatId = msg.chat.id;
const who = msg.from
@@ -454,7 +459,7 @@ const handlePromptAnswer = async (
const res = await postModeratorReply({
env,
executionCtx,
- reqUrl: env.PUBLIC_BASE_URL ?? "https://invalid.local/",
+ reqUrl,
actor: actor.user,
commentId: prompt[2] as string,
bodyMd: text,
@@ -471,6 +476,7 @@ const handlePromptAnswer = async (
const res = await runOpAction({
env,
executionCtx,
+ reqUrl,
actor,
action: "ban",
commentId: prompt[2] as string,
@@ -496,6 +502,7 @@ const handleMessage = async (
token: string,
msg: TgMessage,
executionCtx: { waitUntil(p: Promise): void },
+ reqUrl: string,
): Promise => {
const text = (msg.text ?? "").trim();
const chatId = msg.chat.id;
@@ -507,7 +514,7 @@ const handleMessage = async (
if (replied?.from?.id !== undefined && replied.from.id === botId) {
const prompt = (replied.text ?? "").match(PROMPT_RE);
if (prompt) {
- if (text) await handlePromptAnswer(env, token, msg, prompt, text, executionCtx);
+ if (text) await handlePromptAnswer(env, token, msg, prompt, text, executionCtx, reqUrl);
return;
}
}
diff --git a/tests/discord-interactions.test.ts b/tests/discord-interactions.test.ts
index 0a8ca415..5d65af42 100644
--- a/tests/discord-interactions.test.ts
+++ b/tests/discord-interactions.test.ts
@@ -17,6 +17,7 @@ vi.mock("../src/lib/webhook", async (importOriginal) => ({
import { insertComment } from "../src/db/queries";
import type { Bindings } from "../src/index";
import { loadDiscordSettings } from "../src/lib/settings";
+import { feedCacheKey } from "../src/lib/tree-cache";
import { discord } from "../src/routes/discord";
import { installMockCaches, uninstallMockCaches } from "./helpers/mock-caches";
import { makeD1, makeKv } from "./helpers/admin-sqlite";
@@ -329,6 +330,18 @@ describe("POST /discord/interactions", () => {
expect(banned.json.type).toBe(4);
});
+ it("approve busts the cache key of the request origin when PUBLIC_BASE_URL is unset", async () => {
+ const cid = await seedComment("pending");
+ const key = feedCacheKey("http://localhost/discord/interactions", "hello");
+ const cache = (globalThis as unknown as { caches: { default: { put(k: Request, r: Response): Promise; store: Map } } }).caches.default;
+ await cache.put(key, new Response("stale"));
+ expect(cache.store.has(key.url)).toBe(true);
+ const noBase = { ...env, PUBLIC_BASE_URL: undefined } as unknown as Bindings;
+ const r = await send(await component(asMod, `g:approve:${cid}`), { env: noBase });
+ expect(r.json.type).toBe(7);
+ expect(cache.store.has(key.url)).toBe(false);
+ });
+
it("bsub bans with the reason and answers type 7; staff guard refuses with type 4", async () => {
const cid = await seedComment("pending");
const r = await send(await modal(asAdmin, `g:bsub:${cid}`, "spammer"));
From ebc1d594978a52ed24f4cc91e81b129a8f463ce5 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 00:22:08 -0500
Subject: [PATCH 25/36] fix(opbot): truncate on code points so cards never
split an emoji
---
src/lib/opbot/cards.ts | 15 +++++++++++----
src/routes/discord.ts | 12 +++++++++---
src/routes/telegram.ts | 15 +++++++++------
tests/opbot-truncate.test.ts | 14 ++++++++++++++
4 files changed, 43 insertions(+), 13 deletions(-)
create mode 100644 tests/opbot-truncate.test.ts
diff --git a/src/lib/opbot/cards.ts b/src/lib/opbot/cards.ts
index fe76b40a..1e8b2e76 100644
--- a/src/lib/opbot/cards.ts
+++ b/src/lib/opbot/cards.ts
@@ -24,6 +24,16 @@ export type OpCard = {
buttons: OpButton[][];
};
+/** First `max` code points of `s`; unlike `slice` it never splits a surrogate pair. */
+export const truncateCodePoints = (s: string, max: number): string =>
+ s.length <= max ? s : Array.from(s).slice(0, max).join("");
+
+/** `truncateCodePoints` plus a trailing ellipsis when something was cut. */
+export const ellipsize = (s: string, max: number): string => {
+ const cut = truncateCodePoints(s, max);
+ return cut.length < s.length ? `${cut}β¦` : s;
+};
+
export const relativeAge = (since: number, now: number): string => {
const ms = Math.max(0, now - since);
const mins = Math.floor(ms / 60000);
@@ -84,10 +94,7 @@ export const renderQueueCard = async (
`Author: ${author?.name ?? "unknown"}`,
`On: ${comment.post_slug} Β· ${relativeAge(comment.created_at, Date.now())} ago`,
],
- snippet:
- comment.body_md.length > 200
- ? `${comment.body_md.slice(0, 200)}β¦`
- : comment.body_md,
+ snippet: ellipsize(comment.body_md, 200),
...(env.PUBLIC_BASE_URL && {
adminUrl: `${env.PUBLIC_BASE_URL.replace(/\/$/, "")}/admin/comments/${encodeURIComponent(comment.id)}`,
}),
diff --git a/src/routes/discord.ts b/src/routes/discord.ts
index 125dec18..76a11292 100644
--- a/src/routes/discord.ts
+++ b/src/routes/discord.ts
@@ -36,7 +36,13 @@ import {
runOpAction,
unbanUser,
} from "../lib/opbot/actions";
-import { buttonsFor, type OpCard, renderQueueCard } from "../lib/opbot/cards";
+import {
+ buttonsFor,
+ ellipsize,
+ type OpCard,
+ renderQueueCard,
+ truncateCodePoints,
+} from "../lib/opbot/cards";
import { decodePayload, encodePayload } from "../lib/opbot/codec";
import { type OpActor, resolveDiscordActor } from "../lib/opbot/identity";
import { consumeLinkToken } from "../lib/opbot/link-token";
@@ -186,7 +192,7 @@ const dispatch = async (ctx: Ctx, i: DiscordInteraction): Promise => {
if (action === "rsub") return submitReply(ctx, i, actor, commentId, payload.extra);
if (action !== "bsub") return ephemeral(MSG.unknown);
const input = i.data?.components?.flatMap((r) => r.components).find((x) => x.custom_id === "reason");
- const reason = input?.value?.trim().slice(0, 500) || null;
+ const reason = truncateCodePoints(input?.value?.trim() ?? "", 500) || null;
return runAndRender(ctx, i, actor, "ban", commentId, q, reason);
}
switch (action) {
@@ -562,7 +568,7 @@ const handleCommand = async (ctx: Ctx, i: DiscordInteraction, actor: OpActor): P
const r = await readComment(env, arg);
if (!r) return ephemeral(MSG.gone);
const { comment, authorName, openReports } = r;
- const snippet = comment.body_md.length > 200 ? `${comment.body_md.slice(0, 200)}β¦` : comment.body_md;
+ const snippet = ellipsize(comment.body_md, 200);
const url = adminUrl(env, arg);
return ephemeral(
[
diff --git a/src/routes/telegram.ts b/src/routes/telegram.ts
index 1076be0d..9ce3cc99 100644
--- a/src/routes/telegram.ts
+++ b/src/routes/telegram.ts
@@ -30,7 +30,13 @@ import { t } from "../i18n";
import type { Bindings } from "../index";
import { log } from "../lib/log";
import { hasRole, requiredRole, runOpAction, unbanUser } from "../lib/opbot/actions";
-import { buttonsFor, type OpCard, renderQueueCard } from "../lib/opbot/cards";
+import {
+ buttonsFor,
+ ellipsize,
+ type OpCard,
+ renderQueueCard,
+ truncateCodePoints,
+} from "../lib/opbot/cards";
import { decodePayload, encodePayload } from "../lib/opbot/codec";
import { type OpActor, resolveTelegramActor } from "../lib/opbot/identity";
import { consumeLinkToken, issueLinkToken } from "../lib/opbot/link-token";
@@ -480,7 +486,7 @@ const handlePromptAnswer = async (
actor,
action: "ban",
commentId: prompt[2] as string,
- reason: text.slice(0, 500),
+ reason: truncateCodePoints(text, 500),
});
await sendMessage(
token,
@@ -658,10 +664,7 @@ const renderCommentText = async (
const r = await readComment(env, id);
if (!r) return t("telegram.comment_not_found");
const { comment, authorName, openReports } = r;
- const snippet =
- comment.body_md.length > 200
- ? `${comment.body_md.slice(0, 200)}β¦`
- : comment.body_md;
+ const snippet = ellipsize(comment.body_md, 200);
const url = adminCommentUrl(env, id);
const lines = [
`Comment${escapeTg(id)}`,
diff --git a/tests/opbot-truncate.test.ts b/tests/opbot-truncate.test.ts
new file mode 100644
index 00000000..919b50d4
--- /dev/null
+++ b/tests/opbot-truncate.test.ts
@@ -0,0 +1,14 @@
+import { describe, expect, it } from "vitest";
+import { truncateCodePoints } from "../src/lib/opbot/cards";
+
+describe("truncateCodePoints", () => {
+ it("keeps short strings and cuts on code points, never inside a surrogate pair", () => {
+ expect(truncateCodePoints("abc", 5)).toBe("abc");
+ const s = `${"a".repeat(199)}πtail`;
+ expect(s.slice(0, 200).endsWith("\ud83d")).toBe(true); // UTF-16 slice splits the emoji
+ const cut = truncateCodePoints(s, 200);
+ expect(cut).toBe(`${"a".repeat(199)}π`);
+ expect(Array.from(cut)).toHaveLength(200);
+ expect(truncateCodePoints(s, 199)).toBe("a".repeat(199));
+ });
+});
From 6e3b4c94bac970bf41ad7028824b3b8b1ec3093b Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 00:23:12 -0500
Subject: [PATCH 26/36] refactor(discord): break the discord/webhook-adapters
import cycle
---
src/lib/discord.ts | 29 ++++++-----------------------
src/lib/opbot/codec.ts | 3 +++
src/lib/settings.ts | 2 +-
src/lib/webhook-adapters.ts | 25 +++++++++++++++++++++++--
4 files changed, 33 insertions(+), 26 deletions(-)
diff --git a/src/lib/discord.ts b/src/lib/discord.ts
index bea0f803..b1f8f3d9 100644
--- a/src/lib/discord.ts
+++ b/src/lib/discord.ts
@@ -2,13 +2,17 @@
// a thin REST client and OpCard -> message rendering. No Discord SDK.
import type { Bindings } from "../index";
import { log } from "./log";
-import type { OpButton, OpCard } from "./opbot/cards";
-import { encodePayload } from "./opbot/codec";
+import type { OpCard } from "./opbot/cards";
+import { SNOWFLAKE_RE } from "./opbot/codec";
import {
+ buttonRows,
escapeDiscordMentions,
sanitizeDiscordDescription,
} from "./webhook-adapters";
+// Re-exported so existing importers keep one Discord entry point.
+export { buttonRows, SNOWFLAKE_RE };
+
export type DiscordInteraction = {
id: string;
type: 1 | 2 | 3 | 5;
@@ -39,11 +43,9 @@ export type DiscordInteraction = {
export const DISCORD_API = "https://discord.com/api/v10";
export const EPHEMERAL = 64;
export const TIMESTAMP_WINDOW_S = 300;
-export const SNOWFLAKE_RE = /^\d{17,20}$/;
const API_TIMEOUT_MS = 5000;
const EMBED_COLOR = 0x5865f2;
-const STYLE = { primary: 1, secondary: 2, success: 3, danger: 4 } as const;
export const discordEnabled = (
env: Pick,
@@ -141,25 +143,6 @@ export const discordApi = async (
}
};
-/** Discord action rows (β€ 5 rows of β€ 5 buttons) for an OpButton grid. */
-export const buttonRows = (
- buttons: OpButton[][],
- commentId: string,
- extra: string[] = [],
-): unknown[] =>
- buttons
- .filter((row) => row.length > 0)
- .slice(0, 5)
- .map((row) => ({
- type: 1,
- components: row.slice(0, 5).map((b) => ({
- type: 2,
- style: STYLE[b.style],
- label: b.label,
- custom_id: encodePayload("discord", b.action, commentId, extra),
- })),
- }));
-
export const cardToMessage = (
card: OpCard,
extra: string[] = [],
diff --git a/src/lib/opbot/codec.ts b/src/lib/opbot/codec.ts
index 0d1ef67a..c95fd4da 100644
--- a/src/lib/opbot/codec.ts
+++ b/src/lib/opbot/codec.ts
@@ -62,3 +62,6 @@ export const decodePayload = (
if ((action === "rv") !== (commentId === "-")) return null;
return { action: action as OpAction, commentId, extra };
};
+
+/** A Discord snowflake id. Leaf constant so settings and webhook code need no Discord module. */
+export const SNOWFLAKE_RE = /^\d{17,20}$/;
diff --git a/src/lib/settings.ts b/src/lib/settings.ts
index e07fa3a2..a903e9ec 100644
--- a/src/lib/settings.ts
+++ b/src/lib/settings.ts
@@ -25,7 +25,7 @@ import { COMMENT_SORTS, getAllSettings } from "../db/queries";
import { LOCALES } from "../i18n";
import { AUTO_LOCALE } from "../i18n/negotiate";
import { DEFAULT_REACTION_KINDS, REACTION_KIND_SET } from "../widget/reactions";
-import { SNOWFLAKE_RE } from "./discord";
+import { SNOWFLAKE_RE } from "./opbot/codec";
import { CLEF_MODELS } from "./spam/workers-ai";
import { MAX_DEPTH } from "./tree";
diff --git a/src/lib/webhook-adapters.ts b/src/lib/webhook-adapters.ts
index a4266666..5f4a6cbc 100644
--- a/src/lib/webhook-adapters.ts
+++ b/src/lib/webhook-adapters.ts
@@ -33,8 +33,8 @@
*/
import type { CommentStatus } from "../db/queries";
import { getComment, getPost, getUser } from "../db/queries";
-import { buttonRows } from "./discord";
-import { buttonsFor } from "./opbot/cards";
+import { buttonsFor, type OpButton } from "./opbot/cards";
+import { encodePayload } from "./opbot/codec";
import { moderationKeyboard } from "./telegram";
import type { WebhookEvent, WebhookPayload } from "./webhook";
@@ -290,6 +290,27 @@ export const renderDiscordBody = async (
// Bot-posted variant: the same embed plus moderation buttons. The bot token is
// never part of the body (it is added to the request headers at send time).
+const BUTTON_STYLE = { primary: 1, secondary: 2, success: 3, danger: 4 } as const;
+
+/** Discord action rows (β€ 5 rows of β€ 5 buttons) for an OpButton grid. */
+export const buttonRows = (
+ buttons: OpButton[][],
+ commentId: string,
+ extra: string[] = [],
+): unknown[] =>
+ buttons
+ .filter((row) => row.length > 0)
+ .slice(0, 5)
+ .map((row) => ({
+ type: 1,
+ components: row.slice(0, 5).map((b) => ({
+ type: 2,
+ style: BUTTON_STYLE[b.style],
+ label: b.label,
+ custom_id: encodePayload("discord", b.action, commentId, extra),
+ })),
+ }));
+
export const renderDiscordBotBody = async (
db: WebhookAdapterDb,
payload: WebhookPayload,
From e7d95eae696ad631a55c559595aeaf4817a76840 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 00:23:51 -0500
Subject: [PATCH 27/36] refactor(opbot): share reply refusal text and the bot
rate limit
---
src/lib/opbot/reply.ts | 14 ++++++++++++
src/routes/discord.ts | 21 +++---------------
src/routes/telegram.ts | 42 ++++++++++++------------------------
tests/opbot-truncate.test.ts | 9 ++++++++
4 files changed, 40 insertions(+), 46 deletions(-)
diff --git a/src/lib/opbot/reply.ts b/src/lib/opbot/reply.ts
index ebdd5997..4704ddae 100644
--- a/src/lib/opbot/reply.ts
+++ b/src/lib/opbot/reply.ts
@@ -35,6 +35,20 @@ export type ReplyError =
| "body_too_long"
| "saved_reply_not_visible";
+/** Operator-facing refusal text shared by the Telegram and Discord bots (English by design). */
+export const replyRefusalText = (e: ReplyError, max?: number): string =>
+ e === "comment_not_found"
+ ? "That comment no longer exists."
+ : e === "comment_deleted"
+ ? "That comment was deleted."
+ : e === "thread_too_deep"
+ ? "That thread is too deep to reply to. Reply higher up from the dashboard."
+ : e === "body_too_long"
+ ? `Reply too long (max ${max} characters).`
+ : e === "body_required"
+ ? "Reply text is empty."
+ : "That saved reply isn't available here.";
+
type ChatKind = { chat: "group" | "private" };
export const presetVisible = (
diff --git a/src/routes/discord.ts b/src/routes/discord.ts
index 76a11292..72e7097b 100644
--- a/src/routes/discord.ts
+++ b/src/routes/discord.ts
@@ -52,7 +52,7 @@ import {
postModeratorReply,
presetVars,
presetVisible,
- type ReplyError,
+ replyRefusalText,
} from "../lib/opbot/reply";
import { readComment, readQueue, readStats, readUser, relativeAge } from "../lib/opbot/readouts";
import { checkRateLimit } from "../lib/ratelimit";
@@ -81,21 +81,6 @@ const MSG = {
unknown: "Unknown action.",
} as const;
-// Same wording as the Telegram REPLY_REFUSAL map in routes/telegram.ts; copied
-// rather than imported so this route does not pull in the Telegram handler.
-const REPLY_REFUSAL = (e: ReplyError, max?: number): string =>
- e === "comment_not_found"
- ? MSG.gone
- : e === "comment_deleted"
- ? "That comment was deleted."
- : e === "thread_too_deep"
- ? "That thread is too deep to reply to. Reply higher up from the dashboard."
- : e === "body_too_long"
- ? `Reply too long (max ${max} characters).`
- : e === "body_required"
- ? "Reply text is empty."
- : "That saved reply isn't available here.";
-
const ephemeral = (content: string): Reply => ({
type: 4,
data: { content, flags: EPHEMERAL, allowed_mentions: NO_MENTIONS },
@@ -361,7 +346,7 @@ const openReplyModal = async (
.bind(presetId, commentId)
.first();
if (!r || !presetVisible(r, user?.id ?? "", { chat: "private" })) {
- return ephemeral(REPLY_REFUSAL("saved_reply_not_visible"));
+ return ephemeral(replyRefusalText("saved_reply_not_visible"));
}
prefill = expandPreset(r.body_md, {
name: r.author_name ?? undefined,
@@ -415,7 +400,7 @@ const submitReply = async (
bodyMd: text,
savedReplyId: presetId,
});
- if (!res.ok) return ephemeral(REPLY_REFUSAL(res.error, res.max));
+ if (!res.ok) return ephemeral(replyRefusalText(res.error, res.max));
const doneBy = `Reply posted by ${actor.user.name}`;
if (origin === "q") return update(await queueMessage(env, commentId, doneBy));
diff --git a/src/routes/telegram.ts b/src/routes/telegram.ts
index 9ce3cc99..aa66c5aa 100644
--- a/src/routes/telegram.ts
+++ b/src/routes/telegram.ts
@@ -29,7 +29,13 @@ import {
import { t } from "../i18n";
import type { Bindings } from "../index";
import { log } from "../lib/log";
-import { hasRole, requiredRole, runOpAction, unbanUser } from "../lib/opbot/actions";
+import {
+ hasRole,
+ OPBOT_RATE_LIMIT,
+ requiredRole,
+ runOpAction,
+ unbanUser,
+} from "../lib/opbot/actions";
import {
buttonsFor,
ellipsize,
@@ -53,7 +59,7 @@ import {
postModeratorReply,
presetVars,
presetVisible,
- type ReplyError,
+ replyRefusalText,
} from "../lib/opbot/reply";
import { checkRateLimit } from "../lib/ratelimit";
import {
@@ -85,15 +91,8 @@ export const issueTelegramLinkToken = (
// ------------------------------- Route -------------------------------------
-// Inbound callers are authenticated operators (secret token β linked account β
-// per-action role check), so the comment-posting default (1/10s, 5/10min) is
-// far too strict β a moderator tapping through a queue or running a couple of
-// slash commands would be throttled mid-session. Keep a generous burst that
-// still caps a runaway loop or a redelivery storm.
-const TELEGRAM_RATE_LIMIT = {
- short: { max: 10, windowSec: 10 },
- long: { max: 120, windowSec: 600 },
-};
+// Rate limiting uses OPBOT_RATE_LIMIT, the shared operator-bot burst (callers
+// are authenticated operators, so the comment-posting default is too strict).
telegram.post("/webhook", async (c) => {
// 1. Authenticate the caller. This is the ONLY gate on an originless route.
@@ -124,7 +123,7 @@ telegram.post("/webhook", async (c) => {
if (fromId != null) {
const rl = await checkRateLimit(c.req.url, `tg:${fromId}`, {
scope: "telegram",
- config: TELEGRAM_RATE_LIMIT,
+ config: OPBOT_RATE_LIMIT,
env: c.env,
});
if (!rl.ok) {
@@ -330,19 +329,6 @@ const handleCallback = async (
// ------------------------------- Reply flow --------------------------------
-const REPLY_REFUSAL = (e: ReplyError, max?: number): string =>
- e === "comment_not_found"
- ? t("telegram.comment_not_found")
- : e === "comment_deleted"
- ? "That comment was deleted."
- : e === "thread_too_deep"
- ? "That thread is too deep to reply to. Reply higher up from the dashboard."
- : e === "body_too_long"
- ? `Reply too long (max ${max} characters).`
- : e === "body_required"
- ? "Reply text is empty."
- : "That saved reply isn't available here.";
-
const MAX_PRESET_BUTTONS = 99;
const handleReplyTap = async (
@@ -394,7 +380,7 @@ const handleReplyTap = async (
// sr: post the chosen preset.
const preset = extra[0] ? await getSavedReply(env.DB, extra[0]) : null;
if (!preset || !presetVisible(preset, actor.user.id, ctx)) {
- await sendMessage(token, chatId, REPLY_REFUSAL("saved_reply_not_visible"));
+ await sendMessage(token, chatId, replyRefusalText("saved_reply_not_visible"));
return;
}
const res = await postModeratorReply({
@@ -407,7 +393,7 @@ const handleReplyTap = async (
savedReplyId: preset.id,
});
if (!res.ok) {
- await sendMessage(token, chatId, REPLY_REFUSAL(res.error, res.max));
+ await sendMessage(token, chatId, replyRefusalText(res.error, res.max));
return;
}
await editMessageText(
@@ -475,7 +461,7 @@ const handlePromptAnswer = async (
chatId,
res.ok
? `Reply posted by ${escapeTg(actor.user.name)}`
- : REPLY_REFUSAL(res.error, res.max),
+ : replyRefusalText(res.error, res.max),
);
return;
}
diff --git a/tests/opbot-truncate.test.ts b/tests/opbot-truncate.test.ts
index 919b50d4..5f5b6f82 100644
--- a/tests/opbot-truncate.test.ts
+++ b/tests/opbot-truncate.test.ts
@@ -12,3 +12,12 @@ describe("truncateCodePoints", () => {
expect(truncateCodePoints(s, 199)).toBe("a".repeat(199));
});
});
+
+describe("replyRefusalText", () => {
+ it("matches the wording both bots show", async () => {
+ const { replyRefusalText } = await import("../src/lib/opbot/reply");
+ expect(replyRefusalText("comment_not_found")).toBe("That comment no longer exists.");
+ expect(replyRefusalText("body_too_long", 4000)).toBe("Reply too long (max 4000 characters).");
+ expect(replyRefusalText("saved_reply_not_visible")).toBe("That saved reply isn't available here.");
+ });
+});
From 3c4b52c00e0316af5930d3bd621e46797968ec21 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 00:24:43 -0500
Subject: [PATCH 28/36] fix(discord): validate channel targets strictly and
name permission errors
---
src/lib/discord.ts | 10 +++++++++-
src/lib/webhook.ts | 8 ++++++--
tests/discord-bot-adapter.test.ts | 28 ++++++++++++++++++++++++++++
3 files changed, 43 insertions(+), 3 deletions(-)
diff --git a/src/lib/discord.ts b/src/lib/discord.ts
index b1f8f3d9..d9dd8a71 100644
--- a/src/lib/discord.ts
+++ b/src/lib/discord.ts
@@ -180,9 +180,17 @@ export const validateDiscordChannel = async (
guildId: string,
): Promise<
| { ok: true }
- | { ok: false; error: "channel_not_found" | "channel_wrong_guild" | "channel_wrong_type" }
+ | {
+ ok: false;
+ error:
+ | "channel_not_found"
+ | "channel_forbidden"
+ | "channel_wrong_guild"
+ | "channel_wrong_type";
+ }
> => {
const r = await discordApi(token, "GET", `/channels/${channelId}`);
+ if (r.status === 401 || r.status === 403) return { ok: false, error: "channel_forbidden" };
if (!r.ok) return { ok: false, error: "channel_not_found" };
const ch = r.json as { type?: number; guild_id?: string } | undefined;
if (ch?.guild_id !== guildId) return { ok: false, error: "channel_wrong_guild" };
diff --git a/src/lib/webhook.ts b/src/lib/webhook.ts
index 9c80024f..1d093175 100644
--- a/src/lib/webhook.ts
+++ b/src/lib/webhook.ts
@@ -53,6 +53,7 @@ import {
} from "../db/queries";
import { log } from "./log";
import { DISCORD_API, discordEnabled } from "./discord";
+import { SNOWFLAKE_RE } from "./opbot/codec";
import { checkOutboundUrl } from "./url-safety";
import {
type AdapterOpts,
@@ -209,7 +210,8 @@ const resolveTarget = (
};
}
if (endpoint.adapter === "discord_bot") {
- if (!discordEnabled(env)) return null;
+ // Re-checked here: the row may predate validation or be hand-edited.
+ if (!discordEnabled(env) || !SNOWFLAKE_RE.test(endpoint.url)) return null;
return {
url: `${DISCORD_API}/channels/${endpoint.url}/messages`,
// Fixed, trusted host; the channel id was validated as a snowflake on save.
@@ -253,7 +255,9 @@ const postOnce = async (
status: 0,
error:
endpoint.adapter === "discord_bot"
- ? "discord:not_configured"
+ ? discordEnabled(env)
+ ? "discord:bad_channel"
+ : "discord:not_configured"
: "telegram:no_token",
};
}
diff --git a/tests/discord-bot-adapter.test.ts b/tests/discord-bot-adapter.test.ts
index f8072c36..2766e880 100644
--- a/tests/discord-bot-adapter.test.ts
+++ b/tests/discord-bot-adapter.test.ts
@@ -95,6 +95,10 @@ describe("saving a discord_bot endpoint", () => {
);
channel({}, 404);
expect(await (await save(h, CHANNEL)).json()).toEqual({ error: "channel_not_found" });
+ channel({}, 403);
+ expect(await (await save(h, CHANNEL)).json()).toEqual({ error: "channel_forbidden" });
+ channel({}, 401);
+ expect(await (await save(h, CHANNEL)).json()).toEqual({ error: "channel_forbidden" });
channel({ type: 0, guild_id: "888888888888888888" });
expect(await (await save(h, CHANNEL)).json()).toEqual({ error: "channel_wrong_guild" });
channel({ type: 2, guild_id: GUILD });
@@ -145,6 +149,30 @@ describe("delivery", () => {
});
});
+ it("refuses a stored non-snowflake channel with discord:bad_channel and never fetches", async () => {
+ const h = setup();
+ h.enable();
+ h.sqlite
+ .prepare(
+ `INSERT INTO webhook_endpoints (id, url, adapter, enabled, fail_count, created_at, updated_at)
+ VALUES ('e1', '../../guilds/1', 'discord_bot', 1, 0, 1, 1)`,
+ )
+ .run();
+ h.sqlite
+ .prepare(
+ `INSERT INTO webhook_deliveries (id, endpoint_id, event, payload, status, attempts, next_attempt_at, created_at)
+ VALUES ('d1', 'e1', 'comment.posted', '{}', 'pending', 0, 1, 1)`,
+ )
+ .run();
+ const fetchMock = vi.fn();
+ vi.stubGlobal("fetch", fetchMock);
+ await runWebhookRetries(h.env as never);
+ expect(fetchMock).not.toHaveBeenCalled();
+ expect(h.sqlite.prepare("SELECT last_error FROM webhook_deliveries").get()).toEqual({
+ last_error: "discord:bad_channel",
+ });
+ });
+
it("keeps the token out of logged error strings", async () => {
const h = setup();
h.enable();
From bd44abeb0bab6e7b684e40b31e878ee8f1eaee96 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 00:24:53 -0500
Subject: [PATCH 29/36] docs(discord): correct digest cadence and picker
details
---
docs/discord.md | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/docs/discord.md b/docs/discord.md
index bbaa9042..61548581 100644
--- a/docs/discord.md
+++ b/docs/discord.md
@@ -63,6 +63,12 @@ Open **`/admin/discord`** (admin only) and fill in:
| Admin role id | Optional. Members with this role act as `admin`. |
| Digest channel id | Optional. Channel for the daily digest. Empty = no Discord digest. |
+Garrul checks the digest channel when you save it. If the bot cannot see the
+channel (Discord answers 401 or 403), the save fails with `channel_forbidden`:
+check the bot's **View Channel** permission there. A channel that does not exist
+fails with `channel_not_found`. The same check runs when you save a Discord bot
+alert endpoint under **Webhooks**.
+
Enable Developer Mode in Discord (User Settings β Advanced) to copy ids.
Settings go through the settings memo, so a change can lag up to 60 s.
@@ -164,7 +170,8 @@ Discord limits shape the picker:
- A menu holds 25 options. **Write custom** takes one; when more saved replies
remain, **Moreβ¦** takes another, so a page shows 23 replies (up to 24 on the
last page).
-- Titles over 100 characters are cut with "β¦". Only the title is cut.
+- Titles over 100 characters are cut to 99 characters plus "β¦". Only the title
+ is cut.
- A modal holds 4000 characters. A saved reply whose expanded body is longer is
left out, and the picker says to use the dashboard for those. The bot never
posts a cut body.
@@ -176,8 +183,8 @@ edit fails, the reply stays posted and the failure is logged.
When the bot is on (token and public key) and a **Digest channel id** is set,
the existing `*/15` cron posts a once-daily summary (pending, open reports and
-most-flagged thread, 7-day spam rate, oldest pending) to that channel. It
-self-gates to roughly once every 24 hours using its own timestamp
+most-flagged thread, 7-day spam rate, banned users, oldest pending) to that
+channel. It self-gates to at most once every 23 hours using its own timestamp
(`dc_digest_last_sent_at`), independent of the Telegram digest. Because it goes
to a channel, role-only team mods see it too. A failed post is logged and
retried the next day.
From 4e2d8697075132a8d1f74047074a69d4b3f375e6 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 00:25:05 -0500
Subject: [PATCH 30/36] chore: regenerate release manifest
---
release-manifest.json | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/release-manifest.json b/release-manifest.json
index e9424a8e..2d1cf5ce 100644
--- a/release-manifest.json
+++ b/release-manifest.json
@@ -437,7 +437,8 @@
"0029_owner_login.sql",
"0030_thread_and_retention_indexes.sql",
"0031_admin_list_indexes.sql",
- "0032_page_engagement_totals.sql"
+ "0032_page_engagement_totals.sql",
+ "0033_discord_links.sql"
],
"breakingChanges": [
{
From 59ce11c76ee302f1bb01ec27b9f45efa8fc8a851 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 12:57:30 -0500
Subject: [PATCH 31/36] fix(opbot): find the ban reason past unrelated audit
rows
banState read the latest 20 audit rows for a user and searched them for a
ban. Twenty later notes or session actions pushed the ban out of that
window, so both bots reported the reason as purged. Filter for ban/unban
in SQL and take the latest match.
---
src/db/queries.ts | 16 ++++++++++++++++
src/lib/opbot/actions.ts | 5 ++---
tests/opbot-actions.test.ts | 16 ++++++++++++++++
3 files changed, 34 insertions(+), 3 deletions(-)
diff --git a/src/db/queries.ts b/src/db/queries.ts
index d6c2a5e0..30223ded 100644
--- a/src/db/queries.ts
+++ b/src/db/queries.ts
@@ -3311,6 +3311,22 @@ export const adminAuditForTarget = async (
return result.results ?? [];
};
+// Latest ban/unban row for a user. Filtered in SQL so unrelated audit rows can
+// never push the ban out of a fixed window.
+export const latestBanAudit = async (
+ db: D1Database,
+ user_id: string,
+): Promise<{ action: string; reason: string | null } | null> =>
+ db
+ .prepare(
+ `SELECT action, reason FROM audit_log
+ WHERE target_kind = 'user' AND target_id = ? AND action IN ('ban', 'unban')
+ ORDER BY created_at DESC, id DESC
+ LIMIT 1`,
+ )
+ .bind(user_id)
+ .first<{ action: string; reason: string | null }>();
+
// Map of {target_id: latestAuditRow} for a batch of comment IDs. Used to
// render the "approved by X Β· 3h ago" strip under each queue row in one
// extra query rather than N. Returns only the latest row per target.
diff --git a/src/lib/opbot/actions.ts b/src/lib/opbot/actions.ts
index 86e3bc74..ea0f6eb8 100644
--- a/src/lib/opbot/actions.ts
+++ b/src/lib/opbot/actions.ts
@@ -1,9 +1,9 @@
import {
- adminAuditForTarget,
type CommentStatus,
countOpenReportsByComment,
getComment,
getUser,
+ latestBanAudit,
type User,
} from "../../db/queries";
import type { Bindings } from "../../index";
@@ -131,8 +131,7 @@ export const banState = async (
user: User,
): Promise<{ banned: false } | { banned: true; reason: string | null; purged: boolean }> => {
if (!user.is_banned) return { banned: false };
- const rows = await adminAuditForTarget(db, "user", user.id, 20);
- const last = rows.find((r) => r.action === "ban" || r.action === "unban");
+ const last = await latestBanAudit(db, user.id);
// Banned with no ban row means the audit trail was purged.
if (!last) return { banned: true, reason: null, purged: true };
return { banned: true, reason: last.reason ?? null, purged: false };
diff --git a/tests/opbot-actions.test.ts b/tests/opbot-actions.test.ts
index 7608a7be..bf944b37 100644
--- a/tests/opbot-actions.test.ts
+++ b/tests/opbot-actions.test.ts
@@ -206,6 +206,22 @@ describe("banState", () => {
purged: false,
});
});
+
+ it("keeps the ban reason after more unrelated audit rows than any fixed window", async () => {
+ const { env, sqlite } = setup();
+ const real = await actorOf(env, "adm", "admin", false);
+ await runOpAction({ env, executionCtx: undefined, actor: real, action: "ban", commentId: "c1", reason: "abuse" });
+ const add = sqlite.prepare(
+ `INSERT INTO audit_log (id, admin_id, action, target_kind, target_id, created_at)
+ VALUES (?, 'adm', 'note', 'user', 'plain', ?)`,
+ );
+ for (let n = 0; n < 60; n++) add.run(`zz-note-${n}`, 9_000_000_000_000 + n);
+ expect(await banState(env.DB, (await getUser(env.DB, "plain")) as User)).toEqual({
+ banned: true,
+ reason: "abuse",
+ purged: false,
+ });
+ });
});
describe("readouts", () => {
From 4997ea960effdb3d29cfa0ecba23720775895d56 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 12:58:03 -0500
Subject: [PATCH 32/36] fix(telegram): mention the moderator in force-reply
prompts
The ban-reason and write-custom prompts used selective force_reply but
mentioned no one and replied to no one, so in a group no user got the
reply box. Mention the tapping moderator with a tg://user link so the
selective target exists.
---
src/routes/telegram.ts | 8 ++++++--
tests/telegram.test.ts | 5 +++--
2 files changed, 9 insertions(+), 4 deletions(-)
diff --git a/src/routes/telegram.ts b/src/routes/telegram.ts
index aa66c5aa..e2f4f834 100644
--- a/src/routes/telegram.ts
+++ b/src/routes/telegram.ts
@@ -216,7 +216,7 @@ const handleCallback = async (
await sendMessage(
token,
cq.message.chat.id,
- `Ban reason for comment ${escapeTg(decoded.commentId)} β send the reason as a reply to this message.`,
+ `Ban reason for comment ${escapeTg(decoded.commentId)} β send the reason as a reply to this message, ${mentionOf(cq.from.id, actor.user.name)}.`,
{ replyMarkup: { force_reply: true, selective: true } },
);
}
@@ -371,7 +371,7 @@ const handleReplyTap = async (
await sendMessage(
token,
chatId,
- `Reply to comment ${escapeTg(cid)} β send your text as a reply to this message.`,
+ `Reply to comment ${escapeTg(cid)} β send your text as a reply to this message, ${mentionOf(cq.from.id, actor.user.name)}.`,
{ replyMarkup: { force_reply: true, selective: true } },
);
return;
@@ -736,6 +736,10 @@ const handleStart = async (
// Quotes are escaped too because this also feeds href="..." attributes via
// tgLink β an unescaped quote there breaks the attribute and Telegram rejects
// the whole message.
+// selective force_reply targets only users @mentioned in the text, so a prompt
+// must mention the moderator who tapped or no one in a group gets the reply box.
+const mentionOf = (id: number, name: string): string => `${escapeTg(name)}`;
+
const escapeTg = (s: string): string =>
s
.replace(/&/g, "&")
diff --git a/tests/telegram.test.ts b/tests/telegram.test.ts
index 2858aaad..55ca166f 100644
--- a/tests/telegram.test.ts
+++ b/tests/telegram.test.ts
@@ -1019,7 +1019,7 @@ describe("POST /telegram/webhook β ban reason, /unban and /user ban state", ()
SECRET,
);
expect(lastText()).toBe(
- `Ban reason for comment ${id} β send the reason as a reply to this message.`,
+ `Ban reason for comment ${id} β send the reason as a reply to this message, Boss.`,
);
expect(replies().at(-1)?.body.reply_markup).toEqual({ force_reply: true, selective: true });
expect(tgCalls.find((c) => c.method === "answerCallbackQuery")?.body.text).toBeUndefined();
@@ -1228,7 +1228,8 @@ describe("POST /telegram/webhook β reply with saved replies", () => {
it("rc tap sends a force_reply prompt; the reply posts verbatim", async () => {
const id = await seedComment("approved");
await tap("43", encodeCallback("rc", id));
- const prompt = `Reply to comment ${id} β send your text as a reply to this message.`;
+ // selective force_reply targets only @mentioned users, so the tapping moderator is mentioned.
+ const prompt = `Reply to comment ${id} β send your text as a reply to this message, Moddy.`;
expect(lastText()).toBe(prompt);
expect(sent().at(-1)?.body.reply_markup).toEqual({ force_reply: true, selective: true });
await answerPrompt("43", "Thanks {name}", `Reply to comment ${id} β send your text as a reply to this message.`);
From 8236992c074c3a23a29874b5f45238c07055814f Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 13:00:58 -0500
Subject: [PATCH 33/36] fix(discord): defer slow interactions instead of
missing the 3 s deadline
Every interaction waited for the whole handler before answering. Discord
fails an interaction not acknowledged within 3 s even when the action
already committed, so a slow D1 round trip showed operators a failure and
invited a duplicate retry.
Race the handler against a 2.5 s budget. On time, answer as before. Late,
acknowledge with a deferred response (ephemeral type 5 for commands,
type 6 for buttons and modal submits) and deliver the result through the
interaction webhook. Modal-opening paths stay synchronous: a modal cannot
be deferred.
---
src/lib/discord.ts | 5 ++-
src/routes/discord.ts | 53 +++++++++++++++++-----
tests/discord-interactions.test.ts | 70 ++++++++++++++++++++++++++++++
3 files changed, 116 insertions(+), 12 deletions(-)
diff --git a/src/lib/discord.ts b/src/lib/discord.ts
index d9dd8a71..1e1694b5 100644
--- a/src/lib/discord.ts
+++ b/src/lib/discord.ts
@@ -94,8 +94,9 @@ export const verifyDiscordRequest = async (
}
};
+// token null: interaction webhooks authenticate by the interaction token in the path.
export const discordApi = async (
- token: string,
+ token: string | null,
method: "GET" | "POST" | "PATCH" | "PUT",
path: string,
body?: unknown,
@@ -111,7 +112,7 @@ export const discordApi = async (
const res = await fetch(`${DISCORD_API}${path}`, {
method,
headers: {
- authorization: `Bot ${token}`,
+ ...(token ? { authorization: `Bot ${token}` } : {}),
"content-type": "application/json",
},
...(body === undefined ? {} : { body: JSON.stringify(body) }),
diff --git a/src/routes/discord.ts b/src/routes/discord.ts
index 72e7097b..890ecf53 100644
--- a/src/routes/discord.ts
+++ b/src/routes/discord.ts
@@ -131,14 +131,48 @@ discord.post("/interactions", async (c) => {
if (!rl.ok) return c.json(ephemeral(MSG.slow));
}
- try {
- return c.json(await dispatch({ env: c.env, executionCtx: c.executionCtx, reqUrl: c.req.url }, i));
- } catch (err) {
+ const work = dispatch({ env: c.env, executionCtx: c.executionCtx, reqUrl: c.req.url }, i).catch((err) => {
log.error("discord.handler", { error: String(err) });
- return c.json(ephemeral("Something went wrong. Try again."));
- }
+ return ephemeral("Something went wrong. Try again.");
+ });
+ // A modal cannot be deferred; opening one does a fixed, small read.
+ if (opensModal(i, decodePayload(i.data?.custom_id))) return c.json(await work);
+
+ let timer: ReturnType | undefined;
+ const late = new Promise((r) => {
+ timer = setTimeout(() => r(null), DEFER_AFTER_MS);
+ });
+ const first = await Promise.race([work, late]);
+ clearTimeout(timer);
+ if (first) return c.json(first);
+ c.executionCtx.waitUntil(work.then((reply) => deliverLate(i, reply)));
+ return c.json(i.type === 2 ? { type: 5, data: { flags: EPHEMERAL } } : { type: 6 });
});
+// Discord fails an interaction not answered within 3 s, even when the action
+// already committed. Past this budget, acknowledge with a deferred response and
+// deliver the result through the interaction webhook.
+const DEFER_AFTER_MS = 2500;
+
+const opensModal = (i: DiscordInteraction, payload: ReturnType): boolean =>
+ i.type === 3 &&
+ (payload?.action === "banr" ||
+ payload?.action === "ban" ||
+ (payload?.action === "pick" && i.data?.values?.[0] !== "more"));
+
+const deliverLate = async (i: DiscordInteraction, reply: Reply): Promise => {
+ if (!SNOWFLAKE_RE.test(i.application_id ?? "") || !i.token) return;
+ const hook = `/webhooks/${i.application_id}/${encodeURIComponent(i.token)}`;
+ // A command was deferred as an ephemeral message, so its result edits that
+ // message. A component or modal was deferred as an update: type 7 edits the
+ // clicked message, an ephemeral notice goes out as a follow-up.
+ const res =
+ i.type === 2 || reply.type === 7
+ ? await discordApi(null, "PATCH", `${hook}/messages/@original`, reply.data)
+ : await discordApi(null, "POST", hook, reply.data);
+ if (!res.ok) log.error("discord.deliver_late", { status: res.status });
+};
+
const dispatch = async (ctx: Ctx, i: DiscordInteraction): Promise => {
const uid = senderId(i);
if (!uid) return refusal("wrong_guild");
@@ -151,11 +185,10 @@ const dispatch = async (ctx: Ctx, i: DiscordInteraction): Promise => {
// Opening the ban modal must stay within a fixed D1 read bound and write
// nothing, so it skips full actor resolution. The submit re-authorizes.
- if (i.type === 3 && (payload?.action === "banr" || payload?.action === "ban")) {
- return openBanModal(ctx, i, cfg, payload.commentId, payload.extra[0] === "q");
- }
- if (i.type === 3 && payload?.action === "pick" && i.data?.values?.[0] !== "more") {
- return openReplyModal(ctx, i, cfg, payload.commentId, payload.extra, i.data?.values?.[0] ?? "");
+ if (payload && opensModal(i, payload)) {
+ return payload.action === "pick"
+ ? openReplyModal(ctx, i, cfg, payload.commentId, payload.extra, i.data?.values?.[0] ?? "")
+ : openBanModal(ctx, i, cfg, payload.commentId, payload.extra[0] === "q");
}
const who = await resolveDiscordActor(ctx.env.DB, cfg, {
diff --git a/tests/discord-interactions.test.ts b/tests/discord-interactions.test.ts
index 5d65af42..1fb6e806 100644
--- a/tests/discord-interactions.test.ts
+++ b/tests/discord-interactions.test.ts
@@ -295,6 +295,76 @@ describe("POST /discord/interactions", () => {
expect(commentStatus(cid)).toBe("deleted");
});
+ // Sends one interaction whose every D1 result waits until the defer timer has fired,
+ // then lets the work finish and returns the response plus the late webhook calls.
+ const sendSlow = async (body: unknown) => {
+ let open!: () => void;
+ const gate = new Promise((r) => {
+ open = r;
+ });
+ const slow = (o: T): T =>
+ new Proxy(o, {
+ get(t, k) {
+ const v = Reflect.get(t, k);
+ if (typeof v !== "function") return v;
+ return (...a: unknown[]) => {
+ const r = v.apply(t, a);
+ if (r instanceof Promise) return gate.then(() => r);
+ return r && typeof r === "object" ? slow(r) : r;
+ };
+ },
+ });
+ const waits: Promise[] = [];
+ vi.spyOn(execCtx, "waitUntil").mockImplementation((p: Promise) => void waits.push(p));
+ fetchMock.mockImplementation(async () => new Response("{}", { status: 200 }));
+ vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
+ try {
+ let res: Awaited> | undefined;
+ const p = send(body, { env: { ...env, DB: slow(env.DB) } as Bindings }).then((r) => {
+ res = r;
+ });
+ for (let n = 0; n < 100 && !res; n++) await vi.advanceTimersByTimeAsync(500);
+ open();
+ await p;
+ await Promise.all(waits);
+ return { json: res?.json, calls: fetchMock.mock.calls as [string, RequestInit][] };
+ } finally {
+ open();
+ vi.useRealTimers();
+ }
+ };
+
+ it("a slow button defers with type 6 and edits the clicked message through the interaction webhook", async () => {
+ const cid = await seedComment("pending");
+ const { json, calls } = await sendSlow(await component(asMod, `g:approve:${cid}`));
+ expect(json).toEqual({ type: 6 });
+ expect(commentStatus(cid)).toBe("approved");
+ expect(calls).toHaveLength(1);
+ const [url, init] = calls[0] as [string, RequestInit];
+ expect(url).toBe("https://discord.com/api/v10/webhooks/500000000000000001/tok/messages/@original");
+ expect(init.method).toBe("PATCH");
+ expect(new Headers(init.headers).has("authorization")).toBe(false);
+ expect(JSON.parse(init.body as string).content).toBe("Approved by ModDisplay");
+ });
+
+ it("a slow command defers as ephemeral and edits that response with the result", async () => {
+ const { json, calls } = await sendSlow(await command(asMod, "stats"));
+ expect(json).toEqual({ type: 5, data: { flags: EPHEMERAL } });
+ const [url, init] = calls[0] as [string, RequestInit];
+ expect(url).toBe("https://discord.com/api/v10/webhooks/500000000000000001/tok/messages/@original");
+ expect(init.method).toBe("PATCH");
+ expect(JSON.parse(init.body as string).content).toContain("**Stats**");
+ });
+
+ it("a slow ephemeral notice on a button goes out as a follow-up", async () => {
+ const { json, calls } = await sendSlow(await component(asMod, "g:approve:01HNOPE0000000000000000000"));
+ expect(json).toEqual({ type: 6 });
+ const [url, init] = calls[0] as [string, RequestInit];
+ expect(url).toBe("https://discord.com/api/v10/webhooks/500000000000000001/tok");
+ expect(init.method).toBe("POST");
+ expect(JSON.parse(init.body as string).flags).toBe(EPHEMERAL);
+ });
+
it("banr opens a type 9 modal with at most 2 D1 reads and no writes", async () => {
const cid = await seedComment("pending");
await loadDiscordSettings(env); // settings are memoized per isolate
From 3023fe1b6017249f82c892567ec4ab839b24794a Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 14:42:05 -0500
Subject: [PATCH 34/36] test(discord): let real crypto settle before opening
the slow-path gate
sendSlow advanced fake timers 100 times without yielding to the real event
loop. Under suite load crypto.subtle.verify could outlast those turns, so the
gate opened before the defer timer existed and the command answered in time
(type 4), failing the deferred-response assertion about half the time.
---
tests/discord-interactions.test.ts | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/tests/discord-interactions.test.ts b/tests/discord-interactions.test.ts
index 1fb6e806..514d5a62 100644
--- a/tests/discord-interactions.test.ts
+++ b/tests/discord-interactions.test.ts
@@ -323,7 +323,14 @@ describe("POST /discord/interactions", () => {
const p = send(body, { env: { ...env, DB: slow(env.DB) } as Bindings }).then((r) => {
res = r;
});
- for (let n = 0; n < 100 && !res; n++) await vi.advanceTimersByTimeAsync(500);
+ // Signature verification (crypto.subtle) settles on the real event loop, not on
+ // fake timers, so yield real turns too or the gate can open before the defer
+ // timer exists.
+ const deadline = Date.now() + 5000;
+ while (!res && Date.now() < deadline) {
+ await vi.advanceTimersByTimeAsync(500);
+ await new Promise((r) => setImmediate(r));
+ }
open();
await p;
await Promise.all(waits);
From fc2fdf89c669405953ba6be7054825037074d92a Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sat, 14 Nov 2026 21:01:50 -0500
Subject: [PATCH 35/36] test(discord): use placeholder-shaped fixture usernames
---
tests/discord-interactions.test.ts | 10 +++++-----
tests/discord-reply.test.ts | 4 ++--
2 files changed, 7 insertions(+), 7 deletions(-)
diff --git a/tests/discord-interactions.test.ts b/tests/discord-interactions.test.ts
index 514d5a62..0fc91d91 100644
--- a/tests/discord-interactions.test.ts
+++ b/tests/discord-interactions.test.ts
@@ -105,13 +105,13 @@ const base = (dc: string, over: Record = {}) => ({
token: "tok",
application_id: "500000000000000001",
guild_id: GUILD,
- member: { user: { id: dc, username: "uname", global_name: "Display" }, roles: [] as string[] },
+ member: { user: { id: dc, username: "testuser", global_name: "Display" }, roles: [] as string[] },
...over,
});
const asMod = (over: Record = {}) =>
- base(MOD_DC, { ...over, member: { user: { id: MOD_DC, username: "modu", global_name: "ModDisplay" }, roles: [MOD_ROLE] } });
+ base(MOD_DC, { ...over, member: { user: { id: MOD_DC, username: "testmod", global_name: "ModDisplay" }, roles: [MOD_ROLE] } });
const asAdmin = (over: Record = {}) =>
- base(ADMIN_DC, { ...over, member: { user: { id: ADMIN_DC, username: "admu", global_name: null }, roles: [ADMIN_ROLE] } });
+ base(ADMIN_DC, { ...over, member: { user: { id: ADMIN_DC, username: "testadmin", global_name: null }, roles: [ADMIN_ROLE] } });
const command = (who: (o: Record) => any, name: string, value?: string, optName = "id") =>
who({ type: 2, data: { name, options: value === undefined ? [] : [{ name: optName, value }] } });
@@ -394,7 +394,7 @@ describe("POST /discord/interactions", () => {
const cid = await seedComment("pending");
const OAUTH_DC = "300000000000000009";
const asOauth = (over: Record = {}) =>
- base(OAUTH_DC, { ...over, member: { user: { id: OAUTH_DC, username: "oa" }, roles: [] as string[] } });
+ base(OAUTH_DC, { ...over, member: { user: { id: OAUTH_DC, username: "testoauth" }, roles: [] as string[] } });
sqlite
.prepare(
"INSERT INTO users (id, provider, provider_id, name, role, is_admin, is_banned, created_at) VALUES ('01HOAUTHADMIN000000000000A', 'discord', ?, 'OA', 'admin', 1, 0, 1)",
@@ -423,7 +423,7 @@ describe("POST /discord/interactions", () => {
const cid = await seedComment("pending");
const r = await send(await modal(asAdmin, `g:bsub:${cid}`, "spammer"));
expect(r.json.type).toBe(7);
- expect(r.json.data.content).toBe("Banned by admu");
+ expect(r.json.data.content).toBe("Banned by testadmin");
expect(isBanned(AUTHOR)).toBe(1);
const row = sqlite.prepare("SELECT reason FROM audit_log WHERE action = 'ban'").get() as any;
expect(row.reason).toBe("spammer");
diff --git a/tests/discord-reply.test.ts b/tests/discord-reply.test.ts
index a515245c..6dc2caf6 100644
--- a/tests/discord-reply.test.ts
+++ b/tests/discord-reply.test.ts
@@ -115,11 +115,11 @@ const base = (dc: string, over: Record = {}) => ({
token: "tok",
application_id: "500000000000000001",
guild_id: GUILD,
- member: { user: { id: dc, username: "uname", global_name: "Display" }, roles: [] as string[] },
+ member: { user: { id: dc, username: "testuser", global_name: "Display" }, roles: [] as string[] },
...over,
});
const asMod = (over: Record = {}) =>
- base(MOD_DC, { ...over, member: { user: { id: MOD_DC, username: "modu", global_name: "ModDisplay" }, roles: [MOD_ROLE] } });
+ base(MOD_DC, { ...over, member: { user: { id: MOD_DC, username: "testmod", global_name: "ModDisplay" }, roles: [MOD_ROLE] } });
From 21ffb7e309b215247c48fc2e96f496aceb2c0937 Mon Sep 17 00:00:00 2001
From: KingPin <28669+KingPin@users.noreply.github.com>
Date: Sun, 15 Nov 2026 00:25:49 -0500
Subject: [PATCH 36/36] fix(discord): count rate-limiter latency toward the
defer deadline
The defer timer started after checkRateLimit resolved, so a slow
limiter plus a slow D1 read could push the first response past
Discord's 3 s deadline. Run the limiter inside the raced work.
---
src/routes/discord.ts | 24 ++++++++--------
tests/discord-interactions.test.ts | 44 ++++++++++++++++++++++++++++--
2 files changed, 54 insertions(+), 14 deletions(-)
diff --git a/src/routes/discord.ts b/src/routes/discord.ts
index 890ecf53..020f0ed9 100644
--- a/src/routes/discord.ts
+++ b/src/routes/discord.ts
@@ -121,17 +121,19 @@ discord.post("/interactions", async (c) => {
// Anything but a command, component or modal submit: refuse before any lookup.
if (i.type !== 2 && i.type !== 3 && i.type !== 5) return c.json(ephemeral(MSG.unknown));
- const uid = senderId(i);
- if (uid) {
- const rl = await checkRateLimit(c.req.url, `dc:${uid}`, {
- scope: "discord",
- config: OPBOT_RATE_LIMIT,
- env: c.env,
- });
- if (!rl.ok) return c.json(ephemeral(MSG.slow));
- }
-
- const work = dispatch({ env: c.env, executionCtx: c.executionCtx, reqUrl: c.req.url }, i).catch((err) => {
+ // The rate limiter is inside `work` so its latency counts toward the deadline.
+ const work = (async (): Promise => {
+ const uid = senderId(i);
+ if (uid) {
+ const rl = await checkRateLimit(c.req.url, `dc:${uid}`, {
+ scope: "discord",
+ config: OPBOT_RATE_LIMIT,
+ env: c.env,
+ });
+ if (!rl.ok) return ephemeral(MSG.slow);
+ }
+ return dispatch({ env: c.env, executionCtx: c.executionCtx, reqUrl: c.req.url }, i);
+ })().catch((err) => {
log.error("discord.handler", { error: String(err) });
return ephemeral("Something went wrong. Try again.");
});
diff --git a/tests/discord-interactions.test.ts b/tests/discord-interactions.test.ts
index 0fc91d91..1b641b5d 100644
--- a/tests/discord-interactions.test.ts
+++ b/tests/discord-interactions.test.ts
@@ -297,7 +297,9 @@ describe("POST /discord/interactions", () => {
// Sends one interaction whose every D1 result waits until the defer timer has fired,
// then lets the work finish and returns the response plus the late webhook calls.
- const sendSlow = async (body: unknown) => {
+ // With `latency`, the rate limiter's cache reads and the first D1 call each take
+ // that much fake time instead, and nothing else is slow.
+ const sendSlow = async (body: unknown, latency?: { limiterMs: number; dbMs: number }) => {
let open!: () => void;
const gate = new Promise((r) => {
open = r;
@@ -309,7 +311,12 @@ describe("POST /discord/interactions", () => {
if (typeof v !== "function") return v;
return (...a: unknown[]) => {
const r = v.apply(t, a);
- if (r instanceof Promise) return gate.then(() => r);
+ if (r instanceof Promise) {
+ if (!latency) return gate.then(() => r);
+ const ms = latency.dbMs;
+ latency.dbMs = 0;
+ return new Promise((ok) => setTimeout(ok, ms)).then(() => r);
+ }
return r && typeof r === "object" ? slow(r) : r;
};
},
@@ -318,6 +325,14 @@ describe("POST /discord/interactions", () => {
vi.spyOn(execCtx, "waitUntil").mockImplementation((p: Promise) => void waits.push(p));
fetchMock.mockImplementation(async () => new Response("{}", { status: 200 }));
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
+ if (latency) {
+ const cache = (globalThis as unknown as { caches: { default: Cache } }).caches.default;
+ const match = cache.match.bind(cache);
+ vi.spyOn(cache, "match").mockImplementation(async (...a) => {
+ await new Promise((r) => setTimeout(r, latency.limiterMs));
+ return match(...a);
+ });
+ }
try {
let res: Awaited> | undefined;
const p = send(body, { env: { ...env, DB: slow(env.DB) } as Bindings }).then((r) => {
@@ -333,7 +348,16 @@ describe("POST /discord/interactions", () => {
}
open();
await p;
- await Promise.all(waits);
+ // Late work can still sit on fake timers (the `latency` delays).
+ let settled = false;
+ const all = Promise.all(waits).finally(() => {
+ settled = true;
+ });
+ while (!settled && Date.now() < deadline) {
+ await vi.advanceTimersByTimeAsync(500);
+ await new Promise((r) => setImmediate(r));
+ }
+ await all;
return { json: res?.json, calls: fetchMock.mock.calls as [string, RequestInit][] };
} finally {
open();
@@ -363,6 +387,20 @@ describe("POST /discord/interactions", () => {
expect(JSON.parse(init.body as string).content).toContain("**Stats**");
});
+ // Two limiter reads (2.2 s) plus one D1 read (0.4 s) pass the 2.5 s budget only
+ // together, so a defer timer started after the limiter would answer directly.
+ it("rate-limiter latency counts toward the defer deadline", async () => {
+ const cid = await seedComment("pending");
+ const { json, calls } = await sendSlow(await component(asMod, `g:approve:${cid}`), {
+ limiterMs: 1100,
+ dbMs: 400,
+ });
+ expect(json).toEqual({ type: 6 });
+ expect(commentStatus(cid)).toBe("approved");
+ expect(calls).toHaveLength(1);
+ expect(calls[0]?.[1].method).toBe("PATCH");
+ });
+
it("a slow ephemeral notice on a button goes out as a follow-up", async () => {
const { json, calls } = await sendSlow(await component(asMod, "g:approve:01HNOPE0000000000000000000"));
expect(json).toEqual({ type: 6 });