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.

99 satır
4.4KB

  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. return
  39. e_context["context"].type = ContextType.TEXT
  40. msg: ChatMessage = e_context["context"]["msg"]
  41. e_context["context"].content = f'请你随机使用一种风格说一句问候语来欢迎新用户"{msg.actual_user_nickname}"加入群聊。'
  42. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
  43. if not self.config or not self.config.get("use_character_desc"):
  44. e_context["context"]["generate_breaked_by"] = EventAction.BREAK
  45. return
  46. if e_context["context"].type == ContextType.EXIT_GROUP:
  47. if conf().get("group_chat_exit_group"):
  48. e_context["context"].type = ContextType.TEXT
  49. msg: ChatMessage = e_context["context"]["msg"]
  50. e_context["context"].content = f'请你随机使用一种风格跟其他群用户说他违反规则"{msg.actual_user_nickname}"退出群聊。'
  51. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑
  52. return
  53. e_context.action = EventAction.BREAK
  54. return
  55. if e_context["context"].type == ContextType.PATPAT:
  56. e_context["context"].type = ContextType.TEXT
  57. msg: ChatMessage = e_context["context"]["msg"]
  58. e_context["context"].content = f"请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。"
  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. content = e_context["context"].content
  64. logger.debug("[Hello] on_handle_context. content: %s" % content)
  65. if content == "Hello":
  66. reply = Reply()
  67. reply.type = ReplyType.TEXT
  68. msg: ChatMessage = e_context["context"]["msg"]
  69. if e_context["context"]["isgroup"]:
  70. reply.content = f"Hello, {msg.actual_user_nickname} from {msg.from_user_nickname}"
  71. else:
  72. reply.content = f"Hello, {msg.from_user_nickname}"
  73. e_context["reply"] = reply
  74. e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑
  75. if content == "Hi":
  76. reply = Reply()
  77. reply.type = ReplyType.TEXT
  78. reply.content = "Hi"
  79. e_context["reply"] = reply
  80. e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑,一般会覆写reply
  81. if content == "End":
  82. # 如果是文本消息"End",将请求转换成"IMAGE_CREATE",并将content设置为"The World"
  83. e_context["context"].type = ContextType.IMAGE_CREATE
  84. content = "The World"
  85. e_context.action = EventAction.CONTINUE # 事件继续,交付给下个插件或默认逻辑
  86. def get_help_text(self, **kwargs):
  87. help_text = "输入Hello,我会回复你的名字\n输入End,我会回复你世界的图片\n"
  88. return help_text