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.

65 lines
1.5KB

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