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.

57 satır
1.5KB

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