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.

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