From 30fcc0be499895c1335edb647b3cb355d8e73b72 Mon Sep 17 00:00:00 2001 From: MrMissx Date: Tue, 20 Feb 2024 19:07:17 +0700 Subject: [PATCH] chore: add more alert information --- anjani/core/command_dispatcher.py | 5 +++-- anjani/core/event_dispatcher.py | 31 ++++++++++++++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/anjani/core/command_dispatcher.py b/anjani/core/command_dispatcher.py index 91c4d33f5..dd481f438 100644 --- a/anjani/core/command_dispatcher.py +++ b/anjani/core/command_dispatcher.py @@ -1,4 +1,5 @@ """Anjani command dispatcher""" + # Copyright (C) 2020 - 2023 UserbotIndo Team, # # This program is free software: you can redistribute it and/or modify @@ -229,7 +230,7 @@ async def on_command( exc_info=constructor_invoke, ) await self.dispatch_alert( - f"command `/{' '.join(message.command)}`", constructor_invoke + f"command `/{' '.join(message.command)}`", constructor_invoke, chat.id ) await self.dispatch_event("command", ctx, cmd) @@ -254,7 +255,7 @@ async def on_command( exc_info=constructor_handler, ) await self.dispatch_alert( - f"command `/{' '.join(message.command)}`", constructor_handler + f"command `/{' '.join(message.command)}`", constructor_handler, chat.id ) finally: # Continue processing handler of on_message diff --git a/anjani/core/event_dispatcher.py b/anjani/core/event_dispatcher.py index 7a0592990..d8b0f0fdf 100644 --- a/anjani/core/event_dispatcher.py +++ b/anjani/core/event_dispatcher.py @@ -1,4 +1,5 @@ """Anjani event dispatcher""" + # Copyright (C) 2020 - 2023 UserbotIndo Team, # # This program is free software: you can redistribute it and/or modify @@ -195,9 +196,6 @@ async def dispatch_event( dispatcher_error = EventDispatchError( f"raised from {type(err).__name__}: {str(err)}" ).with_traceback(err.__traceback__) - await self.dispatch_alert( - f"Event __{event}__ on `{lst.func.__qualname__}`", dispatcher_error - ) if is_tg_event and args[0] is not None: data = _get_event_data(args[0]) self.log.error( @@ -215,6 +213,11 @@ async def dispatch_event( data.get("input"), exc_info=dispatcher_error, ) + await self.dispatch_alert( + f"Event __{event}__ on `{lst.func.__qualname__}`", + dispatcher_error, + data.get("chat_id"), + ) else: self.log.error( "Error dispatching event '%s' on %s with data\n%s", @@ -223,6 +226,10 @@ async def dispatch_event( _unpack_args(args), exc_info=dispatcher_error, ) + await self.dispatch_alert( + f"Event __{event}__ on `{lst.func.__qualname__}`", + dispatcher_error, + ) continue finally: if result: @@ -329,20 +336,26 @@ async def send_missed_update( {"$unset": {"pts": "", "date": "", "qts": "", "seq": ""}}, ) - async def dispatch_alert(self: "Anjani", invoker: str, exc: BaseException) -> None: + async def dispatch_alert( + self: "Anjani", + invoker: str, + exc: BaseException, + chat_id: Optional[int] = None, + ) -> None: """Dispatches an alert to the configured alert log.""" if not self.config.ALERT_LOG: return log_chat = self.config.ALERT_LOG.split("#") - thread_id = None - chat_id = int(log_chat[0]) + log_thread_id = None + log_chat_id = int(log_chat[0]) if len(log_chat) == 2: - thread_id = int(log_chat[1]) + log_thread_id = int(log_chat[1]) alert = f"""🔴 **Anjani ERROR ALERT** - **Alert by:** {invoker} + - **Chat ID:** {chat_id} - **Time (UTC):** {datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S")} **ERROR** @@ -351,9 +364,9 @@ async def dispatch_alert(self: "Anjani", invoker: str, exc: BaseException) -> No ``` """ await self.client.send_message( - chat_id, + log_chat_id, alert, - message_thread_id=thread_id, # type: ignore + message_thread_id=log_thread_id, # type: ignore ) async def log_stat(self: "Anjani", stat: str, *, value: int = 1) -> None: