Sfoglia il codice sorgente

Add sender name and debugging information

Kirk Trombley 4 anni fa
parent
commit
2e76981356
3 ha cambiato i file con 25 aggiunte e 2 eliminazioni
  1. 13 1
      lib/rollbot/bot.py
  2. 5 1
      lib/rollbot/injection/util.py
  3. 7 0
      lib/rollbot/types.py

+ 13 - 1
lib/rollbot/bot.py

@@ -1,5 +1,6 @@
 from dataclasses import dataclass
 from typing import Any, Generic, TypeVar
+import traceback
 import asyncio
 import logging
 
@@ -78,4 +79,15 @@ class Rollbot(Generic[RawMsg]):
             )
             return
 
-        await command_call(message, self.context)
+        try:
+            await command_call(message, self.context)
+        except Exception as e:
+            self.context.logger.exception(
+                f"Unhandled error during execution of {command} in {message}"
+            )
+            await self.respond(
+                Response.from_message(
+                    message, f"Encountered an unhandled error: {type(e).__name__}"
+                )
+            )
+            self.context.debugging = "".join(traceback.format_exc())

+ 5 - 1
lib/rollbot/injection/util.py

@@ -1,5 +1,5 @@
 from collections.abc import Callable, Coroutine
-from typing import TypeVar, Any
+from typing import TypeVar, Any, Optional
 from datetime import datetime
 from logging import Logger
 
@@ -17,12 +17,14 @@ __all__ = [
     "Timestamp",
     "OriginAdmin",
     "ChannelAdmin",
+    "SenderName",
     "Text",
     "Attachments",
     "CommandInjector",
     "Request",
     "Respond",
     "LoggerInjector",
+    "Debugging",
     "Config",
     "Lazy",
 ]
@@ -35,12 +37,14 @@ Sender = Simple[str](lambda m, c: m.sender_id)
 Timestamp = Simple[datetime](lambda m, c: m.timestamp)
 OriginAdmin = Simple[bool](lambda m, c: m.origin_admin)
 ChannelAdmin = Simple[bool](lambda m, c: m.channel_admin)
+SenderName = Simple[Optional[str]](lambda m, c: m.sender_name)
 Text = Simple[str](lambda m, c: m.text)
 Attachments = Simple[list[Attachment]](lambda m, c: m.attachments)
 CommandInjector = Simple[Command](lambda m, c: m.command)
 Request = Simple[ClientSession](lambda m, c: c.request)
 Respond = Simple[Callable[[], Coroutine[None, None, None]]](lambda m, c: c.respond)
 LoggerInjector = Simple[Logger](lambda m, c: c.logger)
+Debugging = Simple[Optional[str]](lambda m, c: c.get_debugging())
 
 
 class Config(Injector[Any]):

+ 7 - 0
lib/rollbot/types.py

@@ -35,6 +35,7 @@ class Message:
     timestamp: datetime
     origin_admin: bool
     channel_admin: bool
+    sender_name: Optional[str] = None
     text: Optional[str] = None
     attachments: list[Attachment] = field(default_factory=list)
 
@@ -106,6 +107,12 @@ class Context:
     request: ClientSession
     database: Callable[[], Coroutine[None, None, Connection]]
     logger: Logger
+    debugging: Optional[str] = None
+
+    def get_debugging(self) -> Optional[str]:
+        old = self.debugging
+        self.debugging = None
+        return old
 
 
 CommandType = Callable[[Message, Context], Coroutine[None, None, None]]