From a3ee603cd26c71bfbee23abbd5be7ef9d6b87962 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Fri, 8 Jul 2022 22:26:36 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=96=20Update=20to=20v1.2.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix some bugs --- pagermaid/__init__.py | 4 ++-- pagermaid/hook.py | 22 ++++++++++++++++++++++ pagermaid/listener.py | 1 + pagermaid/modules/message.py | 11 +++++++++-- pagermaid/modules/system.py | 4 ++-- 5 files changed, 36 insertions(+), 6 deletions(-) diff --git a/pagermaid/__init__.py b/pagermaid/__init__.py index c9429d8..382eb67 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.2.0" +pgm_version = "1.2.1" CMD_LIST = {} module_dir = __path__[0] working_dir = getcwd() @@ -22,7 +22,7 @@ working_dir = getcwd() read_context = {} help_messages = {} hook_functions: Dict[str, Set[Callable[[], Awaitable[None]]]] = { - "startup": set(), "shutdown": set(), "command_pre": set(), "command_post": set()} + "startup": set(), "shutdown": set(), "command_pre": set(), "command_post": set(), "process_error": set(), } all_permissions = [] logs = getLogger(__name__) diff --git a/pagermaid/hook.py b/pagermaid/hook.py index fe46206..9806655 100644 --- a/pagermaid/hook.py +++ b/pagermaid/hook.py @@ -51,6 +51,18 @@ class Hook: return decorator + @staticmethod + def process_error(): + """ + 注册一个错误处理钩子 + """ + + def decorator(function): + hook_functions["process_error"].add(function) + return function + + return decorator + @staticmethod async def startup(): if cors := [startup() for startup in hook_functions["startup"]]: @@ -86,3 +98,13 @@ class Hook: raise StopPropagation from e except Exception as exception: logs.info(f"[command_post]: {type(exception)}: {exception}") + + @staticmethod + async def process_error_exec(message: Message, exc_info: BaseException, exc_format: str): + if cors := [error(message, exc_info, exc_format) for error in hook_functions["process_error"]]: # noqa + try: + await asyncio.gather(*cors) + except StopPropagation as e: + raise StopPropagation from e + except Exception as exception: + logs.info(f"[process_error]: {type(exception)}: {exception}") diff --git a/pagermaid/listener.py b/pagermaid/listener.py index 09e0c75..181c702 100644 --- a/pagermaid/listener.py +++ b/pagermaid/listener.py @@ -190,6 +190,7 @@ def listener(**args): await message.edit(lang("run_error"), no_reply=True) # noqa if not diagnostics: return + await Hook.process_error_exec(message, exc_info, exc_format) if Config.ERROR_REPORT: report = f"""# Generated: {strftime('%H:%M %d/%m/%Y', gmtime())}. \n# ChatID: {message.chat.id}. \n# UserID: {message.from_user.id if message.from_user else message.sender_chat.id}. \n# Message: \n-----BEGIN TARGET MESSAGE-----\n{message.text or message.caption}\n-----END TARGET MESSAGE-----\n# Traceback: \n-----BEGIN TRACEBACK-----\n{str(exc_format)}\n-----END TRACEBACK-----\n# Error: "{str(exc_info)}". \n""" diff --git a/pagermaid/modules/message.py b/pagermaid/modules/message.py index 49ec3a5..f1455a0 100644 --- a/pagermaid/modules/message.py +++ b/pagermaid/modules/message.py @@ -34,9 +34,9 @@ async def userid(message: Message): return await message.edit(lang("leave_not_group")) text += f"protected: `{str(msg_from.has_protected_content)}" + "`\n" if reply: - text += "\n" + lang('id_hint') + "\nMessage ID: `" + str(reply.id) + \ - "`\n\n**User**\nid: `" + str(reply.from_user.id) + "`" + text += "\n" + lang('id_hint') + "\nMessage ID: `" + str(reply.id) + "`" try: + text += "\n\n**User**\nid: `" + str(reply.from_user.id) + "`" if reply.from_user.is_bot: text += f"\nis_bot: {lang('id_is_bot_yes')}" try: @@ -51,6 +51,13 @@ async def userid(message: Message): text += "\ndc: `" + str(reply.from_user.dc_id) + "`" except AttributeError: pass + try: + text += "\n\n**Chat**\nid: `" + str(reply.sender_chat.id) + "`" + text += "\ntitle: `" + reply.sender_chat.title + "`" + if reply.sender_chat.username: + text += "\nusername: @" + reply.sender_chat.username + except AttributeError: + pass if reply.forward_from_chat: text += "\n\n**Forward From Channel**\n" \ "id: `" + str(message.forward_from_chat.id) + \ diff --git a/pagermaid/modules/system.py b/pagermaid/modules/system.py index 36f7bb8..7229523 100644 --- a/pagermaid/modules/system.py +++ b/pagermaid/modules/system.py @@ -34,7 +34,7 @@ async def sh(message: Message): if result: if len(result) > 4096: - await attach_log(bot, result, message.chat.id, "output.log", message.id) + await attach_log(result, message.chat.id, "output.log", message.id) return await message.edit( @@ -90,7 +90,7 @@ async def sh_eval(message: Message): final_output = f"**>>>** ```{cmd}``` \n```{evaluation}```" if len(final_output) > 4096: message = await message.edit(f"**>>>** ```{cmd}```") - await attach_log(bot, evaluation, message.chat.id, "output.log", message.id) + await attach_log(evaluation, message.chat.id, "output.log", message.id) else: await message.edit(final_output)