Бот для капчи в чатах
db.py
@@ -66,7 +66,8 @@
    "type TEXT, "
    "added_at INTEGER, "
    "updated_at INTEGER, "
    "active INTEGER DEFAULT 1)",
    "active INTEGER DEFAULT 1, "
    "msgs INTEGER DEFAULT 0)",
    "CREATE TABLE IF NOT EXISTS usr_fails ("
    "user_id INTEGER, "
    "chat_id INTEGER, "
@@ -129,7 +130,9 @@
    "target_id INTEGER, "
    "text TEXT, "
    "created_at INTEGER, "
    "status TEXT DEFAULT 'new')",
    "status TEXT DEFAULT 'new', "
    "target_msg_id INTEGER DEFAULT 0, "
    "target_text TEXT DEFAULT '')",
    "CREATE TABLE IF NOT EXISTS report_block ("
    "user_id INTEGER, "
    "chat_id INTEGER, "
@@ -204,6 +207,13 @@
                await db.execute(f"ALTER TABLE chat_cfg ADD COLUMN {col} {ddl}")
    if await tbl_exists(db, "alarm") and not await col_exists(db, "alarm", "until"):
        await db.execute("ALTER TABLE alarm ADD COLUMN until INTEGER DEFAULT 0")
    if await tbl_exists(db, "reports"):
        if not await col_exists(db, "reports", "target_msg_id"):
            await db.execute("ALTER TABLE reports ADD COLUMN target_msg_id INTEGER DEFAULT 0")
        if not await col_exists(db, "reports", "target_text"):
            await db.execute("ALTER TABLE reports ADD COLUMN target_text TEXT DEFAULT ''")
    if await tbl_exists(db, "chats") and not await col_exists(db, "chats", "msgs"):
        await db.execute("ALTER TABLE chats ADD COLUMN msgs INTEGER DEFAULT 0")
async def init_db() -> None:
@@ -222,10 +232,11 @@
async def upsert_chat(chat_id: int, title: Optional[str], ctype: Optional[str], ts: int) -> None:
    async with db_conn() as db:
        await db.execute(
            "INSERT INTO chats (chat_id, title, type, added_at, updated_at, active) "
            "VALUES (?, ?, ?, ?, ?, 1) "
            "INSERT INTO chats (chat_id, title, type, added_at, updated_at, active, msgs) "
            "VALUES (?, ?, ?, ?, ?, 1, 1) "
            "ON CONFLICT(chat_id) DO UPDATE SET "
            "title=excluded.title, type=excluded.type, updated_at=excluded.updated_at, active=1",
            "title=excluded.title, type=excluded.type, updated_at=excluded.updated_at, "
            "active=1, msgs=msgs+1",
            (chat_id, title, ctype, ts, ts),
        )
        await db.commit()
@@ -651,17 +662,30 @@
# --- Репорты ---
async def add_report(chat_id: int, user_id: int, target_id: int, text: str, ts: int) -> int:
async def add_report(chat_id: int, user_id: int, target_id: int, text: str, ts: int, target_msg_id: int = 0, target_text: str = "") -> 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),
            "INSERT INTO reports (chat_id, user_id, target_id, text, created_at, status, target_msg_id, target_text) "
            "VALUES (?, ?, ?, ?, ?, 'new', ?, ?)",
            (chat_id, user_id, target_id, text, ts, target_msg_id, target_text),
        )
        await db.commit()
        return cur.lastrowid
async def get_report(report_id: int) -> Optional[dict]:
    async with db_conn() as db:
        async with db.execute("SELECT * FROM reports WHERE id=?", (report_id,)) as cur:
            row = await cur.fetchone()
    return dict(row) if row else None
async def clr_reports(chat_id: int) -> None:
    async with db_conn() as db:
        await db.execute("DELETE FROM reports WHERE chat_id=?", (chat_id,))
        await db.commit()
async def list_reports(chat_id: int, limit: int = 200) -> List[dict]:
    async with db_conn() as db:
        async with db.execute(