瀏覽代碼

Temporary fix for FloodWait on iter_cm

TheHamkerCat 4 年之前
父節點
當前提交
a9ffe6735a
共有 6 個文件被更改,包括 43 次插入 和 52 次删除
  1. 2 1
      .gitignore
  2. 5 4
      sample_config.py
  3. 0 18
      wbb/core/filters.py
  4. 28 20
      wbb/modules/admin.py
  5. 6 7
      wbb/modules/couple.py
  6. 2 2
      wbb/modules/img_pdf.py

+ 2 - 1
.gitignore

@@ -1,3 +1,4 @@
+mirror.py
 dump
 *.m4a
 *.apk
@@ -154,4 +155,4 @@ neofetch.txt
 error.log
 permissions.json
 .vscode
-.idea
+.idea

+ 5 - 4
sample_config.py

@@ -1,11 +1,12 @@
-from dotenv import load_dotenv
 from os import environ
 
+from dotenv import load_dotenv
+
 load_dotenv("config.env")
 
-HEROKU = (
-    bool(environ.get("DYNO"))  # NOTE Make it false if you're not deploying on heroku or docker.
-)
+HEROKU = bool(
+    environ.get("DYNO")
+)  # NOTE Make it false if you're not deploying on heroku or docker.
 
 if HEROKU:
 

+ 0 - 18
wbb/core/filters.py

@@ -42,23 +42,6 @@ def url(_, __, message: Message) -> bool:
     return bool(get_urls_from_text(text))
 
 
-async def admin(_, __, message: Message) -> bool:
-    if message.chat.type not in ["group", "supergroup"]:
-        return False
-    if not message.from_user:
-        if not message.sender_chat:
-            return False
-        return True
-    # Calling iter_chat_members again and again
-    # doesn't cause floodwait, that's why i'm using it here.
-    return message.from_user.id in [
-        member.user.id
-        async for member in message._client.iter_chat_members(
-            message.chat.id, filter="administrators"
-        )
-    ]
-
-
 def entities(_, __, message: Message) -> bool:
     return bool(message.entities)
 
@@ -85,7 +68,6 @@ class Filters:
 
 filters = Filters
 filters.url = filters_.create(url)
-filters.admin = filters_.create(admin)
 filters.entities = filters_.create(entities)
 filters.anonymous = filters_.create(anonymous)
 filters.sudoers = filters_.create(sudoers)

+ 28 - 20
wbb/modules/admin.py

@@ -22,6 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 """
 import asyncio
+from time import time
 
 from pyrogram import filters
 from pyrogram.types import CallbackQuery, ChatPermissions, Message
@@ -87,14 +88,26 @@ async def member_permissions(chat_id: int, user_id: int):
 
 from wbb.core.decorators.permissions import adminsOnly
 
+admins_in_chat = {}
+
 
 async def list_admins(chat_id: int):
-    return [
-        member.user.id
-        async for member in app.iter_chat_members(
-            chat_id, filter="administrators"
-        )
-    ]
+    global admins_in_chat
+    if chat_id in admins_in_chat:
+        interval = time() - admins_in_chat[chat_id]["last_updated_at"]
+        if interval < 3600:
+            return admins_in_chat[chat_id]["data"]
+
+    admins_in_chat[chat_id] = {
+        "last_updated_at": time(),
+        "data": [
+            member.user.id
+            async for member in app.iter_chat_members(
+                chat_id, filter="administrators"
+            )
+        ],
+    }
+    return admins_in_chat[chat_id]["data"]
 
 
 async def current_chat_permissions(chat_id):
@@ -120,13 +133,6 @@ async def current_chat_permissions(chat_id):
     return perms
 
 
-# Get List Of Members In A Chat
-
-
-async def list_members(group_id):
-    return [member.user.id async for member in app.iter_chat_members(group_id)]
-
-
 # Purge Messages
 
 
@@ -381,7 +387,9 @@ async def demote(_, message: Message):
 # Pin Messages
 
 
-@app.on_message(filters.command(["pin", "unpin"]) & ~filters.edited & ~filters.private)
+@app.on_message(
+    filters.command(["pin", "unpin"]) & ~filters.edited & ~filters.private
+)
 @adminsOnly("can_pin_messages")
 async def pin(_, message: Message):
     if not message.reply_to_message:
@@ -390,8 +398,8 @@ async def pin(_, message: Message):
     if message.command[0][0] == "u":
         await r.unpin()
         return await message.reply_text(
-                       f"**Unpinned [this]({r.link}) message.**",
-                       disable_web_page_preview=True,
+            f"**Unpinned [this]({r.link}) message.**",
+            disable_web_page_preview=True,
         )
     await r.pin(disable_notification=True)
     await message.reply(
@@ -480,6 +488,8 @@ async def ban_deleted_accounts(_, message: Message):
     chat_id = message.chat.id
     deleted_users = []
     banned_users = 0
+    m = await message.reply("Finding ghosts...")
+
     async for i in app.iter_chat_members(chat_id):
         if i.user.is_deleted:
             deleted_users.append(i.user.id)
@@ -490,9 +500,9 @@ async def ban_deleted_accounts(_, message: Message):
             except Exception:
                 pass
             banned_users += 1
-        await message.reply_text(f"Banned {banned_users} Deleted Accounts")
+        await m.edit(f"Banned {banned_users} Deleted Accounts")
     else:
-        await message.reply_text("There are no deleted accounts in this chat")
+        await m.edit("There are no deleted accounts in this chat")
 
 
 @app.on_message(
@@ -516,8 +526,6 @@ async def warn_user(_, message: Message):
         return await message.reply_text(
             "I can't warn an admin, You know the rules, so do i."
         )
-    if user_id not in (await list_members(chat_id)):
-        return await message.reply_text("This user isn't here.")
     user, warns = await asyncio.gather(
         app.get_users(user_id),
         get_warn(chat_id, await int_to_alpha(user_id)),

+ 6 - 7
wbb/modules/couple.py

@@ -62,6 +62,9 @@ tomorrow = str(dt_tom())
 async def couple(_, message):
     if message.chat.type == "private":
         return await message.reply_text("This command only works in groups.")
+
+    m = await message.reply("Detecting gay among us...")
+
     try:
         chat_id = message.chat.id
         is_selected = await get_couple(chat_id, today)
@@ -71,7 +74,7 @@ async def couple(_, message):
                 if not i.user.is_bot:
                     list_of_users.append(i.user.id)
             if len(list_of_users) < 2:
-                return await message.reply_text("Not enough users")
+                return await m.edit("Not enough users")
             c1_id = random.choice(list_of_users)
             c2_id = random.choice(list_of_users)
             while c1_id == c2_id:
@@ -83,9 +86,7 @@ async def couple(_, message):
 {c1_mention} + {c2_mention} = ❤️
 
 __New couple of the day may be chosen at 12AM {tomorrow}__"""
-            await app.send_message(
-                message.chat.id, text=couple_selection_message
-            )
+            await m.edit(couple_selection_message)
             couple = {"c1_id": c1_id, "c2_id": c2_id}
             await save_couple(chat_id, today, couple)
 
@@ -98,9 +99,7 @@ __New couple of the day may be chosen at 12AM {tomorrow}__"""
 [{c1_name}](tg://openmessage?user_id={c1_id}) + [{c2_name}](tg://openmessage?user_id={c2_id}) = ❤️
 
 __New couple of the day may be chosen at 12AM {tomorrow}__"""
-            await app.send_message(
-                message.chat.id, text=couple_selection_message
-            )
+            await m.edit(couple_selection_message)
     except Exception as e:
         print(e)
         await message.reply_text(e)

+ 2 - 2
wbb/modules/img_pdf.py

@@ -65,8 +65,8 @@ async def convert(
 
     if len(main_message.command) >= 2:
         names = main_message.text.split(None, 1)[1]
-        if not names.endswith('.pdf'):
-            pdf.name = names + '.pdf'
+        if not names.endswith(".pdf"):
+            pdf.name = names + ".pdf"
         else:
             pdf.name = names