types.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. from dataclasses import dataclass, field
  2. from datetime import datetime
  3. from collections.abc import Callable, Coroutine, Container
  4. from typing import Union, Any, Optional
  5. from aiosqlite.core import Connection
  6. @dataclass
  7. class Attachment:
  8. name: str
  9. body: Union[str, bytes]
  10. @dataclass
  11. class Message:
  12. origin_id: str
  13. channel_id: str
  14. sender_id: str
  15. timestamp: datetime
  16. origin_admin: bool
  17. channel_admin: bool
  18. text: Optional[str]
  19. attachments: list[Attachment]
  20. @dataclass
  21. class Response:
  22. origin_id: str
  23. channel_id: str
  24. text: Optional[str]
  25. attachments: list[Attachment]
  26. @staticmethod
  27. def from_message(msg: Message, text: str, attachments: list[Attachment] = None) -> "Response":
  28. return Response(
  29. origin_id=msg.origin_id,
  30. channel_id=msg.channel_id,
  31. text=text,
  32. attachments=attachments or [],
  33. )
  34. @dataclass
  35. class Context:
  36. config: Callable[[str], Any]
  37. respond: Callable[[], Coroutine[None, None, None]]
  38. database: Callable[[], Coroutine[None, None, Connection]]
  39. CommandType = Callable[[Message, 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] = ("!",)
  46. startup: list[Callable[[Context], Coroutine[None, None, None]]] = field(default_factory=list)
  47. shutdown: list[Callable[[Context], Coroutine[None, None, None]]] = field(default_factory=list)