|
@@ -17,12 +17,29 @@ from config import conf |
|
|
version="0.1", |
|
|
version="0.1", |
|
|
author="lanvent", |
|
|
author="lanvent", |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Hello(Plugin): |
|
|
class Hello(Plugin): |
|
|
|
|
|
|
|
|
|
|
|
group_welc_prompt = "请你随机使用一种风格说一句问候语来欢迎新用户\"{nickname}\"加入群聊。" |
|
|
|
|
|
group_exit_prompt = "请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。" |
|
|
|
|
|
patpat_prompt = "请你随机使用一种风格跟其他群用户说他违反规则\"{nickname}\"退出群聊。" |
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
def __init__(self): |
|
|
super().__init__() |
|
|
super().__init__() |
|
|
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context |
|
|
|
|
|
logger.info("[Hello] inited") |
|
|
|
|
|
self.config = super().load_config() |
|
|
|
|
|
|
|
|
try: |
|
|
|
|
|
self.config = super().load_config() |
|
|
|
|
|
if not self.config: |
|
|
|
|
|
self.config = self._load_config_template() |
|
|
|
|
|
self.group_welc_fixed_msg = self.config.get("group_welc_fixed_msg", {}) |
|
|
|
|
|
self.group_welc_prompt = self.config.get("group_welc_prompt", self.group_welc_prompt) |
|
|
|
|
|
self.group_exit_prompt = self.config.get("group_exit_prompt", self.group_exit_prompt) |
|
|
|
|
|
self.patpat_prompt = self.config.get("patpat_prompt", self.patpat_prompt) |
|
|
|
|
|
logger.info("[Hello] inited") |
|
|
|
|
|
self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
logger.error(f"[Hello]初始化异常:{e}") |
|
|
|
|
|
raise "[Hello] init failed, ignore " |
|
|
|
|
|
|
|
|
def on_handle_context(self, e_context: EventContext): |
|
|
def on_handle_context(self, e_context: EventContext): |
|
|
if e_context["context"].type not in [ |
|
|
if e_context["context"].type not in [ |
|
@@ -32,17 +49,21 @@ class Hello(Plugin): |
|
|
ContextType.EXIT_GROUP |
|
|
ContextType.EXIT_GROUP |
|
|
]: |
|
|
]: |
|
|
return |
|
|
return |
|
|
|
|
|
msg: ChatMessage = e_context["context"]["msg"] |
|
|
|
|
|
group_name = msg.from_user_nickname |
|
|
if e_context["context"].type == ContextType.JOIN_GROUP: |
|
|
if e_context["context"].type == ContextType.JOIN_GROUP: |
|
|
if "group_welcome_msg" in conf(): |
|
|
|
|
|
|
|
|
if "group_welcome_msg" in conf() or group_name in self.group_welc_fixed_msg: |
|
|
reply = Reply() |
|
|
reply = Reply() |
|
|
reply.type = ReplyType.TEXT |
|
|
reply.type = ReplyType.TEXT |
|
|
reply.content = conf().get("group_welcome_msg", "") |
|
|
|
|
|
|
|
|
if group_name in self.group_welc_fixed_msg: |
|
|
|
|
|
reply.content = self.group_welc_fixed_msg.get(group_name, "") |
|
|
|
|
|
else: |
|
|
|
|
|
reply.content = conf().get("group_welcome_msg", "") |
|
|
e_context["reply"] = reply |
|
|
e_context["reply"] = reply |
|
|
e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑 |
|
|
e_context.action = EventAction.BREAK_PASS # 事件结束,并跳过处理context的默认逻辑 |
|
|
return |
|
|
return |
|
|
e_context["context"].type = ContextType.TEXT |
|
|
e_context["context"].type = ContextType.TEXT |
|
|
msg: ChatMessage = e_context["context"]["msg"] |
|
|
|
|
|
e_context["context"].content = f'请你随机使用一种风格说一句问候语来欢迎新用户"{msg.actual_user_nickname}"加入群聊。' |
|
|
|
|
|
|
|
|
e_context["context"].content = self.group_welc_prompt.format(nickname=msg.actual_user_nickname) |
|
|
e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 |
|
|
e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 |
|
|
if not self.config or not self.config.get("use_character_desc"): |
|
|
if not self.config or not self.config.get("use_character_desc"): |
|
|
e_context["context"]["generate_breaked_by"] = EventAction.BREAK |
|
|
e_context["context"]["generate_breaked_by"] = EventAction.BREAK |
|
@@ -51,8 +72,7 @@ class Hello(Plugin): |
|
|
if e_context["context"].type == ContextType.EXIT_GROUP: |
|
|
if e_context["context"].type == ContextType.EXIT_GROUP: |
|
|
if conf().get("group_chat_exit_group"): |
|
|
if conf().get("group_chat_exit_group"): |
|
|
e_context["context"].type = ContextType.TEXT |
|
|
e_context["context"].type = ContextType.TEXT |
|
|
msg: ChatMessage = e_context["context"]["msg"] |
|
|
|
|
|
e_context["context"].content = f'请你随机使用一种风格跟其他群用户说他违反规则"{msg.actual_user_nickname}"退出群聊。' |
|
|
|
|
|
|
|
|
e_context["context"].content = self.group_exit_prompt.format(nickname=msg.actual_user_nickname) |
|
|
e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 |
|
|
e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 |
|
|
return |
|
|
return |
|
|
e_context.action = EventAction.BREAK |
|
|
e_context.action = EventAction.BREAK |
|
@@ -60,8 +80,7 @@ class Hello(Plugin): |
|
|
|
|
|
|
|
|
if e_context["context"].type == ContextType.PATPAT: |
|
|
if e_context["context"].type == ContextType.PATPAT: |
|
|
e_context["context"].type = ContextType.TEXT |
|
|
e_context["context"].type = ContextType.TEXT |
|
|
msg: ChatMessage = e_context["context"]["msg"] |
|
|
|
|
|
e_context["context"].content = f"请你随机使用一种风格介绍你自己,并告诉用户输入#help可以查看帮助信息。" |
|
|
|
|
|
|
|
|
e_context["context"].content = self.patpat_prompt |
|
|
e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 |
|
|
e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 |
|
|
if not self.config or not self.config.get("use_character_desc"): |
|
|
if not self.config or not self.config.get("use_character_desc"): |
|
|
e_context["context"]["generate_breaked_by"] = EventAction.BREAK |
|
|
e_context["context"]["generate_breaked_by"] = EventAction.BREAK |
|
@@ -72,7 +91,6 @@ class Hello(Plugin): |
|
|
if content == "Hello": |
|
|
if content == "Hello": |
|
|
reply = Reply() |
|
|
reply = Reply() |
|
|
reply.type = ReplyType.TEXT |
|
|
reply.type = ReplyType.TEXT |
|
|
msg: ChatMessage = e_context["context"]["msg"] |
|
|
|
|
|
if e_context["context"]["isgroup"]: |
|
|
if e_context["context"]["isgroup"]: |
|
|
reply.content = f"Hello, {msg.actual_user_nickname} from {msg.from_user_nickname}" |
|
|
reply.content = f"Hello, {msg.actual_user_nickname} from {msg.from_user_nickname}" |
|
|
else: |
|
|
else: |
|
@@ -96,3 +114,14 @@ class Hello(Plugin): |
|
|
def get_help_text(self, **kwargs): |
|
|
def get_help_text(self, **kwargs): |
|
|
help_text = "输入Hello,我会回复你的名字\n输入End,我会回复你世界的图片\n" |
|
|
help_text = "输入Hello,我会回复你的名字\n输入End,我会回复你世界的图片\n" |
|
|
return help_text |
|
|
return help_text |
|
|
|
|
|
|
|
|
|
|
|
def _load_config_template(self): |
|
|
|
|
|
logger.debug("No Hello plugin config.json, use plugins/hello/config.json.template") |
|
|
|
|
|
try: |
|
|
|
|
|
plugin_config_path = os.path.join(self.path, "config.json.template") |
|
|
|
|
|
if os.path.exists(plugin_config_path): |
|
|
|
|
|
with open(plugin_config_path, "r", encoding="utf-8") as f: |
|
|
|
|
|
plugin_conf = json.load(f) |
|
|
|
|
|
return plugin_conf |
|
|
|
|
|
except Exception as e: |
|
|
|
|
|
logger.exception(e) |