types.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from dataclasses import dataclass
  2. from datetime import datetime
  3. from collections.abc import Callable, Coroutine, Container
  4. from typing import Union, Any, Optional
  5. @dataclass
  6. class Attachment:
  7. name: str
  8. body: Union[str, bytes]
  9. @dataclass
  10. class Message:
  11. origin_id: str
  12. channel_id: str
  13. sender_id: str
  14. timestamp: datetime
  15. origin_admin: bool
  16. channel_admin: bool
  17. text: Optional[str]
  18. attachments: list[Attachment]
  19. @dataclass
  20. class Response:
  21. origin_id: str
  22. channel_id: str
  23. text: str
  24. attachments: list[Attachment]
  25. @staticmethod
  26. def from_message(msg: Message, text: str, attachments: list[Attachment] = None) -> "Response":
  27. return Response(
  28. origin_id=msg.origin_id,
  29. channel_id=msg.channel_id,
  30. text=text,
  31. attachments=attachments or [],
  32. )
  33. @dataclass
  34. class Context:
  35. message: Message
  36. config: Callable[[str], Any]
  37. respond: Callable[[], Coroutine[None, None, None]]
  38. # database: Callable # TODO proper type
  39. CommandType = Callable[[Context], Coroutine[None, None, None]]
  40. @dataclass
  41. class CommandConfiguration:
  42. commands: dict[str, CommandType]
  43. call_and_response: dict[str, str]
  44. aliases: dict[str, str]
  45. bangs: Container[str] = ("!",)