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.

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