bot_pm.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  2. # █▀█ █ █ █ █▀█ █▀▄ █
  3. # © Copyright 2022
  4. # https://t.me/hikariatama
  5. #
  6. # 🔒 Licensed under the GNU AGPLv3
  7. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  8. import logging
  9. import typing
  10. from .types import InlineUnit
  11. logger = logging.getLogger(__name__)
  12. class BotPM(InlineUnit):
  13. def set_fsm_state(
  14. self,
  15. user: typing.Union[str, int],
  16. state: typing.Union[str, bool],
  17. ) -> bool:
  18. if not isinstance(user, (str, int)):
  19. logger.error(
  20. "Invalid type for `user` in `set_fsm_state`. Expected `str` or `int`,"
  21. " got %s",
  22. type(user),
  23. )
  24. return False
  25. if not isinstance(state, (str, bool)):
  26. logger.error(
  27. "Invalid type for `state` in `set_fsm_state`. Expected `str` or `bool`,"
  28. " got %s",
  29. type(state),
  30. )
  31. return False
  32. if state:
  33. self.fsm[str(user)] = state
  34. elif str(user) in self.fsm:
  35. del self.fsm[str(user)]
  36. return True
  37. ss = set_fsm_state
  38. def get_fsm_state(self, user: typing.Union[str, int]) -> typing.Union[bool, str]:
  39. if not isinstance(user, (str, int)):
  40. logger.error(
  41. "Invalid type for `user` in `get_fsm_state`. Expected `str` or `int`,"
  42. " got %s",
  43. type(user),
  44. )
  45. return False
  46. return self.fsm.get(str(user), False)
  47. gs = get_fsm_state