bot_pm.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # ©️ Dan Gazizullin, 2021-2023
  2. # This file is a part of Hikka Userbot
  3. # 🌐 https://github.com/hikariatama/Hikka
  4. # You can redistribute it and/or modify it under the terms of the GNU AGPLv3
  5. # 🔑 https://www.gnu.org/licenses/agpl-3.0.html
  6. import logging
  7. import typing
  8. from .types import InlineUnit
  9. logger = logging.getLogger(__name__)
  10. class BotPM(InlineUnit):
  11. def set_fsm_state(
  12. self,
  13. user: typing.Union[str, int],
  14. state: typing.Union[str, bool],
  15. ) -> bool:
  16. """
  17. Set FSM state for user
  18. :param user: user id
  19. :param state: state to set
  20. :return: True if state was set, False otherwise
  21. :rtype: bool
  22. """
  23. if not isinstance(user, (str, int)):
  24. logger.error(
  25. (
  26. "Invalid type for `user` in `set_fsm_state`. Expected `str` or"
  27. " `int`, got %s"
  28. ),
  29. type(user),
  30. )
  31. return False
  32. if not isinstance(state, (str, bool)):
  33. logger.error(
  34. (
  35. "Invalid type for `state` in `set_fsm_state`. Expected `str` or"
  36. " `bool`, got %s"
  37. ),
  38. type(state),
  39. )
  40. return False
  41. if state:
  42. self.fsm[str(user)] = state
  43. elif str(user) in self.fsm:
  44. del self.fsm[str(user)]
  45. return True
  46. ss = set_fsm_state
  47. def get_fsm_state(self, user: typing.Union[str, int]) -> typing.Union[bool, str]:
  48. """
  49. Get FSM state for user
  50. :param user: user id
  51. :return: FSM state or False if user has no FSM state
  52. :rtype: typing.Union[bool, str]
  53. """
  54. if not isinstance(user, (str, int)):
  55. logger.error(
  56. (
  57. "Invalid type for `user` in `get_fsm_state`. Expected `str` or"
  58. " `int`, got %s"
  59. ),
  60. type(user),
  61. )
  62. return False
  63. return self.fsm.get(str(user), False)
  64. gs = get_fsm_state