選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

127 行
5.8KB

  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. group_welc_prompt = "请你随机使用一种风格说一句问候语来欢迎新用户\"{nickname}\"加入群聊。"
  19. group_exit_prompt = "请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。"
  20. patpat_prompt = "请你随机使用一种风格跟其他群用户说他违反规则\"{nickname}\"退出群聊。"
  21. def __init__(self):
  22. super().__init__()
  23. try:
  24. self.config = super().load_config()
  25. if not self.config:
  26. self.config = self._load_config_template()
  27. self.group_welc_fixed_msg = self.config.get("group_welc_fixed_msg", {})
  28. self.group_welc_prompt = self.config.get("group_welc_prompt", self.group_welc_prompt)
  29. self.group_exit_prompt = self.config.get("group_exit_prompt", self.group_exit_prompt)
  30. self.patpat_prompt = self.config.get("patpat_prompt", self.patpat_prompt)
  31. logger.info("[Hello] inited")
  32. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  33. except Exception as e:
  34. logger.error(f"[Hello]初始化异常:{e}")
  35. raise "[Hello] init failed, ignore "
  36. def on_handle_context(self, e_context: EventContext):
  37. if e_context["context"].type not in [
  38. ContextType.TEXT,
  39. ContextType.JOIN_GROUP,
  40. ContextType.PATPAT,
  41. ContextType.EXIT_GROUP
  42. ]:
  43. return
  44. msg: ChatMessage = e_context["context"]["msg"]
  45. group_name = msg.from_user_nickname
  46. if e_context["context"].type == ContextType.JOIN_GROUP:
  47. if "group_welcome_msg" in conf() or group_name in self.group_welc_fixed_msg:
  48. reply = Reply()
  49. reply.type = ReplyType.TEXT
  50. if group_name in self.group_welc_fixed_msg:
  51. reply.content = self.group_welc_fixed_msg.get(group_name, "")
  52. else:
  53. reply.content = conf().get("group_welcome_msg", "")
  54. e_context["reply"] = reply
  55. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  56. return
  57. e_context["context"].type = ContextType.TEXT
  58. e_context["context"].content = self.group_welc_prompt.format(nickname=msg.actual_user_nickname)
  59. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
  60. if not self.config or not self.config.get("use_character_desc"):
  61. e_context["context"]["generate_breaked_by"] = EventAction.BREAK
  62. return
  63. if e_context["context"].type == ContextType.EXIT_GROUP:
  64. if conf().get("group_chat_exit_group"):
  65. e_context["context"].type = ContextType.TEXT
  66. e_context["context"].content = self.group_exit_prompt.format(nickname=msg.actual_user_nickname)
  67. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
  68. return
  69. e_context.action = EventAction.BREAK
  70. return
  71. if e_context["context"].type == ContextType.PATPAT:
  72. e_context["context"].type = ContextType.TEXT
  73. e_context["context"].content = self.patpat_prompt
  74. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
  75. if not self.config or not self.config.get("use_character_desc"):
  76. e_context["context"]["generate_breaked_by"] = EventAction.BREAK
  77. return
  78. content = e_context["context"].content
  79. logger.debug("[Hello] on_handle_context. content: %s" % content)
  80. if content == "Hello":
  81. reply = Reply()
  82. reply.type = ReplyType.TEXT
  83. if e_context["context"]["isgroup"]:
  84. reply.content = f"Hello, {msg.actual_user_nickname} from {msg.from_user_nickname}"
  85. else:
  86. reply.content = f"Hello, {msg.from_user_nickname}"
  87. e_context["reply"] = reply
  88. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  89. if content == "Hi":
  90. reply = Reply()
  91. reply.type = ReplyType.TEXT
  92. reply.content = "Hi"
  93. e_context["reply"] = reply
  94. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑,一般会覆写reply
  95. if content == "End":
  96. # 如果是文本消息"End",将请求转换成"IMAGE_CREATE",并将content设置为"The World"
  97. e_context["context"].type = ContextType.IMAGE_CREATE
  98. content = "The World"
  99. e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
  100. def get_help_text(self, **kwargs):
  101. help_text = "输入Hello,我会回复你的名字\n输入End,我会回复你世界的图片\n"
  102. return help_text
  103. def _load_config_template(self):
  104. logger.debug("No Hello plugin config.json, use plugins/hello/config.json.template")
  105. try:
  106. plugin_config_path = os.path.join(self.path, "config.json.template")
  107. if os.path.exists(plugin_config_path):
  108. with open(plugin_config_path, "r", encoding="utf-8") as f:
  109. plugin_conf = json.load(f)
  110. return plugin_conf
  111. except Exception as e:
  112. logger.exception(e)