TheHamkerCat před 5 roky
rodič
revize
3d59b7d3dc

+ 126 - 0
wbb/modules/blacklist.py

@@ -0,0 +1,126 @@
+"""
+MIT License
+
+Copyright (c) 2021 TheHamkerCat
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
+"""
+from wbb import app, SUDOERS
+from wbb.modules.admin import member_permissions, list_admins
+from wbb.utils.dbfunctions import (
+    save_blacklist_filter, get_blacklisted_words, delete_blacklist_filter
+)
+from pyrogram import filters
+from pyrogram.types import ChatPermissions
+from wbb.core.decorators.errors import capture_err
+from wbb.utils.filter_groups import blacklist_filters_group
+import re
+import traceback
+
+__MODULE__ = "Blacklist"
+__HELP__ = """
+/blacklisted - Get All The Blacklisted Words In The Chat.
+/blacklist [WORD|SENTENCE] - Blacklist A Word Or A Sentence.
+/whitelist [WORD|SENTENCE] - Whitelist A Word Or A Sentence.
+"""
+
+
+@app.on_message(filters.command("blacklist") & ~filters.edited & ~filters.private)
+@capture_err
+async def save_filters(_, message):
+    if len(message.command) < 2:
+        await message.reply_text("Usage:\n/blacklist [WORD|SENTENCE]")
+        return
+    chat_id = message.chat.id
+    user_id = message.from_user.id
+    permissions = await member_permissions(chat_id, user_id)
+    if "can_restrict_members" not in permissions and user_id not in SUDOERS:
+        await message.reply_text("**You don't have enough permissions**")
+        return
+    word = message.text.split(None, 1)[1].strip()
+    if not word:
+        await message.reply_text("**Usage**\n__/blacklist [WORD|SENTENCE]__")
+        return
+    await save_blacklist_filter(chat_id, word)
+    await message.reply_text(f"__**Blacklisted {word}.**__")
+
+
+@app.on_message(filters.command("blacklisted") & ~filters.edited & ~filters.private)
+@capture_err
+async def get_filterss(_, message):
+    data = await get_blacklisted_words(message.chat.id)
+    if not data:
+        await message.reply_text("**No blacklisted words in this chat.**")
+    else:
+        msg = f"List of blacklisted words in {message.chat.title}\n"
+        for word in data:
+            msg += f"**-** `{word}`\n"
+        await message.reply_text(msg)
+
+
+@app.on_message(filters.command("whitelist") & ~filters.edited & ~filters.private)
+@capture_err
+async def del_filter(_, message):
+    if len(message.command) < 2:
+        await message.reply_text("Usage:\n/whitelist [WORD|SENTENCE]")
+        return
+    chat_id = message.chat.id
+    user_id = message.from_user.id
+    permissions = await member_permissions(chat_id, user_id)
+    if "can_restrict_members" not in permissions and user_id not in SUDOERS:
+        await message.reply_text("**You don't have enough permissions**")
+        return
+    word = message.text.split(None, 1)[1].strip()
+    if not word:
+        await message.reply_text("Usage:\n/whitelist [WORD|SENTENCE]")
+        return
+    deleted = await delete_blacklist_filter(chat_id, word)
+    if deleted:
+        await message.reply_text(f"**Whitelist {word}.**")
+    else:
+        await message.reply_text("**No such blacklist filter.**")
+
+
+@app.on_message(filters.text & ~filters.private, group=blacklist_filters_group)
+async def blacklist_filters_re(_, message):
+    text = message.text.lower().strip()
+    if not text:
+        return
+    chat_id = message.chat.id
+    user = message.from_user
+    if user.id in SUDOERS:
+        return
+    try:
+        list_of_filters = await get_blacklisted_words(chat_id)
+        for word in list_of_filters:
+            pattern = r"( |^|[^\w])" + re.escape(word) + r"( |$|[^\w])"
+            if re.search(pattern, text, flags=re.IGNORECASE):
+                if user.id in await list_admins(chat_id):
+                    return
+                try:
+                    await message.chat.restrict_member(user.id, ChatPermissions())
+                except Exception:
+                    return
+                await app.send_message(chat_id, f"Muted {user.mention} [`{user.id}`] due to a blacklist "
+                                       + f"match on {word}.")
+                return
+    except Exception as e:
+        e = traceback.format_exc()
+        print(e)
+        pass

+ 36 - 30
wbb/modules/filters.py

@@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 """
-from wbb import app
+from wbb import app, SUDOERS
 from wbb.modules.admin import member_permissions
 from wbb.utils.dbfunctions import (
     save_filter, get_filters_names, get_filter,
@@ -31,6 +31,7 @@ from pyrogram import filters
 from wbb.core.decorators.errors import capture_err
 from wbb.utils.filter_groups import chat_filters_group
 import re
+import traceback
 
 
 __MODULE__ = "Filters"
@@ -45,26 +46,29 @@ You can use markdown or html to save text too."""
 @app.on_message(filters.command("filter") & ~filters.edited & ~filters.private)
 @capture_err
 async def save_filters(_, message):
+    chat_id = message.chat.id
+    user_id = message.from_user.id
     if len(message.command) < 2 or not message.reply_to_message:
         await message.reply_text("Usage:\nReply to a text or sticker with /filter [FILTER_NAME] to save it.")
-
-    elif not message.reply_to_message.text and not message.reply_to_message.sticker:
+        return
+    if not message.reply_to_message.text and not message.reply_to_message.sticker:
         await message.reply_text("__**You can only save text or stickers in filters.**__")
-
-    elif len(await member_permissions(message.chat.id, message.from_user.id)) < 1:
+        return
+    permissions = await member_permissions(chat_id, user_id)
+    if "can_restrict_members" not in permissions and user_id not in SUDOERS:
         await message.reply_text("**You don't have enough permissions**")
-    else:
-        name = message.text.split(None, 1)[1].strip()
-        if not name:
-            await message.reply_text("**Usage**\n__/filter [FILTER_NAME]__")
-            return
-        _type = "text" if message.reply_to_message.text else "sticker"
-        _filter = {
-            "type": _type,
-            "data": message.reply_to_message.text.markdown if _type == "text" else message.reply_to_message.sticker.file_id
-        }
-        await save_filter(message.chat.id, name, _filter)
-        await message.reply_text(f"__**Saved filter {name}.**__")
+        return
+    name = message.text.split(None, 1)[1].strip()
+    if not name:
+        await message.reply_text("**Usage**\n__/filter [FILTER_NAME]__")
+        return
+    _type = "text" if message.reply_to_message.text else "sticker"
+    _filter = {
+        "type": _type,
+        "data": message.reply_to_message.text.markdown if _type == "text" else message.reply_to_message.sticker.file_id
+    }
+    await save_filter(chat_id, name, _filter)
+    await message.reply_text(f"__**Saved filter {name}.**__")
 
 
 @app.on_message(filters.command("filters") & ~filters.edited & ~filters.private)
@@ -83,23 +87,24 @@ async def get_filterss(_, message):
 @app.on_message(filters.command("stop") & ~filters.edited & ~filters.private)
 @capture_err
 async def del_filter(_, message):
+    chat_id = message.chat.id
+    user_id = message.from_user.id
     if len(message.command) < 2:
         await message.reply_text("**Usage**\n__/stop [FILTER_NAME]__")
-
-    elif len(await member_permissions(message.chat.id, message.from_user.id)) < 1:
+        return
+    permissions = await member_permissions(chat_id, user_id)
+    if "can_restrict_members" not in permissions and user_id not in SUDOERS:
         await message.reply_text("**You don't have enough permissions**")
-
+        return
+    name = message.text.split(None, 1)[1].strip()
+    if not name:
+        await message.reply_text("**Usage**\n__/stop [FILTER_NAME]__")
+        return
+    deleted = await delete_filter(chat_id, name)
+    if deleted:
+        await message.reply_text(f"**Deleted filter {name}.**")
     else:
-        name = message.text.split(None, 1)[1].strip()
-        if not name:
-            await message.reply_text("**Usage**\n__/stop [FILTER_NAME]__")
-            return
-        chat_id = message.chat.id
-        deleted = await delete_filter(chat_id, name)
-        if deleted:
-            await message.reply_text(f"**Deleted filter {name}.**")
-        else:
-            await message.reply_text("**No such filter.**")
+        await message.reply_text("**No such filter.**")
 
 
 @app.on_message(filters.text & ~filters.edited &
@@ -133,6 +138,7 @@ async def filters_re(_, message):
                         return
                     await message.reply_sticker(data)
     except Exception as e:
+        e = traceback.format_exc()
         print(e)
         pass
 

+ 10 - 3
wbb/modules/global_stats.py

@@ -25,6 +25,7 @@ from wbb import app, SUDOERS, BOT_ID, BOT_NAME
 from wbb.core.decorators.errors import capture_err
 from wbb.utils.fetch import fetch
 from wbb.modules import ALL_MODULES
+from wbb.utils.inlinefuncs import keywords_list
 from wbb.utils.dbfunctions import (
     get_served_chats,
     remove_served_chat,
@@ -90,6 +91,11 @@ async def global_stats(_, message):
     filters_count = _filters["filters_count"]
     filters_chats_count = _filters["chats_count"]
 
+    # Blacklisted filters count across chats
+    _filters = await get_blacklist_filters_count()
+    blacklist_filters_count = _filters["filters_count"]
+    blacklist_filters_chats_count = _filters["chats_count"]
+
     # Warns count across chats
     _warns = await get_warns_count()
     warns_count = _warns["warns_count"]
@@ -108,19 +114,20 @@ async def global_stats(_, message):
     for developer in developers:
         commits += developer['contributions']
     developers = len(developers)
-
     # Modules info
     modules_count = len(ALL_MODULES)
 
     msg = f"""
 **Global Stats of {BOT_NAME}**:
 **{modules_count}** Modules Loaded
+**{len(keywords_list)}** Inline Modules Loaded.
 **{gbans}** Globally banned users.
 **{filters_count}** Filters, Across **{filters_chats_count}** chats.
+**{blacklist_filters_count}** Blacklist Filters, Across **{blacklist_filters_chats_count}** chats.
 **{notes_count}** Notes, Across **{notes_chats_count}** chats.
 **{warns_count}** Warns, Across **{warns_chats_count}** chats.
 **{karmas_count}** Karma, Across **{karmas_chats_count}** chats.
 **{total_users}** Users, Across **{len(served_chats)}** chats.
-**{developers}** Developers And **{commits}** Commits On **[Github]({rurl})**."""
-
+**{developers}** Developers And **{commits}** Commits On **[Github]({rurl})**.
+"""
     await m.edit(msg, disable_web_page_preview=True)

+ 2 - 2
wbb/modules/sudoers.py

@@ -86,7 +86,7 @@ async def get_stats(_, message):
 async def ban_globally(_, message):
     if not message.reply_to_message:
         if len(message.command) < 3:
-            await message.reply_text("**Usage**\n/gban [USERNAME | USER_ID] [REASON]")
+            await message.reply_text("**Usage:**\n/gban [USERNAME | USER_ID] [REASON]")
             return
         user = message.text.split(None, 2)[1]
         reason = message.text.split(None, 2)[2]
@@ -136,7 +136,7 @@ __**New Global Ban**__
                 return
         return
     if len(message.command) < 2:
-        await message.reply_text("**Usage**\n/gban [REASON]")
+        await message.reply_text("**Usage:**\n/gban [REASON]")
         return
     reason = message.text.strip().split(None, 1)[1]
     from_user_id = message.from_user.id

+ 63 - 0
wbb/utils/dbfunctions.py

@@ -38,6 +38,7 @@ pmpermitdb = db.pmpermit
 welcomedb = db.welcome_text
 nsfwdb = db.nsfw
 captcha_cachedb = db.captcha_cache
+blacklist_filtersdb = db.blaclist_filters
 
 """ Notes functions """
 
@@ -605,3 +606,65 @@ async def get_captcha_cache():
     if not cache:
         return []
     return str_to_obj(cache['pickled'])
+
+
+""" BLACKLIST FILTERS SYSTEM """
+
+
+async def get_blacklist_filters_count() -> dict:
+    chats = blacklist_filtersdb.find({"chat_id": {"$lt": 0}})
+    if not chats:
+        return {}
+    chats_count = 0
+    filters_count = 0
+    for chat in await chats.to_list(length=1000000000):
+        filters = await get_blacklisted_words(chat['chat_id'])
+        filters_count += len(filters)
+        chats_count += 1
+    return {
+        "chats_count": chats_count,
+        "filters_count": filters_count
+    }
+
+
+async def get_blacklisted_words(chat_id: int) -> List[str]:
+    _filters = await blacklist_filtersdb.find_one({"chat_id": chat_id})
+    if _filters:
+        _filters = _filters['filters']
+    else:
+        _filters = []
+    return _filters
+
+
+async def save_blacklist_filter(chat_id: int, word: str):
+    word = word.lower().strip()
+    _filters = await get_blacklisted_words(chat_id)
+    _filters.append(word)
+    await blacklist_filtersdb.update_one(
+        {"chat_id": chat_id},
+        {
+            "$set": {
+                "filters": _filters
+            }
+        },
+        upsert=True
+    )
+
+
+async def delete_blacklist_filter(chat_id: int, word: str) -> bool:
+    filtersd = await get_blacklisted_words(chat_id)
+    word = word.lower().strip()
+    if word in filtersd:
+        filtersd.remove(word)
+        await blacklist_filtersdb.update_one(
+            {"chat_id": chat_id},
+            {
+                "$set": {
+                    "filters": filtersd
+                }
+            },
+            upsert=True
+        )
+        return True
+    return False
+

+ 1 - 0
wbb/utils/filter_groups.py

@@ -29,3 +29,4 @@ regex_group = 5
 welcome_captcha_group = 6
 antiflood_group = 7
 nsfw_detect_group = 8
+blacklist_filters_group = 9

+ 16 - 8
wbb/utils/inlinefuncs.py

@@ -55,16 +55,16 @@ from wbb import (
     SUDOERS, USERBOT_NAME, MESSAGE_DUMP_CHAT
 )
 
+keywords_list = [
+    "alive", "ping", "tr", "ud", "google", "bitly",
+    "wall", "yt", "torrent", "lyrics", "wiki",
+    "speedtest", "music", "saavn", "deezer",
+    "gh_repo", "gh_user", "search", "paste",
+    "nsfw_scan", "ytmusic", "carbon", "info"
+]
 
 async def inline_help_func(__HELP__):
     buttons = InlineKeyboard(row_width=3)
-    keywords_list = [
-        "alive", "ping", "tr", "ud", "google", "bitly",
-        "wall", "yt", "torrent", "lyrics", "wiki",
-        "speedtest", "music", "saavn", "deezer",
-        "gh_repo", "gh_user", "search", "paste",
-        "nsfw_scan", "ytmusic", "carbon", "info"
-    ]
     buttons.add(*[(InlineKeyboardButton(
         text=i, switch_inline_query_current_chat=i)) for i in keywords_list])
     answerss = [
@@ -75,7 +75,15 @@ async def inline_help_func(__HELP__):
                 "Click A Button To Get Started."),
             thumb_url="https://hamker.me/cy00x5x.png",
             reply_markup=buttons
-        )
+        ),
+        InlineQueryResultArticle(
+            title="Github Repo",
+            description="Get Github Respository Of Bot.",
+            input_message_content=InputTextMessageContent(
+                "https://github.com/thehamkercat/WilliamButcherBot"
+                ),
+            thumb_url="https://hamker.me/gjc9fo3.png"
+            )
     ]
     answerss = await alive_function(answerss)
     return answerss