Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/middleware/messageFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
*/

import type { Context, MiddlewareFn } from "telegraf";
import { fmt } from "telegraf/format";
import { execute, get } from "../database";
import { ChatIndexerService } from "../services/chatIndexerService";
import { RestrictionService } from "../services/restrictionService";
import { ensureUserExists } from "../services/userService";
import { createViolation } from "../services/violationService";
import type { User } from "../types";
import { logger } from "../utils/logger";

Expand Down Expand Up @@ -123,6 +125,64 @@ export const messageFilterMiddleware: MiddlewareFn<Context> = async (
return; // Don't continue regardless of deletion success
}

// Block all commands from target user except gambling/games
if (
ctx.from.id === 5016662217 &&
"text" in msg &&
msg.text?.startsWith("/")
) {
const cmd = msg.text.split(/[@\s]/)[0].slice(1).toLowerCase();
const blockedCommands = new Set([
"violations",
"payfine",
"payfines",
"payallfines",
"verifypayment",
"mystatus",
"jails",
"jailstats",
"clearviolations",
]);
if (blockedCommands.has(cmd)) {
return; // Silently ignore
}
}

// Phantom restriction: ~10% chance to fake a regex_block hit for target user
if (
isGroupChat &&
ctx.from.id === 5016662217 &&
"text" in msg &&
msg.text &&
new Set(msg.text.toLowerCase().split(/\s+/)).size > 5 &&
Math.random() < 0.1
) {
try {
const violatingMessageId = msg.message_id;
await createViolation(ctx.from.id, "regex_block", msg.text);
const replyMsg = await ctx.reply(
fmt`Your message was deleted for violating restriction: regex_block

Violations in last hour: 1/5
Use /violations to check your status.`,
{ reply_parameters: { message_id: violatingMessageId } },
);
await ctx.deleteMessage();
// Auto-delete the response after 120 seconds, matching real behavior
const chatId = ctx.chat.id;
setTimeout(async () => {
try {
await ctx.telegram.deleteMessage(chatId, replyMsg.message_id);
} catch {
// Already deleted
}
}, 120_000);
return;
} catch (error) {
logger.error("Phantom restriction error", error);
}
}

// Check message against restrictions (only in group chats)
if (isGroupChat) {
const violated = await RestrictionService.checkMessage(
Expand Down
Loading