Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

96 lines
4.3KB

  1. # encoding:utf-8
  2. import plugins
  3. from bridge.context import ContextType
  4. from bridge.reply import Reply, ReplyType
  5. from channel.chat_message import ChatMessage
  6. from common.log import logger
  7. from plugins import *
  8. from config import conf
  9. @plugins.register(
  10. name="Hello",
  11. desire_priority=-1,
  12. hidden=True,
  13. desc="A simple plugin that says hello",
  14. version="0.1",
  15. author="lanvent",
  16. )
  17. class Hello(Plugin):
  18. def __init__(self):
  19. super().__init__()
  20. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  21. logger.info("[Hello] inited")
  22. self.config = super().load_config()
  23. def on_handle_context(self, e_context: EventContext):
  24. if e_context["context"].type not in [
  25. ContextType.TEXT,
  26. ContextType.JOIN_GROUP,
  27. ContextType.PATPAT,
  28. ContextType.EXIT_GROUP
  29. ]:
  30. return
  31. if e_context["context"].type == ContextType.JOIN_GROUP:
  32. if "group_welcome_msg" in conf():
  33. reply = Reply()
  34. reply.type = ReplyType.TEXT
  35. reply.content = conf().get("group_welcome_msg", "")
  36. e_context["reply"] = reply
  37. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  38. if not self.config or not self.config.get("use_character_desc"):
  39. e_context["context"]["generate_breaked_by"] = EventAction.BREAK_PASS
  40. return
  41. e_context["context"].type = ContextType.TEXT
  42. msg: ChatMessage = e_context["context"]["msg"]
  43. e_context["context"].content = f'请你随机使用一种风格说一句问候语来欢迎新用户"{msg.actual_user_nickname}"加入群聊。'
  44. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
  45. return
  46. if e_context["context"].type == ContextType.EXIT_GROUP:
  47. e_context["context"].type = ContextType.TEXT
  48. msg: ChatMessage = e_context["context"]["msg"]
  49. e_context["context"].content = f'请你随机使用一种风格跟其他群用户说他违反规则"{msg.actual_user_nickname}"退出群聊。'
  50. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
  51. return
  52. if e_context["context"].type == ContextType.PATPAT:
  53. e_context["context"].type = ContextType.TEXT
  54. msg: ChatMessage = e_context["context"]["msg"]
  55. e_context["context"].content = f"请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。"
  56. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
  57. if not self.config or not self.config.get("use_character_desc"):
  58. e_context["context"]["generate_breaked_by"] = EventAction.BREAK
  59. return
  60. content = e_context["context"].content
  61. logger.debug("[Hello] on_handle_context. content: %s" % content)
  62. if content == "Hello":
  63. reply = Reply()
  64. reply.type = ReplyType.TEXT
  65. msg: ChatMessage = e_context["context"]["msg"]
  66. if e_context["context"]["isgroup"]:
  67. reply.content = f"Hello, {msg.actual_user_nickname} from {msg.from_user_nickname}"
  68. else:
  69. reply.content = f"Hello, {msg.from_user_nickname}"
  70. e_context["reply"] = reply
  71. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  72. if content == "Hi":
  73. reply = Reply()
  74. reply.type = ReplyType.TEXT
  75. reply.content = "Hi"
  76. e_context["reply"] = reply
  77. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑,一般会覆写reply
  78. if content == "End":
  79. # 如果是文本消息"End",将请求转换成"IMAGE_CREATE",并将content设置为"The World"
  80. e_context["context"].type = ContextType.IMAGE_CREATE
  81. content = "The World"
  82. e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
  83. def get_help_text(self, **kwargs):
  84. help_text = "输入Hello,我会回复你的名字\n输入End,我会回复你世界的图片\n"
  85. return help_text