context.py 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # encoding:utf-8
  2. from enum import Enum
  3. class ContextType (Enum):
  4. TEXT = 1 # 文本消息
  5. VOICE = 2 # 音频消息
  6. IMAGE_CREATE = 3 # 创建图片命令
  7. def __str__(self):
  8. return self.name
  9. class Context:
  10. def __init__(self, type : ContextType = None , content = None, kwargs = dict()):
  11. self.type = type
  12. self.content = content
  13. self.kwargs = kwargs
  14. def __getitem__(self, key):
  15. if key == 'type':
  16. return self.type
  17. elif key == 'content':
  18. return self.content
  19. else:
  20. return self.kwargs[key]
  21. def __setitem__(self, key, value):
  22. if key == 'type':
  23. self.type = value
  24. elif key == 'content':
  25. self.content = value
  26. else:
  27. self.kwargs[key] = value
  28. def __delitem__(self, key):
  29. if key == 'type':
  30. self.type = None
  31. elif key == 'content':
  32. self.content = None
  33. else:
  34. del self.kwargs[key]
  35. def __str__(self):
  36. return "Context(type={}, content={}, kwargs={})".format(self.type, self.content, self.kwargs)