42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
import time
|
|
from collections import defaultdict
|
|
|
|
|
|
class ModerationManager:
|
|
def __init__(self, limit: int, window: int, ban_duration: int):
|
|
self.limit = limit
|
|
self.window = window
|
|
self.ban_duration = ban_duration
|
|
self.counters: dict[int, list[float]] = defaultdict(list)
|
|
self.bans: dict[int, float] = {}
|
|
|
|
def add_message(self, user_id: int) -> None:
|
|
now = time.time()
|
|
self.counters[user_id].append(now)
|
|
self.counters[user_id] = [
|
|
t for t in self.counters[user_id] if now - t <= self.window
|
|
]
|
|
|
|
def is_banned(self, user_id: int) -> bool:
|
|
if user_id in self.bans:
|
|
if time.time() - self.bans[user_id] < self.ban_duration:
|
|
return True
|
|
del self.bans[user_id]
|
|
return False
|
|
|
|
def check_and_ban(self, user_id: int) -> bool:
|
|
if len(self.counters.get(user_id, [])) >= self.limit:
|
|
self.bans[user_id] = time.time()
|
|
return True
|
|
return False
|
|
|
|
def should_delete(self, user_id: int) -> bool:
|
|
return self.is_banned(user_id)
|
|
|
|
|
|
moderation = ModerationManager(
|
|
limit=25,
|
|
window=60,
|
|
ban_duration=300,
|
|
)
|