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.

69 lines
1.7KB

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