Skip to content

Commit

Permalink
chore: add more alert information
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMissx committed Feb 20, 2024
1 parent b6a52b1 commit 30fcc0b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
5 changes: 3 additions & 2 deletions anjani/core/command_dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Anjani command dispatcher"""

# Copyright (C) 2020 - 2023 UserbotIndo Team, <https://github.com/userbotindo.git>
#
# This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
31 changes: 22 additions & 9 deletions anjani/core/event_dispatcher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Anjani event dispatcher"""

# Copyright (C) 2020 - 2023 UserbotIndo Team, <https://github.com/userbotindo.git>
#
# This program is free software: you can redistribute it and/or modify
Expand Down Expand Up @@ -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(
Expand All @@ -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",
Expand All @@ -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:
Expand Down Expand Up @@ -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**
Expand All @@ -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:
Expand Down

0 comments on commit 30fcc0b

Please sign in to comment.