You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.6KB

  1. # encoding:utf-8
  2. from enum import Enum
  3. class ContextType(Enum):
  4. TEXT = 1 # 文本消息
  5. VOICE = 2 # 音频消息
  6. IMAGE = 3 # 图片消息
  7. IMAGE_CREATE = 10 # 创建图片命令
  8. JOIN_GROUP = 20 # 加入群聊
  9. PATPAT = 21 # 拍了拍
  10. def __str__(self):
  11. return self.name
  12. class Context:
  13. def __init__(self, type: ContextType = None, content=None, kwargs=dict()):
  14. self.type = type
  15. self.content = content
  16. self.kwargs = kwargs
  17. def __contains__(self, key):
  18. if key == "type":
  19. return self.type is not None
  20. elif key == "content":
  21. return self.content is not None
  22. else:
  23. return key in self.kwargs
  24. def __getitem__(self, key):
  25. if key == "type":
  26. return self.type
  27. elif key == "content":
  28. return self.content
  29. else:
  30. return self.kwargs[key]
  31. def get(self, key, default=None):
  32. try:
  33. return self[key]
  34. except KeyError:
  35. return default
  36. def __setitem__(self, key, value):
  37. if key == "type":
  38. self.type = value
  39. elif key == "content":
  40. self.content = value
  41. else:
  42. self.kwargs[key] = value
  43. def __delitem__(self, key):
  44. if key == "type":
  45. self.type = None
  46. elif key == "content":
  47. self.content = None
  48. else:
  49. del self.kwargs[key]
  50. def __str__(self):
  51. return "Context(type={}, content={}, kwargs={})".format(
  52. self.type, self.content, self.kwargs
  53. )