Bladeren bron

Blacklist chats

TheHamkerCat 5 jaren geleden
bovenliggende
commit
794e242e43
3 gewijzigde bestanden met toevoegingen van 99 en 8 verwijderingen
  1. 60 0
      wbb/modules/blacklist_chat.py
  2. 9 7
      wbb/modules/filters.py
  3. 30 1
      wbb/utils/dbfunctions.py

+ 60 - 0
wbb/modules/blacklist_chat.py

@@ -0,0 +1,60 @@
+from pyrogram import filters
+from pyrogram.types import Message
+
+from wbb import SUDOERS, app
+from wbb.core.decorators.errors import capture_err
+from wbb.utils.dbfunctions import (blacklist_chat, blacklisted_chats,
+                                   whitelist_chat)
+
+__MODULE__ = "Blacklist Chat"
+__HELP__ = """
+**THIS MODULE IS ONLY FOR DEVS**
+
+Use this module to make the bot leave some chats
+in which you don't want it to be in.
+
+/blacklist_chat [CHAT_ID] - Blacklist a chat.
+/whitelist_chat [CHAT_ID] - Whitelist a chat.
+/blacklisted - Show blacklisted chats.
+"""
+
+
+@app.on_message(filters.command("blacklist_chat") & filters.user(SUDOERS))
+async def blacklist_chat_func(_, message: Message):
+    if len(message.command) != 2:
+        await message.reply_text("**Usage:**\n/blacklist_chat [CHAT_ID]")
+        return
+    chat_id = int(message.text.strip().split()[1])
+    if chat_id in await blacklisted_chats():
+        await message.reply_text("Chat is already blacklisted.")
+        return
+    blacklisted = await blacklist_chat(chat_id)
+    if blacklisted:
+        await message.reply_text("Chat has been successfully blacklisted")
+        return
+    await message.reply_text("Something wrong happened, check logs.")
+
+
+@app.on_message(filters.command("whitelist_chat") & filters.user(SUDOERS))
+async def whitelist_chat_func(_, message: Message):
+    if len(message.command) != 2:
+        await message.reply_text("**Usage:**\n/whitelist_chat [CHAT_ID]")
+        return
+    chat_id = int(message.text.strip().split()[1])
+    if chat_id not in await blacklisted_chats():
+        await message.reply_text("Chat is already whitelisted.")
+        return
+    whitelisted = await whitelist_chat(chat_id)
+    if whitelisted:
+        await message.reply_text("Chat has been successfully whitelisted")
+        return
+    await message.reply_text("Something wrong happened, check logs.")
+
+
+@app.on_message(filters.command("blacklisted_chats") & filters.user(SUDOERS))
+async def blacklisted_chats_func(_, message: Message):
+    text = ""
+    for count, chat_id in enumerate(await blacklisted_chats()):
+        title = (await app.get_chat(chat_id)).title
+        text += f"**{count}. {title}** [`chat_id`]\n"
+    await message.reply_text(text)

+ 9 - 7
wbb/modules/filters.py

@@ -29,7 +29,8 @@ from pyrogram import filters
 from wbb import SUDOERS, app
 from wbb.core.decorators.errors import capture_err
 from wbb.modules.admin import member_permissions
-from wbb.utils.dbfunctions import (add_served_chat, delete_filter, get_filter,
+from wbb.utils.dbfunctions import (add_served_chat, blacklisted_chats,
+                                   delete_filter, get_filter,
                                    get_filters_names, is_served_chat,
                                    save_filter)
 from wbb.utils.filter_groups import chat_filters_group
@@ -134,21 +135,18 @@ async def filters_re(_, message):
                 _filter = await get_filter(chat_id, word)
                 data_type = _filter["type"]
                 data = _filter["data"]
+                if text == word:
+                    await message.delete()
+                return
                 if data_type == "text":
                     if message.reply_to_message:
                         await message.reply_to_message.reply_text(
                             data, disable_web_page_preview=True
                         )
-                        if text == word:
-                            await message.delete()
-                        return
                     await message.reply_text(data, disable_web_page_preview=True)
                 else:
                     if message.reply_to_message:
                         await message.reply_to_message.reply_sticker(data)
-                        if text == word:
-                            await message.delete()
-                        return
                     await message.reply_sticker(data)
     except Exception as e:
         e = traceback.format_exc()
@@ -156,6 +154,10 @@ async def filters_re(_, message):
         pass
 
     """ CHAT WATCHER """
+    blacklisted_chats_list = await blacklisted_chats()
+    if chat_id in blacklisted_chats_list:
+        await app.leave_chat(chat_id)
+        return
     served_chat = await is_served_chat(chat_id)
     if served_chat:
         return

+ 30 - 1
wbb/utils/dbfunctions.py

@@ -39,9 +39,10 @@ pmpermitdb = db.pmpermit
 welcomedb = db.welcome_text
 nsfwdb = db.nsfw
 captcha_cachedb = db.captcha_cache
-blacklist_filtersdb = db.blaclist_filters
+blacklist_filtersdb = db.blacklist_filters
 pipesdb = db.pipes
 sudoersdb = db.sudoers
+blacklist_chatdb = db.blacklist_chat
 
 """ Notes functions """
 
@@ -639,3 +640,31 @@ async def remove_sudo(user_id: int) -> bool:
         {"sudo": "sudo"}, {"$set": {"sudoers": sudoers}}, upsert=True
     )
     return True
+
+
+""" BLACKLIST CHATS FUNCTIONS """
+
+
+async def blacklisted_chats() -> list:
+    chats = await blacklist_chatdb.find_one({"chat": "chat"})
+    if not chats:
+        return []
+    return chats["chats"]
+
+
+async def blacklist_chat(chat_id: int) -> bool:
+    chats = await blacklisted_chats()
+    chats.append(chats)
+    await blacklist_chatdb.update_one(
+        {"chat": "chat"}, {"$set": {"chats": chats}}, upsert=True
+    )
+    return True
+
+
+async def whitelist_chat(chat_id: int) -> bool:
+    chats = await blacklisted_chats()
+    chats.remove(chats)
+    await blacklist_chatdb.update_one(
+        {"chat": "chat"}, {"$set": {"chats": chats}}, upsert=True
+    )
+    return True