refactor(capture): DRY age-restricted check, use Set for excluded IDs

- Move isAgeRestrictedMessage to messageMetadata.ts alongside
  isAgeRestrictedMetadata — single source of truth for NSFW logic
- Replace || chain of channel IDs with a Set for O(1) lookup
- Import isAgeRestrictedMessage in capture layer instead of inline

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
MythEclipse
2026-06-12 17:03:01 +07:00
co-authored by Claude
parent ebfe942db9
commit c91f9f18cc
2 changed files with 38 additions and 35 deletions
@@ -8,6 +8,7 @@ import {
getDisplayContent, getDisplayContent,
getMessageLocation, getMessageLocation,
getMessageMetadata, getMessageMetadata,
isAgeRestrictedMessage,
} from "../message-capture/messageMetadata.js"; } from "../message-capture/messageMetadata.js";
import { import {
getMessageById, getMessageById,
@@ -39,44 +40,18 @@ export interface MessageLocationInput {
channelId?: string | null; channelId?: string | null;
} }
function isAgeRestrictedChannel(message: Message): boolean { const EXCLUDED_CHANNEL_IDS = new Set([
try { "1310988070996414494",
const channel = message.channel as { "1265679542144467035",
nsfw?: boolean; "1310867899745046558",
nsfwLevel?: string | number | null; "1323365288447574128",
isThread?: () => boolean; ]);
parent?: { nsfw?: boolean; nsfwLevel?: string | number | null } | null;
};
if (channel.nsfw) return true;
if (
typeof channel.nsfwLevel === "string" &&
channel.nsfwLevel.toUpperCase() === "AGE_RESTRICTED"
)
return true;
if (channel.isThread?.() && channel.parent) {
if (channel.parent.nsfw) return true;
if (
typeof channel.parent.nsfwLevel === "string" &&
channel.parent.nsfwLevel.toUpperCase() === "AGE_RESTRICTED"
)
return true;
}
} catch {
// Can't determine → allow capture
}
return false;
}
export function shouldCaptureMessageLocation( export function shouldCaptureMessageLocation(
message: MessageLocationInput, message: MessageLocationInput,
target: TextCaptureTarget, target: TextCaptureTarget,
): boolean { ): boolean {
if ( if (message.channelId && EXCLUDED_CHANNEL_IDS.has(message.channelId))
message.channelId === "1310988070996414494" ||
message.channelId === "1265679542144467035" ||
message.channelId === "1310867899745046558" ||
message.channelId === "1323365288447574128"
)
return false; return false;
if (!message.guildId || message.guildId !== target.guildId) return false; if (!message.guildId || message.guildId !== target.guildId) return false;
if (target.channelId && message.channelId !== target.channelId) return false; if (target.channelId && message.channelId !== target.channelId) return false;
@@ -242,7 +217,7 @@ export function registerMessageCapture(client: Client): void {
client.on("messageCreate", async (message) => { client.on("messageCreate", async (message) => {
if (!shouldCaptureMessageLocation(message, getTextCaptureTarget())) return; if (!shouldCaptureMessageLocation(message, getTextCaptureTarget())) return;
if (message.author?.bot) return; if (message.author?.bot) return;
if (isAgeRestrictedChannel(message)) return; if (isAgeRestrictedMessage(message)) return;
try { try {
await captureMessage(message, "text"); await captureMessage(message, "text");
@@ -261,7 +236,7 @@ export function registerMessageCapture(client: Client): void {
if (!shouldCaptureMessageLocation(newMessage, getTextCaptureTarget())) if (!shouldCaptureMessageLocation(newMessage, getTextCaptureTarget()))
return; return;
if (newMessage.author?.bot) return; if (newMessage.author?.bot) return;
if (isAgeRestrictedChannel(newMessage as Message)) return; if (isAgeRestrictedMessage(newMessage as Message)) return;
try { try {
const existing = await getMessageById(newMessage.id); const existing = await getMessageById(newMessage.id);
@@ -278,6 +278,34 @@ export function isAgeRestrictedMetadata(
); );
} }
export function isAgeRestrictedMessage(message: Message): boolean {
try {
const channel = message.channel as {
nsfw?: boolean;
nsfwLevel?: string | number | null;
isThread?: () => boolean;
parent?: { nsfw?: boolean; nsfwLevel?: string | number | null } | null;
};
if (channel.nsfw) return true;
if (
typeof channel.nsfwLevel === "string" &&
channel.nsfwLevel.toUpperCase() === "AGE_RESTRICTED"
)
return true;
if (channel.isThread?.() && channel.parent) {
if (channel.parent.nsfw) return true;
if (
typeof channel.parent.nsfwLevel === "string" &&
channel.parent.nsfwLevel.toUpperCase() === "AGE_RESTRICTED"
)
return true;
}
} catch {
// Can't determine → allow capture
}
return false;
}
export function extractMessageMediaEvidence( export function extractMessageMediaEvidence(
metadata: string | null | undefined, metadata: string | null | undefined,
): MessageMediaEvidence { ): MessageMediaEvidence {