From c2ba0366043c044a331c0956560a81d4869a03ca Mon Sep 17 00:00:00 2001
From: Roman Sergienko <romasergienko@outlook.com>
Date: Sat, 10 Oct 2026 14:58:04 +0000
Subject: [PATCH] fix: владелец бота в /help и /version; AJAX-переключение прав админов в панели

---
 db.py |   30 ++++++++++++++++++++++++++++++
 1 files changed, 30 insertions(+), 0 deletions(-)

diff --git a/db.py b/db.py
index d5f649e..37b7192 100644
--- a/db.py
+++ b/db.py
@@ -405,6 +405,36 @@
         }
 
 
+async def stats_scoped(ids, ts: int) -> dict:
+    # Статистика только по доступным пользователю чатам (для дашборда).
+    result = {"chats": len(ids), "warns": 0, "mutes": 0, "bans": 0, "logs": 0}
+    ids = list(ids)
+    if not ids:
+        return result
+    ph = ",".join("?" for _ in ids)
+    async with db_conn() as db:
+        async def one(q: str, args: tuple):
+            async with db.execute(q, args) as cur:
+                return (await cur.fetchone())[0]
+        result["warns"] = await one(
+            f"SELECT COUNT(*) FROM warns WHERE chat_id IN ({ph}) "
+            f"AND (expires_at IS NULL OR expires_at > ?)",
+            tuple(ids) + (ts,),
+        )
+        result["mutes"] = await one(
+            f"SELECT COUNT(*) FROM punish WHERE chat_id IN ({ph}) AND active=1 AND kind='mute'",
+            tuple(ids),
+        )
+        result["bans"] = await one(
+            f"SELECT COUNT(*) FROM punish WHERE chat_id IN ({ph}) AND active=1 AND kind='ban'",
+            tuple(ids),
+        )
+        result["logs"] = await one(
+            f"SELECT COUNT(*) FROM mod_log WHERE chat_id IN ({ph})", tuple(ids)
+        )
+    return result
+
+
 async def add_log(chat_id: int, admin_id: int, target_id: int, action: str, reason: str, ts: int) -> None:
     async with db_conn() as db:
         await db.execute(

--
Gitblit v1.9.3