Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

context.py 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. def __str__(self):
  16. return self.name
  17. class Context:
  18. def __init__(self, type: ContextType = None, content=None, kwargs=dict()):
  19. self.type = type
  20. self.content = content
  21. self.kwargs = kwargs
  22. def __contains__(self, key):
  23. if key == "type":
  24. return self.type is not None
  25. elif key == "content":
  26. return self.content is not None
  27. else:
  28. return key in self.kwargs
  29. def __getitem__(self, key):
  30. if key == "type":
  31. return self.type
  32. elif key == "content":
  33. return self.content
  34. else:
  35. return self.kwargs[key]
  36. def get(self, key, default=None):
  37. try:
  38. return self[key]
  39. except KeyError:
  40. return default
  41. def __setitem__(self, key, value):
  42. if key == "type":
  43. self.type = value
  44. elif key == "content":
  45. self.content = value
  46. else:
  47. self.kwargs[key] = value
  48. def __delitem__(self, key):
  49. if key == "type":
  50. self.type = None
  51. elif key == "content":
  52. self.content = None
  53. else:
  54. del self.kwargs[key]
  55. def __str__(self):
  56. return "Context(type={}, content={}, kwargs={})".format(self.type, self.content, self.kwargs)