فهرست منبع

fix: Use .get() instead of subscripts

TheHamkerCat 2 سال پیش
والد
کامیت
e6d44d2494
5فایلهای تغییر یافته به همراه134 افزوده شده و 54 حذف شده
  1. 1 7
      wbb/core/decorators/errors.py
  2. 48 16
      wbb/modules/filters.py
  3. 22 8
      wbb/modules/greetings.py
  4. 60 20
      wbb/modules/notes.py
  5. 3 3
      wbb/utils/functions.py

+ 1 - 7
wbb/core/decorators/errors.py

@@ -1,6 +1,5 @@
 """ WRITTEN BY @pokurt, https://github.com/pokurt"""
 
-import sys
 import traceback
 from functools import wraps
 
@@ -37,12 +36,7 @@ def capture_err(func):
             await app.leave_chat(message.chat.id)
             return
         except Exception as err:
-            exc_type, exc_obj, exc_tb = sys.exc_info()
-            errors = traceback.format_exception(
-                etype=exc_type,
-                value=exc_obj,
-                tb=exc_tb,
-            )
+            errors = traceback.format_exc()
             error_feedback = split_limits(
                 "**ERROR** | `{}` | `{}`\n\n```{}```\n\n```{}```\n".format(
                     0 if not message.from_user else message.from_user.id,

+ 48 - 16
wbb/modules/filters.py

@@ -81,19 +81,33 @@ async def save_filters(_, message):
         if len(text) > 1:
             name = text[0]
             data = text[1].strip()
-            if replied_message and (replied_message.sticker or replied_message.video_note):
+            if replied_message and (
+                replied_message.sticker or replied_message.video_note
+            ):
                 data = None
         else:
-            if replied_message and (replied_message.sticker or replied_message.video_note):
+            if replied_message and (
+                replied_message.sticker or replied_message.video_note
+            ):
                 data = None
-            elif replied_message and not replied_message.text and not replied_message.caption:
+            elif (
+                replied_message
+                and not replied_message.text
+                and not replied_message.caption
+            ):
                 data = None
             else:
-                data = replied_message.text.markdown if replied_message.text else replied_message.caption.markdown
+                data = (
+                    replied_message.text.markdown
+                    if replied_message.text
+                    else replied_message.caption.markdown
+                )
                 match = "/filter " + name
                 if not message.reply_to_message and message.text:
                     if match == data:
-                        return await message.reply_text("**Usage:**\n__/filter [FILTER_NAME] [CONTENT]__\n`-----------OR-----------`\nReply to a message with.\n/filter [FILTER_NAME].")
+                        return await message.reply_text(
+                            "**Usage:**\n__/filter [FILTER_NAME] [CONTENT]__\n`-----------OR-----------`\nReply to a message with.\n/filter [FILTER_NAME]."
+                        )
                 elif not message.reply_to_message and not message.text:
                     if match == data:
                         data = None
@@ -127,12 +141,16 @@ async def save_filters(_, message):
         if replied_message.reply_markup and not "~" in data:
             urls = extract_urls(replied_message.reply_markup)
             if urls:
-                response = "\n".join([f"{name}=[{text}, {url}]" for name, text, url in urls])
+                response = "\n".join(
+                    [f"{name}=[{text}, {url}]" for name, text, url in urls]
+                )
                 data = data + response
         if data:
             data = await check_format(ikb, data)
             if not data:
-                return await message.reply_text("**Wrong formatting, check the help section.**")
+                return await message.reply_text(
+                    "**Wrong formatting, check the help section.**"
+                )
         name = name.replace("_", " ")
         _filter = {
             "type": _type,
@@ -142,7 +160,9 @@ async def save_filters(_, message):
         await save_filter(chat_id, name, _filter)
         return await message.reply_text(f"__**Saved filter {name}.**__")
     except UnboundLocalError:
-        return await message.reply_text("**Replied message is inaccessible.\n`Forward the message and try again`**")
+        return await message.reply_text(
+            "**Replied message is inaccessible.\n`Forward the message and try again`**"
+        )
 
 
 @app.on_message(filters.command("filters") & ~filters.private)
@@ -193,13 +213,15 @@ async def filters_re(_, message):
             _filter = await get_filter(chat_id, word)
             data_type = _filter["type"]
             data = _filter["data"]
-            file_id = _filter["file_id"]
+            file_id = _filter.get("file_id")
             keyb = None
             if data:
                 if "{chat}" in data:
                     data = data.replace("{chat}", (await app.get_chat(chat_id)).title)
                 if "{name}" in data:
-                    data = data.replace("{name}", (await app.get_users(user_id)).mention)
+                    data = data.replace(
+                        "{name}", (await app.get_users(user_id)).mention
+                    )
                 if re.findall(r"\[.+\,.+\]", data):
                     keyboard = extract_text_and_keyb(ikb, data)
                     if keyboard:
@@ -261,6 +283,7 @@ async def filters_re(_, message):
                     caption=data,
                     reply_markup=keyb,
                 )
+            return  # NOTE: Avoid filter spam
 
 
 @app.on_message(filters.command("stopall") & ~filters.private)
@@ -272,12 +295,16 @@ async def stop_all(_, message):
     else:
         keyboard = InlineKeyboardMarkup(
             [
-                [InlineKeyboardButton("YES, DO IT", callback_data="stop_yes"), 
-                 InlineKeyboardButton("Cancel", callback_data="stop_no")
+                [
+                    InlineKeyboardButton("YES, DO IT", callback_data="stop_yes"),
+                    InlineKeyboardButton("Cancel", callback_data="stop_no"),
                 ]
             ]
         )
-        await message.reply_text("**Are you sure you want to delete all the filters in this chat forever ?.**", reply_markup=keyboard)
+        await message.reply_text(
+            "**Are you sure you want to delete all the filters in this chat forever ?.**",
+            reply_markup=keyboard,
+        )
 
 
 @app.on_callback_query(filters.regex("stop_(.*)"))
@@ -287,12 +314,17 @@ async def stop_all_cb(_, cb):
     permissions = await member_permissions(chat_id, from_user.id)
     permission = "can_change_info"
     if permission not in permissions:
-        return await cb.answer(f"You don't have the required permission.\n Permission: {permission}", show_alert=True)
+        return await cb.answer(
+            f"You don't have the required permission.\n Permission: {permission}",
+            show_alert=True,
+        )
     input = cb.data.split("_", 1)[1]
     if input == "yes":
         stoped_all = await deleteall_filters(chat_id)
         if stoped_all:
-            return await cb.message.edit("**Successfully deleted all filters on this chat.**")
+            return await cb.message.edit(
+                "**Successfully deleted all filters on this chat.**"
+            )
     if input == "no":
         await cb.message.reply_to_message.delete()
-        await cb.message.delete()
+        await cb.message.delete()

+ 22 - 8
wbb/modules/greetings.py

@@ -107,6 +107,7 @@ async def get_initial_captcha_cache():
 
 loop.create_task(get_initial_captcha_cache())
 
+
 async def handle_new_member(message: Message, member, chat):
     global answers_dicc
 
@@ -116,7 +117,7 @@ async def handle_new_member(message: Message, member, chat):
     # Mute new member and send message with button
     if not await is_captcha_on(message.chat.id):
         if member.is_bot:
-            return 
+            return
         return await send_welcome_message(message.chat, message.from_user.id)
 
     try:
@@ -129,7 +130,7 @@ async def handle_new_member(message: Message, member, chat):
                 chat.id,
                 f"{member.mention} was globally banned, and got removed,"
                 + " if you think this is a false gban, you can appeal"
-                + " for this ban in support chat."
+                + " for this ban in support chat.",
             )
             return
 
@@ -214,6 +215,7 @@ async def handle_new_member(message: Message, member, chat):
     )
     await asyncio.sleep(0.5)
 
+
 @app.on_message(filters.new_chat_members, group=welcome_captcha_group)
 @capture_err
 async def welcome(_, message: Message):
@@ -222,6 +224,7 @@ async def welcome(_, message: Message):
     for member in members:
         return await handle_new_member(message, member, chat)
 
+
 async def send_welcome_message(chat: Chat, user_id: int, delete: bool = False):
     welcome, raw_text, file_id = await get_welcome(chat.id)
 
@@ -420,7 +423,7 @@ async def set_welcome_func(_, message):
         if replied_message.animation:
             welcome = "Animation"
             file_id = replied_message.animation.file_id
-            text = replied_message.caption      
+            text = replied_message.caption
             if not text:
                 return await message.reply_text(usage, reply_markup=key)
             raw_text = text.markdown
@@ -439,16 +442,25 @@ async def set_welcome_func(_, message):
         if replied_message.reply_markup and not "~" in raw_text:
             urls = extract_urls(replied_message.reply_markup)
             if urls:
-                response = "\n".join([f"{name}=[{text}, {url}]" for name, text, url in urls])
+                response = "\n".join(
+                    [f"{name}=[{text}, {url}]" for name, text, url in urls]
+                )
                 raw_text = raw_text + response
         raw_text = await check_format(ikb, raw_text)
         if raw_text:
             await set_welcome(chat_id, welcome, raw_text, file_id)
-            return await message.reply_text("Welcome message has been successfully set.")
+            return await message.reply_text(
+                "Welcome message has been successfully set."
+            )
         else:
-            return await message.reply_text("Wrong formatting, check the help section.\n\n**Usage:**\nText: `Text`\nText + Buttons: `Text ~ Buttons`", reply_markup=key)
+            return await message.reply_text(
+                "Wrong formatting, check the help section.\n\n**Usage:**\nText: `Text`\nText + Buttons: `Text ~ Buttons`",
+                reply_markup=key,
+            )
     except UnboundLocalError:
-        return await message.reply_text("**Only Text, Gif and Photo welcome message are supported.**")
+        return await message.reply_text(
+            "**Only Text, Gif and Photo welcome message are supported.**"
+        )
 
 
 @app.on_message(filters.command("del_welcome") & ~filters.private)
@@ -471,4 +483,6 @@ async def get_welcome_func(_, message):
 
     await send_welcome_message(chat, message.from_user.id)
 
-    await message.reply_text(f'Welcome: {welcome}\n\nFile_id: `{file_id}`\n\n`{raw_text.replace("`", "")}`')
+    await message.reply_text(
+        f'Welcome: {welcome}\n\nFile_id: `{file_id}`\n\n`{raw_text.replace("`", "")}`'
+    )

+ 60 - 20
wbb/modules/notes.py

@@ -31,7 +31,13 @@ from wbb import SUDOERS, USERBOT_ID, USERBOT_PREFIX, app, app2, eor
 from wbb.core.decorators.errors import capture_err
 from wbb.core.decorators.permissions import adminsOnly
 from wbb.core.keyboard import ikb
-from wbb.utils.dbfunctions import delete_note, get_note, get_note_names, save_note, deleteall_notes
+from wbb.utils.dbfunctions import (
+    delete_note,
+    get_note,
+    get_note_names,
+    save_note,
+    deleteall_notes,
+)
 from wbb.modules.admin import member_permissions
 from wbb.utils.functions import extract_text_and_keyb, check_format
 
@@ -60,7 +66,11 @@ def extract_urls(reply_markup):
         for i, row in enumerate(buttons):
             for j, button in enumerate(row):
                 if button.url:
-                    name = "\n~\nbutton" if i * len(row) + j + 1 == 1 else f"button{i * len(row) + j + 1}"
+                    name = (
+                        "\n~\nbutton"
+                        if i * len(row) + j + 1 == 1
+                        else f"button{i * len(row) + j + 1}"
+                    )
                     urls.append((f"{name}", button.text, button.url))
     return urls
 
@@ -86,19 +96,33 @@ async def save_notee(_, message):
             if len(text) > 1:
                 name = text[0]
                 data = text[1].strip()
-                if replied_message and (replied_message.sticker or replied_message.video_note):
+                if replied_message and (
+                    replied_message.sticker or replied_message.video_note
+                ):
                     data = None
             else:
-                if replied_message and (replied_message.sticker or replied_message.video_note):
+                if replied_message and (
+                    replied_message.sticker or replied_message.video_note
+                ):
                     data = None
-                elif replied_message and not replied_message.text and not replied_message.caption:
+                elif (
+                    replied_message
+                    and not replied_message.text
+                    and not replied_message.caption
+                ):
                     data = None
                 else:
-                    data = replied_message.text.markdown if replied_message.text else replied_message.caption.markdown
+                    data = (
+                        replied_message.text.markdown
+                        if replied_message.text
+                        else replied_message.caption.markdown
+                    )
                     match = "/save " + name
                     if not message.reply_to_message and message.text:
                         if match == data:
-                            return await message.reply_text("**Usage:**\n__/save [NOTE_NAME] [CONTENT]__\n`-----------OR-----------`\nReply to a message with.\n/save [NOTE_NAME]")
+                            return await message.reply_text(
+                                "**Usage:**\n__/save [NOTE_NAME] [CONTENT]__\n`-----------OR-----------`\nReply to a message with.\n/save [NOTE_NAME]"
+                            )
                     elif not message.reply_to_message and not message.text:
                         if match == data:
                             data = None
@@ -132,22 +156,28 @@ async def save_notee(_, message):
             if replied_message.reply_markup and not "~" in data:
                 urls = extract_urls(replied_message.reply_markup)
                 if urls:
-                    response = "\n".join([f"{name}=[{text}, {url}]" for name, text, url in urls])
+                    response = "\n".join(
+                        [f"{name}=[{text}, {url}]" for name, text, url in urls]
+                    )
                     data = data + response
             if data:
                 data = await check_format(ikb, data)
                 if not data:
-                    return await message.reply_text("**Wrong formatting, check the help section.**")
+                    return await message.reply_text(
+                        "**Wrong formatting, check the help section.**"
+                    )
             note = {
                 "type": _type,
                 "data": data,
                 "file_id": file_id,
             }
-            chat_id = message.chat.id 
+            chat_id = message.chat.id
             await save_note(chat_id, name, note)
             await eor(message, text=f"__**Saved note {name}.**__")
     except UnboundLocalError:
-        return await message.reply_text("**Replied message is inaccessible.\n`Forward the message and try again`**")
+        return await message.reply_text(
+            "**Replied message is inaccessible.\n`Forward the message and try again`**"
+        )
 
 
 @app2.on_message(
@@ -204,8 +234,8 @@ async def get_one_note_userbot(_, message):
 @app.on_message(filters.regex(r"^#.+") & filters.text & ~filters.private)
 @capture_err
 async def get_one_note(_, message):
-    user_id = message.from_user.id 
-    chat_id = message.chat.id 
+    user_id = message.from_user.id
+    chat_id = message.chat.id
     name = message.text.replace("#", "", 1)
     if not name:
         return
@@ -214,7 +244,7 @@ async def get_one_note(_, message):
         return
     type = _note["type"]
     data = _note["data"]
-    file_id = _note["file_id"]
+    file_id = _note.get("file_id")
     keyb = None
     if data:
         if "{chat}" in data:
@@ -280,6 +310,7 @@ async def get_one_note(_, message):
             reply_markup=keyb,
         )
 
+
 @app2.on_message(
     filters.command("delete", prefixes=USERBOT_PREFIX)
     & ~filters.forwarded
@@ -315,12 +346,16 @@ async def delete_all(_, message):
     else:
         keyboard = InlineKeyboardMarkup(
             [
-                [InlineKeyboardButton("YES, DO IT", callback_data="delete_yes"), 
-                 InlineKeyboardButton("Cancel", callback_data="delete_no")
+                [
+                    InlineKeyboardButton("YES, DO IT", callback_data="delete_yes"),
+                    InlineKeyboardButton("Cancel", callback_data="delete_no"),
                 ]
             ]
         )
-        await message.reply_text("**Are you sure you want to delete all the notes in this chat forever ?.**", reply_markup=keyboard)
+        await message.reply_text(
+            "**Are you sure you want to delete all the notes in this chat forever ?.**",
+            reply_markup=keyboard,
+        )
 
 
 @app.on_callback_query(filters.regex("delete_(.*)"))
@@ -330,12 +365,17 @@ async def delete_all_cb(_, cb):
     permissions = await member_permissions(chat_id, from_user.id)
     permission = "can_change_info"
     if permission not in permissions:
-        return await cb.answer(f"You don't have the required permission.\n Permission: {permission}", show_alert=True)
+        return await cb.answer(
+            f"You don't have the required permission.\n Permission: {permission}",
+            show_alert=True,
+        )
     input = cb.data.split("_", 1)[1]
     if input == "yes":
         stoped_all = await deleteall_notes(chat_id)
         if stoped_all:
-            return await cb.message.edit("**Successfully deleted all notes on this chat.**")
+            return await cb.message.edit(
+                "**Successfully deleted all notes on this chat.**"
+            )
     if input == "no":
         await cb.message.reply_to_message.delete()
-        await cb.message.delete()
+        await cb.message.delete()

+ 3 - 3
wbb/utils/functions.py

@@ -315,20 +315,20 @@ def extract_text_and_keyb(ikb, text: str, row_width: int = 2):
         return
     return text, keyboard
 
-# BUILDING AN AI BY SI_NN_ER_LS
+
 async def check_format(ikb, raw_text: str):
     keyb = findall(r"\[.+\,.+\]", raw_text)
     if keyb and not "~" in raw_text:
         raw_text = raw_text.replace("button=", "\n~\nbutton=")
         return raw_text
     if "~" in raw_text and not keyb:
-        raw_text =raw_text.replace("~", "")
+        raw_text = raw_text.replace("~", "")
         return raw_text
     if "~" in raw_text and keyb:
         if not extract_text_and_keyb(ikb, raw_text):
             return ""
         else:
-            return raw_text 
+            return raw_text
     else:
         return raw_text