context.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. ACCEPT_FRIEND = 19 # 同意好友请求
  12. JOIN_GROUP = 20 # 加入群聊
  13. PATPAT = 21 # 拍了拍
  14. FUNCTION = 22 # 函数调用
  15. EXIT_GROUP = 23 #退出
  16. def __str__(self):
  17. return self.name
  18. class Context:
  19. def __init__(self, type: ContextType = None, content=None, kwargs=dict()):
  20. self.type = type
  21. self.content = content
  22. self.kwargs = kwargs
  23. def __contains__(self, key):
  24. if key == "type":
  25. return self.type is not None
  26. elif key == "content":
  27. return self.content is not None
  28. else:
  29. return key in self.kwargs
  30. def __getitem__(self, key):
  31. if key == "type":
  32. return self.type
  33. elif key == "content":
  34. return self.content
  35. else:
  36. return self.kwargs[key]
  37. def get(self, key, default=None):
  38. try:
  39. return self[key]
  40. except KeyError:
  41. return default
  42. def __setitem__(self, key, value):
  43. if key == "type":
  44. self.type = value
  45. elif key == "content":
  46. self.content = value
  47. else:
  48. self.kwargs[key] = value
  49. def __delitem__(self, key):
  50. if key == "type":
  51. self.type = None
  52. elif key == "content":
  53. self.content = None
  54. else:
  55. del self.kwargs[key]
  56. def __str__(self):
  57. return "Context(type={}, content={}, kwargs={})".format(self.type, self.content, self.kwargs)