Ver código fonte

fix: Minor changes & Bug fixes (#227)

SI_NN_ER_LS 2 anos atrás
pai
commit
856b44a836
3 arquivos alterados com 218 adições e 127 exclusões
  1. 106 64
      wbb/modules/filters.py
  2. 104 63
      wbb/modules/notes.py
  3. 8 0
      wbb/utils/dbfunctions.py

+ 106 - 64
wbb/modules/filters.py

@@ -24,6 +24,7 @@ SOFTWARE.
 import re
 
 from pyrogram import filters
+from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery
 
 from wbb import app
 from wbb.core.decorators.errors import capture_err
@@ -34,22 +35,24 @@ from wbb.utils.dbfunctions import (
     get_filter,
     get_filters_names,
     save_filter,
+    deleteall_filters,
 )
 from wbb.utils.filter_groups import chat_filters_group
 from wbb.utils.functions import extract_text_and_keyb
+from wbb.modules.admin import member_permissions
 from wbb.modules.notes import extract_urls
 
 __MODULE__ = "Filters"
 __HELP__ = """/filters To Get All The Filters In The Chat.
 /filter [FILTER_NAME] To Save A Filter(reply to a message).
 
-Supported filter types are Text, Animation, Photo, Document, Video, video notes, Audio, Voice
+Supported filter types are Text, Animation, Photo, Document, Video, video notes, Audio, Voice.
 
 To use more words in a filter use.
-`/filter Hey_there` To filter "Hey there"
+`/filter Hey_there` To filter "Hey there".
 
 /stop [FILTER_NAME] To Stop A Filter.
-
+/stopall To delete all the filters in a chat (permanently).
 
 You can use markdown or html to save text too.
 
@@ -60,68 +63,72 @@ Checkout /markdownhelp to know more about formattings and other syntax.
 @app.on_message(filters.command("filter") & ~filters.private)
 @adminsOnly("can_change_info")
 async def save_filters(_, message):
-    if len(message.command) < 2 or not message.reply_to_message:
-        return await message.reply_text(
-            "**Usage:**\nReply to a message with /filter [FILTER_NAME] To set a new filter."
-        )    
-    name = message.text.split(None, 1)[1].strip()
-    if not name:
-        return await message.reply_text("**Usage:**\n__/filter [FILTER_NAME]__")    
-    chat_id = message.chat.id
-    replied_message = message.reply_to_message
-    text = name.split(" ", 1)    
-    if len(text) > 1:
-        name = text[0]
-        data = text[1].strip()
-        if replied_message.sticker or replied_message.video_note:
-            data = None
-    else:
-        if replied_message.sticker or replied_message.video_note:
-            data = None
-        elif not replied_message.text and not replied_message.caption:
-            data = None
+    try:
+        if len(message.command) < 2 or not message.reply_to_message:
+            return await message.reply_text(
+                "**Usage:**\nReply to a message with /filter [FILTER_NAME] To set a new filter."
+            )
+        text = message.text.markdown
+        name = text.split(None, 1)[1].strip()
+        if not name:
+            return await message.reply_text("**Usage:**\n__/filter [FILTER_NAME]__")
+        chat_id = message.chat.id
+        replied_message = message.reply_to_message
+        text = name.split(" ", 1)
+        if len(text) > 1:
+            name = text[0]
+            data = text[1].strip()
+            if replied_message.sticker or replied_message.video_note:
+                data = None
         else:
-            data = replied_message.text.markdown if replied_message.text else replied_message.caption.markdown
-    if replied_message.text:
-        _type = "text"
-        file_id = None
-    if replied_message.sticker:
-        _type = "sticker"
-        file_id = replied_message.sticker.file_id
-    if replied_message.animation:
-        _type = "animation"
-        file_id = replied_message.animation.file_id
-    if replied_message.photo:
-        _type = "photo"
-        file_id = replied_message.photo.file_id
-    if replied_message.document:
-        _type = "document"
-        file_id = replied_message.document.file_id
-    if replied_message.video:
-        _type = "video"
-        file_id = replied_message.video.file_id
-    if replied_message.video_note:
-        _type = "video_note"
-        file_id = replied_message.video_note.file_id
-    if replied_message.audio:
-        _type = "audio"
-        file_id = replied_message.audio.file_id
-    if replied_message.voice:
-        _type = "voice"
-        file_id = replied_message.voice.file_id    
-    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])
-            data = data + response
-    name = name.replace("_", " ")
-    _filter = {
-        "type": _type,
-        "data": data,
-        "file_id": file_id,
-    }
-    await save_filter(chat_id, name, _filter)
-    return await message.reply_text(f"__**Saved filter {name}.**__")
+            if replied_message.sticker or replied_message.video_note:
+                data = None
+            elif 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
+        if replied_message.text:
+            _type = "text"
+            file_id = None
+        if replied_message.sticker:
+            _type = "sticker"
+            file_id = replied_message.sticker.file_id
+        if replied_message.animation:
+            _type = "animation"
+            file_id = replied_message.animation.file_id
+        if replied_message.photo:
+            _type = "photo"
+            file_id = replied_message.photo.file_id
+        if replied_message.document:
+            _type = "document"
+            file_id = replied_message.document.file_id
+        if replied_message.video:
+            _type = "video"
+            file_id = replied_message.video.file_id
+        if replied_message.video_note:
+            _type = "video_note"
+            file_id = replied_message.video_note.file_id
+        if replied_message.audio:
+            _type = "audio"
+            file_id = replied_message.audio.file_id
+        if replied_message.voice:
+            _type = "voice"
+            file_id = replied_message.voice.file_id
+        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])
+                data = data + response
+        name = name.replace("_", " ")
+        _filter = {
+            "type": _type,
+            "data": data,
+            "file_id": file_id,
+        }
+        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`**")
 
 
 @app.on_message(filters.command("filters") & ~filters.private)
@@ -234,3 +241,38 @@ async def filters_re(_, message):
                     caption=data,
                     reply_markup=keyb,
                 )
+
+
+@app.on_message(filters.command("stopall") & ~filters.private)
+@adminsOnly("can_change_info")
+async def stop_all(_, message):
+    _filters = await get_filters_names(message.chat.id)
+    if not _filters:
+        await message.reply_text("**No filters in this chat.**")
+    else:
+        keyboard = InlineKeyboardMarkup(
+            [
+                [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)
+
+
+@app.on_callback_query(filters.regex("stop_(.*)"))
+async def stop_all_cb(_, cb):
+    chat_id = cb.message.chat.id
+    from_user = cb.from_user
+    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)
+    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.**")
+    if input == "no":
+        await cb.message.reply_to_message.delete()
+        await cb.message.delete()

+ 104 - 63
wbb/modules/notes.py

@@ -24,12 +24,14 @@ SOFTWARE.
 from re import findall
 
 from pyrogram import filters
+from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup, CallbackQuery
 
 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
+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
 
 __MODULE__ = "Notes"
@@ -37,13 +39,14 @@ __HELP__ = """/notes To Get All The Notes In The Chat.
 
 /save [NOTE_NAME] To Save A Note.
 
-Supported note types are Text, Animation, Photo, Document, Video, video notes, Audio, Voice
+Supported note types are Text, Animation, Photo, Document, Video, video notes, Audio, Voice.
 
-To change caption of any files use.\n/save [NOTE_NAME] [NEW_CAPTION]
+To change caption of any files use.\n/save [NOTE_NAME] [NEW_CAPTION].
 
 #NOTE_NAME To Get A Note.
 
 /delete [NOTE_NAME] To Delete A Note.
+/deleteall To delete all the notes in a chat (permanently).
 
 Checkout /markdownhelp to know more about formattings and other syntax.
 """
@@ -55,7 +58,7 @@ def extract_urls(reply_markup):
         buttons = reply_markup.inline_keyboard
         for i, row in enumerate(buttons):
             for j, button in enumerate(row):
-                 if button.url:
+                if button.url:
                     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
@@ -69,71 +72,74 @@ def extract_urls(reply_markup):
 @app.on_message(filters.command("save") & ~filters.private)
 @adminsOnly("can_change_info")
 async def save_notee(_, message):
-    if len(message.command) < 2 or not message.reply_to_message:
-        await eor(
-            message,
-            text="**Usage:**\nReply to a message with /save [NOTE_NAME] to save a new note.",
-        )
-    else:
-        name = message.text.split(None, 1)[1].strip()
-        if not name:
-            return await eor(message, text="**Usage**\n__/save [NOTE_NAME]__")
-        replied_message = message.reply_to_message
-        text = name.split(" ", 1)
-        if len(text) > 1:
-            name = text[0]
-            data = text[1].strip()
-            if replied_message.sticker or replied_message.video_note:
-                data = None
+    try:
+        if len(message.command) < 2 or not message.reply_to_message:
+            await eor(
+                message,
+                text="**Usage:**\nReply to a message with /save [NOTE_NAME] to save a new note.",
+            )
         else:
-            if replied_message.sticker or replied_message.video_note:
-                data = None
-            elif not replied_message.text and not replied_message.caption:
-                data = None
+            text = message.text.markdown
+            name = text.split(None, 1)[1].strip()
+            if not name:
+                return await eor(message, text="**Usage**\n__/save [NOTE_NAME]__")
+            replied_message = message.reply_to_message
+            text = name.split(" ", 1)
+            if len(text) > 1:
+                name = text[0]
+                data = text[1].strip()
+                if replied_message.sticker or replied_message.video_note:
+                    data = None
             else:
-                data = replied_message.text.markdown if replied_message.text else replied_message.caption.markdown
-        if replied_message.text:
-            _type = "text"
-            file_id = None
-        if replied_message.sticker:
-            _type = "sticker"
-            file_id = replied_message.sticker.file_id
-        if replied_message.animation:
-            _type = "animation"
-            file_id = replied_message.animation.file_id
-        if replied_message.photo:
-            _type = "photo"
-            file_id = replied_message.photo.file_id
-        if replied_message.document:
-            _type = "document"
-            file_id = replied_message.document.file_id
-        if replied_message.video:
-            _type = "video"
-            file_id = replied_message.video.file_id
-        if replied_message.video_note:
-            _type = "video_note"
-            file_id = replied_message.video_note.file_id
-        if replied_message.audio:
-            _type = "audio"
-            file_id = replied_message.audio.file_id
-        if replied_message.voice:
-            _type = "voice"
-            file_id = replied_message.voice.file_id
-        if replied_message.reply_markup:
-            if not "~" in data:
+                if replied_message.sticker or replied_message.video_note:
+                    data = None
+                elif 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
+            if replied_message.text:
+                _type = "text"
+                file_id = None
+            if replied_message.sticker:
+                _type = "sticker"
+                file_id = replied_message.sticker.file_id
+            if replied_message.animation:
+                _type = "animation"
+                file_id = replied_message.animation.file_id
+            if replied_message.photo:
+                _type = "photo"
+                file_id = replied_message.photo.file_id
+            if replied_message.document:
+                _type = "document"
+                file_id = replied_message.document.file_id
+            if replied_message.video:
+                _type = "video"
+                file_id = replied_message.video.file_id
+            if replied_message.video_note:
+                _type = "video_note"
+                file_id = replied_message.video_note.file_id
+            if replied_message.audio:
+                _type = "audio"
+                file_id = replied_message.audio.file_id
+            if replied_message.voice:
+                _type = "voice"
+                file_id = replied_message.voice.file_id
+            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])
                     data = data + response
-        note = {
-            "type": _type,
-            "data": data,
-            "file_id": file_id,
-        }
-        prefix = message.text.split()[0][0]
-        chat_id = message.chat.id if prefix != USERBOT_PREFIX else USERBOT_ID
-        await save_note(chat_id, name, note)
-        await eor(message, text=f"__**Saved note {name}.**__")
+            note = {
+                "type": _type,
+                "data": data,
+                "file_id": file_id,
+            }
+            prefix = message.text.split()[0][0]
+            chat_id = message.chat.id if prefix != USERBOT_PREFIX else USERBOT_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`**")
 
 
 @app2.on_message(
@@ -284,3 +290,38 @@ async def del_note(_, message):
         await eor(message, text=f"**Deleted note {name} successfully.**")
     else:
         await eor(message, text="**No such note.**")
+
+
+@app.on_message(filters.command("deleteall") & ~filters.private)
+@adminsOnly("can_change_info")
+async def delete_all(_, message):
+    _notes = await get_note_names(message.chat.id)
+    if not _notes:
+        return await message.reply_text("**No notes in this chat.**")
+    else:
+        keyboard = InlineKeyboardMarkup(
+            [
+                [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)
+
+
+@app.on_callback_query(filters.regex("delete_(.*)"))
+async def delete_all_cb(_, cb):
+    chat_id = cb.message.chat.id
+    from_user = cb.from_user
+    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)
+    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.**")
+    if input == "no":
+        await cb.message.reply_to_message.delete()
+        await cb.message.delete()

+ 8 - 0
wbb/utils/dbfunctions.py

@@ -126,6 +126,10 @@ async def delete_note(chat_id: int, name: str) -> bool:
     return False
 
 
+async def deleteall_notes(chat_id: int):
+    return await notesdb.delete_one({"chat_id": chat_id})
+
+
 async def get_filters_count() -> dict:
     chats_count = 0
     filters_count = 0
@@ -186,6 +190,10 @@ async def delete_filter(chat_id: int, name: str) -> bool:
     return False
 
 
+async def deleteall_filters(chat_id: int):
+    return await filtersdb.delete_one({"chat_id": chat_id})
+
+
 async def int_to_alpha(user_id: int) -> str:
     alphabet = list(ascii_lowercase)[:10]
     text = ""