Бот для капчи в чатах
db.py
@@ -92,6 +92,31 @@
    "username TEXT, "
    "photo_url TEXT, "
    "last_login INTEGER)",
    "CREATE TABLE IF NOT EXISTS users ("
    "user_id INTEGER PRIMARY KEY, "
    "username TEXT, "
    "first_name TEXT, "
    "last_name TEXT, "
    "updated_at INTEGER)",
    "CREATE TABLE IF NOT EXISTS reports ("
    "id INTEGER PRIMARY KEY AUTOINCREMENT, "
    "chat_id INTEGER, "
    "user_id INTEGER, "
    "target_id INTEGER, "
    "text TEXT, "
    "created_at INTEGER, "
    "status TEXT DEFAULT 'new')",
    "CREATE TABLE IF NOT EXISTS report_block ("
    "user_id INTEGER, "
    "chat_id INTEGER, "
    "until INTEGER, "
    "PRIMARY KEY (user_id, chat_id))",
    "CREATE TABLE IF NOT EXISTS alarm ("
    "chat_id INTEGER PRIMARY KEY, "
    "enabled INTEGER DEFAULT 0, "
    "enabled_by INTEGER, "
    "enabled_at INTEGER, "
    "raid_until INTEGER)",
)
@@ -537,3 +562,129 @@
        async with db.execute("SELECT * FROM web_users WHERE user_id=?", (user_id,)) as cur:
            row = await cur.fetchone()
    return dict(row) if row else None
# --- Кеш пользователей (для поиска по @username) ---
async def upsert_user(user_id: int, username: Optional[str], first_name: Optional[str], last_name: Optional[str], ts: int) -> None:
    async with db_conn() as db:
        await db.execute(
            "INSERT INTO users (user_id, username, first_name, last_name, updated_at) "
            "VALUES (?, ?, ?, ?, ?) "
            "ON CONFLICT(user_id) DO UPDATE SET "
            "username=excluded.username, first_name=excluded.first_name, "
            "last_name=excluded.last_name, updated_at=excluded.updated_at",
            (user_id, username, first_name, last_name, ts),
        )
        await db.commit()
async def find_user_by_username(username: str) -> Optional[dict]:
    async with db_conn() as db:
        async with db.execute(
            "SELECT * FROM users WHERE lower(username)=lower(?) ORDER BY updated_at DESC LIMIT 1",
            (username.lstrip("@"),),
        ) as cur:
            row = await cur.fetchone()
    return dict(row) if row else None
async def get_user(user_id: int) -> Optional[dict]:
    async with db_conn() as db:
        async with db.execute("SELECT * FROM users WHERE user_id=?", (user_id,)) as cur:
            row = await cur.fetchone()
    return dict(row) if row else None
# --- Репорты ---
async def add_report(chat_id: int, user_id: int, target_id: int, text: str, ts: int) -> int:
    async with db_conn() as db:
        cur = await db.execute(
            "INSERT INTO reports (chat_id, user_id, target_id, text, created_at, status) "
            "VALUES (?, ?, ?, ?, ?, 'new')",
            (chat_id, user_id, target_id, text, ts),
        )
        await db.commit()
        return cur.lastrowid
async def list_reports(chat_id: int, limit: int = 200) -> List[dict]:
    async with db_conn() as db:
        async with db.execute(
            "SELECT * FROM reports WHERE chat_id=? ORDER BY created_at DESC LIMIT ?",
            (chat_id, limit),
        ) as cur:
            return [dict(r) for r in await cur.fetchall()]
async def cnt_reports_since(chat_id: int, user_id: int, since: int) -> int:
    async with db_conn() as db:
        async with db.execute(
            "SELECT COUNT(*) FROM reports WHERE chat_id=? AND user_id=? AND created_at >= ?",
            (chat_id, user_id, since),
        ) as cur:
            return (await cur.fetchone())[0]
async def close_report(report_id: int) -> None:
    async with db_conn() as db:
        await db.execute("UPDATE reports SET status='closed' WHERE id=?", (report_id,))
        await db.commit()
async def report_block_until(chat_id: int, user_id: int) -> int:
    async with db_conn() as db:
        async with db.execute(
            "SELECT until FROM report_block WHERE chat_id=? AND user_id=?", (chat_id, user_id)
        ) as cur:
            row = await cur.fetchone()
    return row[0] if row else 0
async def set_report_block(chat_id: int, user_id: int, until: int) -> None:
    async with db_conn() as db:
        await db.execute(
            "INSERT INTO report_block (user_id, chat_id, until) VALUES (?, ?, ?) "
            "ON CONFLICT(user_id, chat_id) DO UPDATE SET until=excluded.until",
            (user_id, chat_id, until),
        )
        await db.commit()
# --- Режим тревоги ---
async def get_alarm(chat_id: int) -> dict:
    async with db_conn() as db:
        async with db.execute("SELECT * FROM alarm WHERE chat_id=?", (chat_id,)) as cur:
            row = await cur.fetchone()
    if row:
        return dict(row)
    return {"chat_id": chat_id, "enabled": 0, "enabled_by": 0, "enabled_at": 0, "raid_until": 0}
async def set_alarm(chat_id: int, enabled: bool, by: int, ts: int) -> None:
    async with db_conn() as db:
        await db.execute(
            "INSERT INTO alarm (chat_id, enabled, enabled_by, enabled_at, raid_until) "
            "VALUES (?, ?, ?, ?, 0) "
            "ON CONFLICT(chat_id) DO UPDATE SET "
            "enabled=excluded.enabled, enabled_by=excluded.enabled_by, enabled_at=excluded.enabled_at",
            (chat_id, 1 if enabled else 0, by, ts),
        )
        # При выключении сбрасываем принудительный кик.
        if not enabled:
            await db.execute("UPDATE alarm SET raid_until=0 WHERE chat_id=?", (chat_id,))
        await db.commit()
async def set_raid_until(chat_id: int, until: int) -> None:
    async with db_conn() as db:
        await db.execute("UPDATE alarm SET raid_until=? WHERE chat_id=?", (until, chat_id))
        await db.commit()
async def list_alarm_chats() -> List[dict]:
    async with db_conn() as db:
        async with db.execute("SELECT * FROM alarm WHERE enabled=1") as cur:
            return [dict(r) for r in await cur.fetchall()]