浏览代码

Added global stats

TheHamkerCat 5 年之前
父节点
当前提交
1cf3e400c7
共有 4 个文件被更改,包括 36 次插入 和 19 次删除
  1. 1 2
      wbb/__init__.py
  2. 11 14
      wbb/modules/global_stats.py
  3. 23 3
      wbb/utils/dbfunctions.py
  4. 1 0
      wbb/utils/filter_groups.py

+ 1 - 2
wbb/__init__.py

@@ -1,8 +1,7 @@
 from config import (
     BOT_TOKEN, API_ID ,API_HASH, OWNER_ID, SUDO_USER_ID,
     LOG_GROUP_ID, FERNET_ENCRYPTION_KEY, MONGO_DB_URI,
-    WELCOME_DELAY_KICK_SEC, ARQ_API_BASE_URL as ARQ_API,
-    SUDO_CHAT_ID
+    WELCOME_DELAY_KICK_SEC, ARQ_API_BASE_URL as ARQ_API
     )
 from pyrogram import Client
 from motor.motor_asyncio import AsyncIOMotorClient as MongoClient

+ 11 - 14
wbb/modules/global_stats.py

@@ -7,30 +7,29 @@ from wbb.utils.dbfunctions import (
         get_served_chats,
         add_served_chat,
         remove_served_chat,
-        alpha_to_int
+        alpha_to_int_chat
         )
 from pyrogram.errors.exceptions.bad_request_400 import UserNotParticipant
+from pyrogram import filters
 
-
-@app.on_message(filters.new_chat_members & filters.left_chat_member)
+@app.on_message(filters.new_chat_members & filters.left_chat_member, group=global_stats_group)
 @capture_err
 async def chat_watcher(_, message):
     chat_id = message.chat.id
     served_chat = await is_served_chat(chat_id)
-    if served_chat:
-        return
+    if served_chat: return
     await add_served_chat(chat_id)
 
 
-@app.on_message(filters.command("/global_stats") & ~filters.edited)
+@app.on_message(filters.command("global_stats") & ~filters.edited)
 @capture_err
 async def global_stats(_, message):
-    m = await message.reply_text("__**Analysing Stats, Might Take 10-30 Seconds.**__")
+    m = await app.send_message(message.chat.id, text="__**Analysing Stats, Might Take 10-30 Seconds.**__")
     served_chats = []
     total_users = 0
-    chats = await get_served_chats 
+    chats = await get_served_chats()
     for chat in chats:
-        served_chats.append(await alpha_to_int(chat))
+        served_chats.append(await alpha_to_int_chat(chat))
     for served_chat in served_chats:
         try:
             await app.get_chat_member(served_chat, BOT_ID)
@@ -40,16 +39,14 @@ async def global_stats(_, message):
             pass
     for i in served_chats:
         total_users += int((await app.get_chat(i)).members_count)
+    # NOTE need to fix these
     msg = f"""**Global Stats of {BOT_NAME}**:
-0 gbanned users.
+0 gbanned users.                           
 0 blacklist triggers, across 0 chats.
 0 filters, across 0 chats.
 0 notes, across 0 chats.
 0 welcome messages are set.
 {total_users} users, across {len(served_chats)} chats.
-0 warns, across 0 chats.
-0 """
+0 warns, across 0 chats."""
     await m.edit(msg)
 
-
-

+ 23 - 3
wbb/utils/dbfunctions.py

@@ -144,6 +144,14 @@ async def int_to_alpha(user_id: int) -> str:
         text += alphabet[int(i)]
     return text
 
+async def int_to_alpha_chat(chat_id: int) -> str:
+    alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
+    text = ""
+    chat_id = str(chat_id)
+    chat_id = chat_id.replace("-", "")
+    for i in chat_id:
+        text += alphabet[int(i)]
+    return text
 
 async def alpha_to_int(user_id_alphabet: str) -> int:
     alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
@@ -155,6 +163,16 @@ async def alpha_to_int(user_id_alphabet: str) -> int:
     return user_id
 
 
+async def alpha_to_int_chat(chat_id_alphabet: str) -> int:
+    alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]
+    chat_id = "-"
+    for i in chat_id_alphabet:
+        index = alphabet.index(i)
+        chat_id += str(index)
+    chat_id = int(chat_id)
+    return chat_id
+
+
 async def get_warns(chat_id: int) -> Dict[str, int]:
     warns = await warnsdb.find_one({"chat_id": chat_id})
     if warns:
@@ -243,13 +261,15 @@ async def update_karma(chat_id: int, name: str, karma: dict):
 
 async def is_served_chat(chat_id: int) -> bool:
     chats = await chatsdb.find_one({"chats": "chats"})
+    if not chats: return False
     chats = chats['chats']
     for chat in chats:
-        if chat == await int_to_alpha(chat_id): return True
+        if chat == await int_to_alpha_chat(chat_id): return True
 
 
 async def get_served_chats():
     chats = await chatsdb.find_one({"chats": "chats"})
+    if not chats: return {}
     chats = chats['chats']
     return chats
 
@@ -258,7 +278,7 @@ async def add_served_chat(chat_id: int):
     is_served = await is_served_chat(chat_id)
     if is_served: return
     chats = await get_served_chats()
-    chats[await int_to_alpha(chat_id)] = {"chat_id": chat_id}
+    chats[await int_to_alpha_chat(chat_id)] = {"chat_id": chat_id}
     await chatsdb.update_one(
             {"chats": "chats"},
             {
@@ -273,7 +293,7 @@ async def remove_served_chat(chat_id: int):
     is_served = await is_served_chat(chat_id)
     if not is_served: return
     chats = await get_served_chats()
-    del chats[await int_to_alpha(chat_id)]
+    del chats[await int_to_alpha_chat(chat_id)]
     await chatsdb.update_one(
             {"chats": "chats"},
             {

+ 1 - 0
wbb/utils/filter_groups.py

@@ -3,3 +3,4 @@ chatbot_group = 2
 karma_positive_group = 3
 karma_negative_group = 4
 regex_group = 5
+global_stats_group = 6