From dd6957f2398277ede9e949d5df64154bf219d231 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Thu, 30 Jun 2022 14:49:03 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=20Update=20to=20v1.1.4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pagermaid/__init__.py | 2 +- pagermaid/listener.py | 14 +++++++++++++- pagermaid/modules/reload.py | 3 +++ pagermaid/single_utils.py | 2 +- pyromod/listen/listen.py | 38 +++++++++++++++++++++++++++++++------ pyromod/utils/errors.py | 11 +++++++++++ 6 files changed, 61 insertions(+), 9 deletions(-) diff --git a/pagermaid/__init__.py b/pagermaid/__init__.py index 6aa03dc..b892bbd 100644 --- a/pagermaid/__init__.py +++ b/pagermaid/__init__.py @@ -14,7 +14,7 @@ import pyromod.listen from pyrogram import Client import sys -pgm_version = "1.1.3" +pgm_version = "1.1.4" CMD_LIST = {} module_dir = __path__[0] working_dir = getcwd() diff --git a/pagermaid/listener.py b/pagermaid/listener.py index dc156e3..1e9b6db 100644 --- a/pagermaid/listener.py +++ b/pagermaid/listener.py @@ -16,7 +16,7 @@ from pyrogram.handlers import MessageHandler, EditedMessageHandler from pagermaid import help_messages, logs, Config, bot, read_context, all_permissions from pagermaid.group_manager import Permission -from pagermaid.single_utils import Message, AlreadyInConversationError, TimeoutConversationError +from pagermaid.single_utils import Message, AlreadyInConversationError, TimeoutConversationError, ListenerCanceled from pagermaid.utils import lang, attach_report, sudo_filter, alias_command, get_permission_name, process_exit from pagermaid.utils import client as httpx_client from pagermaid.hook import Hook @@ -146,6 +146,12 @@ def listener(**args): ) with contextlib.suppress(BaseException): await message.edit(lang("conversation_timed_out_error")) + except ListenerCanceled: + logs.warning( + "Listener Canceled While Processing Commands.." + ) + with contextlib.suppress(BaseException): + await message.edit(lang("reload_des")) except UserNotParticipant: pass except ContinuePropagation as e: @@ -225,6 +231,12 @@ def raw_listener(filter_s): ) with contextlib.suppress(BaseException): await message.edit(lang("conversation_timed_out_error")) + except ListenerCanceled: + logs.warning( + "Listener Canceled While Processing Commands.." + ) + with contextlib.suppress(BaseException): + await message.edit(lang("reload_des")) except SystemExit: await process_exit(start=False, _client=client, message=message) await Hook.shutdown() diff --git a/pagermaid/modules/reload.py b/pagermaid/modules/reload.py index 8a4ed9c..fe221f2 100644 --- a/pagermaid/modules/reload.py +++ b/pagermaid/modules/reload.py @@ -1,3 +1,4 @@ +import contextlib import importlib import pagermaid.config import pagermaid.modules @@ -10,6 +11,8 @@ from pagermaid.utils import lang, Message def reload_all(): bot.dispatcher.remove_all_handlers() bot.job.remove_all_jobs() + with contextlib.suppress(RuntimeError): + bot.cancel_all_listener() loaded_plugins = list(pagermaid.modules.plugin_list) loaded_plugins.extend(iter(pagermaid.modules.module_list)) # init diff --git a/pagermaid/single_utils.py b/pagermaid/single_utils.py index 31594be..168529c 100644 --- a/pagermaid/single_utils.py +++ b/pagermaid/single_utils.py @@ -9,7 +9,7 @@ from pyrogram import Client from pyrogram.types import Message from pyromod.utils.conversation import Conversation -from pyromod.utils.errors import AlreadyInConversationError, TimeoutConversationError +from pyromod.utils.errors import AlreadyInConversationError, TimeoutConversationError, ListenerCanceled from sqlitedict import SqliteDict diff --git a/pyromod/listen/listen.py b/pyromod/listen/listen.py index 39e2e70..81bfff4 100644 --- a/pyromod/listen/listen.py +++ b/pyromod/listen/listen.py @@ -29,12 +29,7 @@ from pagermaid.scheduler import add_delete_message_job from ..utils import patch, patchable from ..utils.conversation import Conversation -from ..utils.errors import TimeoutConversationError - - -class ListenerCanceled(Exception): - pass - +from ..utils.errors import TimeoutConversationError, ListenerCanceled pyrogram.errors.ListenerCanceled = ListenerCanceled @@ -128,6 +123,37 @@ class MessageHandler: ) +@patch(pyrogram.handlers.edited_message_handler.EditedMessageHandler) +class EditedMessageHandler: + @patchable + def __init__(self, callback: callable, filters=None): + self.user_callback = callback + self.old__init__(self.resolve_listener, filters) + + @patchable + async def resolve_listener(self, client, message, *args): + listener = client.listening.get(message.chat.id) + if listener and not listener['future'].done(): + listener['future'].set_result(message) + else: + if listener and listener['future'].done(): + client.clear_listener(message.chat.id, listener['future']) + await self.user_callback(client, message, *args) + + @patchable + async def check(self, client, update): + listener = client.listening.get(update.chat.id) + + if listener and not listener['future'].done(): + return await listener['filters'](client, update) if callable(listener['filters']) else True + + return ( + await self.filters(client, update) + if callable(self.filters) + else True + ) + + @patch(pyrogram.types.user_and_chats.chat.Chat) class Chat(pyrogram.types.Chat): @patchable diff --git a/pyromod/utils/errors.py b/pyromod/utils/errors.py index 5117ee8..b6dd849 100644 --- a/pyromod/utils/errors.py +++ b/pyromod/utils/errors.py @@ -17,3 +17,14 @@ class TimeoutConversationError(Exception): super().__init__( "Response read timed out" ) + + +class ListenerCanceled(Exception): + """ + Occurs when the listener is canceled. + """ + + def __init__(self): + super().__init__( + "Listener was canceled" + )