Discord: default reaction notifications to own

This commit is contained in:
Peter Steinberger
2026-01-03 18:48:10 +00:00
parent 7abd6713c8
commit 52458a5628
7 changed files with 145 additions and 33 deletions

View File

@@ -8,6 +8,7 @@ import {
resolveDiscordGuildEntry,
resolveDiscordReplyTarget,
resolveGroupDmAllow,
shouldEmitDiscordReactionNotification,
} from "./monitor.js";
const fakeGuild = (id: string, name: string) =>
@@ -21,6 +22,7 @@ const makeEntries = (
out[key] = {
slug: value.slug,
requireMention: value.requireMention,
reactionNotifications: value.reactionNotifications,
users: value.users,
channels: value.channels,
};
@@ -207,3 +209,87 @@ describe("discord reply target selection", () => {
).toBe("123");
});
});
describe("discord reaction notification gating", () => {
it("defaults to own when mode is unset", () => {
expect(
shouldEmitDiscordReactionNotification({
mode: undefined,
botId: "bot-1",
messageAuthorId: "bot-1",
userId: "user-1",
}),
).toBe(true);
expect(
shouldEmitDiscordReactionNotification({
mode: undefined,
botId: "bot-1",
messageAuthorId: "user-1",
userId: "user-2",
}),
).toBe(false);
});
it("skips when mode is off", () => {
expect(
shouldEmitDiscordReactionNotification({
mode: "off",
botId: "bot-1",
messageAuthorId: "bot-1",
userId: "user-1",
}),
).toBe(false);
});
it("allows all reactions when mode is all", () => {
expect(
shouldEmitDiscordReactionNotification({
mode: "all",
botId: "bot-1",
messageAuthorId: "user-1",
userId: "user-2",
}),
).toBe(true);
});
it("requires bot ownership when mode is own", () => {
expect(
shouldEmitDiscordReactionNotification({
mode: "own",
botId: "bot-1",
messageAuthorId: "bot-1",
userId: "user-2",
}),
).toBe(true);
expect(
shouldEmitDiscordReactionNotification({
mode: "own",
botId: "bot-1",
messageAuthorId: "user-2",
userId: "user-3",
}),
).toBe(false);
});
it("requires allowlist matches when mode is allowlist", () => {
expect(
shouldEmitDiscordReactionNotification({
mode: "allowlist",
botId: "bot-1",
messageAuthorId: "user-1",
userId: "user-2",
allowlist: [],
}),
).toBe(false);
expect(
shouldEmitDiscordReactionNotification({
mode: "allowlist",
botId: "bot-1",
messageAuthorId: "user-1",
userId: "123",
userName: "steipete",
allowlist: ["123", "other"],
}),
).toBe(true);
});
});

View File

@@ -559,28 +559,17 @@ export async function monitorDiscordProvider(opts: MonitorDiscordOpts = {}) {
const botId = client.user?.id;
if (botId && user.id === botId) return;
const reactionMode = guildInfo?.reactionNotifications ?? "allowlist";
if (reactionMode === "off") return;
if (reactionMode === "own") {
const authorId = message.author?.id;
if (!botId || authorId !== botId) return;
}
if (reactionMode === "allowlist") {
const userAllow = guildInfo?.users;
if (!Array.isArray(userAllow) || userAllow.length === 0) return;
const users = normalizeDiscordAllowList(userAllow, [
"discord:",
"user:",
]);
const userOk =
!!users &&
allowListMatches(users, {
id: user.id,
name: user.username,
tag: user.tag,
});
if (!userOk) return;
}
const reactionMode = guildInfo?.reactionNotifications ?? "own";
const shouldNotify = shouldEmitDiscordReactionNotification({
mode: reactionMode,
botId,
messageAuthorId: message.author?.id,
userId: user.id,
userName: user.username,
userTag: user.tag,
allowlist: guildInfo?.users,
});
if (!shouldNotify) return;
const emojiLabel = formatDiscordReactionEmoji(resolvedReaction);
const actorLabel = user.tag ?? user.username ?? user.id;
@@ -985,6 +974,43 @@ export function allowListMatches(
return false;
}
export function shouldEmitDiscordReactionNotification(params: {
mode: "off" | "own" | "all" | "allowlist" | undefined;
botId?: string | null;
messageAuthorId?: string | null;
userId: string;
userName?: string | null;
userTag?: string | null;
allowlist?: Array<string | number> | null;
}) {
const {
mode,
botId,
messageAuthorId,
userId,
userName,
userTag,
allowlist,
} = params;
const effectiveMode = mode ?? "own";
if (effectiveMode === "off") return false;
if (effectiveMode === "own") {
if (!botId || !messageAuthorId) return false;
return messageAuthorId === botId;
}
if (effectiveMode === "allowlist") {
if (!Array.isArray(allowlist) || allowlist.length === 0) return false;
const users = normalizeDiscordAllowList(allowlist, ["discord:", "user:"]);
if (!users) return false;
return allowListMatches(users, {
id: userId,
name: userName ?? undefined,
tag: userTag ?? undefined,
});
}
return true;
}
export function resolveDiscordGuildEntry(params: {
guild: Guild | null;
guildEntries: Record<string, DiscordGuildEntryResolved> | undefined;