From 36f9680eec870be244afe5c77ea5bf05c0ee3566 Mon Sep 17 00:00:00 2001 From: erayyym Date: Tue, 5 Dec 2023 03:58:42 -0500 Subject: [PATCH 1/2] =?UTF-8?q?adding=20features:=20=E9=80=80=E7=BE=A4?= =?UTF-8?q?=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 后面还打算想办法加用户自己退出的提醒,目前版本是可以在群主(且群主/管理员自己是bot)踢人时候发出提醒 --- bridge/context.py | 2 ++ channel/wechat/wechat_channel.py | 2 +- channel/wechat/wechat_message.py | 14 +++++++++++--- plugins/hello/hello.py | 8 ++++++++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/bridge/context.py b/bridge/context.py index 1e5958c..04d6320 100644 --- a/bridge/context.py +++ b/bridge/context.py @@ -16,6 +16,8 @@ class ContextType(Enum): JOIN_GROUP = 20 # 加入群聊 PATPAT = 21 # 拍了拍 FUNCTION = 22 # 函数调用 + EXIT_GROUP = 23 #退出 + def __str__(self): return self.name diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index 0989a85..db77d83 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -170,7 +170,7 @@ class WechatChannel(ChatChannel): logger.debug("[WX]receive voice for group msg: {}".format(cmsg.content)) elif cmsg.ctype == ContextType.IMAGE: logger.debug("[WX]receive image for group msg: {}".format(cmsg.content)) - elif cmsg.ctype in [ContextType.JOIN_GROUP, ContextType.PATPAT, ContextType.ACCEPT_FRIEND]: + elif cmsg.ctype in [ContextType.JOIN_GROUP, ContextType.PATPAT, ContextType.ACCEPT_FRIEND, ContextType.EXIT_GROUP]: logger.debug("[WX]receive note msg: {}".format(cmsg.content)) elif cmsg.ctype == ContextType.TEXT: # logger.debug("[WX]receive group msg: {}, cmsg={}".format(json.dumps(cmsg._rawmsg, ensure_ascii=False), cmsg)) diff --git a/channel/wechat/wechat_message.py b/channel/wechat/wechat_message.py index e102018..b8b1d91 100644 --- a/channel/wechat/wechat_message.py +++ b/channel/wechat/wechat_message.py @@ -27,13 +27,21 @@ class WechatMessage(ChatMessage): self._prepare_fn = lambda: itchat_msg.download(self.content) elif itchat_msg["Type"] == NOTE and itchat_msg["MsgType"] == 10000: if is_group and ("加入群聊" in itchat_msg["Content"] or "加入了群聊" in itchat_msg["Content"]): - self.ctype = ContextType.JOIN_GROUP - self.content = itchat_msg["Content"] # 这里只能得到nickname, actual_user_id还是机器人的id if "加入了群聊" in itchat_msg["Content"]: + self.ctype = ContextType.JOIN_GROUP + self.content = itchat_msg["Content"] self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[-1] elif "加入群聊" in itchat_msg["Content"]: + self.ctype = ContextType.JOIN_GROUP + self.content = itchat_msg["Content"] self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0] + + elif is_group and ("移出了群聊" in itchat_msg["Content"]): + self.ctype = ContextType.EXIT_GROUP + self.content = itchat_msg["Content"] + self.actual_user_nickname = re.findall(r"\"(.*?)\"", itchat_msg["Content"])[0] + elif "你已添加了" in itchat_msg["Content"]: #通过好友请求 self.ctype = ContextType.ACCEPT_FRIEND self.content = itchat_msg["Content"] @@ -90,5 +98,5 @@ class WechatMessage(ChatMessage): if self.is_group: self.is_at = itchat_msg["IsAt"] self.actual_user_id = itchat_msg["ActualUserName"] - if self.ctype not in [ContextType.JOIN_GROUP, ContextType.PATPAT]: + if self.ctype not in [ContextType.JOIN_GROUP, ContextType.PATPAT, ContextType.EXIT_GROUP]: self.actual_user_nickname = itchat_msg["ActualNickName"] diff --git a/plugins/hello/hello.py b/plugins/hello/hello.py index a0004be..80c7837 100644 --- a/plugins/hello/hello.py +++ b/plugins/hello/hello.py @@ -29,6 +29,7 @@ class Hello(Plugin): ContextType.TEXT, ContextType.JOIN_GROUP, ContextType.PATPAT, + ContextType.EXIT_GROUP ]: return if e_context["context"].type == ContextType.JOIN_GROUP: @@ -46,6 +47,13 @@ class Hello(Plugin): e_context["context"].content = f'请你随机使用一种风格说一句问候语来欢迎新用户"{msg.actual_user_nickname}"加入群聊。' e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 return + + if e_context["context"].type == ContextType.EXIT_GROUP: + e_context["context"].type = ContextType.TEXT + msg: ChatMessage = e_context["context"]["msg"] + e_context["context"].content = f'请你随机使用一种风格跟其他群用户说他违反规则"{msg.actual_user_nickname}"退出群聊。' + e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 + return if e_context["context"].type == ContextType.PATPAT: e_context["context"].type = ContextType.TEXT From 4d8790655941f7d057653e791ba451fbb348b0eb Mon Sep 17 00:00:00 2001 From: erayyym Date: Tue, 5 Dec 2023 13:18:42 -0500 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BA=86=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 本地跑没有问题,用户打开这个功能需要在config.json加入 "group_chat_exit_group": true, (但是不确定写的对不对,刚开始学cs哈哈,之前没搞过这个) --- config.py | 1 + plugins/hello/hello.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/config.py b/config.py index 6cc5708..550c4f3 100644 --- a/config.py +++ b/config.py @@ -40,6 +40,7 @@ available_setting = { "image_create_prefix": ["画", "看", "找"], # 开启图片回复的前缀 "concurrency_in_session": 1, # 同一会话最多有多少条消息在处理中,大于1可能乱序 "image_create_size": "256x256", # 图片大小,可选有 256x256, 512x512, 1024x1024 (dall-e-3默认为1024x1024) + "group_chat_exit_group": False, # chatgpt会话参数 "expires_in_seconds": 3600, # 无操作会话的过期时间 # 人格描述 diff --git a/plugins/hello/hello.py b/plugins/hello/hello.py index 80c7837..76772af 100644 --- a/plugins/hello/hello.py +++ b/plugins/hello/hello.py @@ -49,12 +49,15 @@ class Hello(Plugin): return if e_context["context"].type == ContextType.EXIT_GROUP: - e_context["context"].type = ContextType.TEXT - msg: ChatMessage = e_context["context"]["msg"] - e_context["context"].content = f'请你随机使用一种风格跟其他群用户说他违反规则"{msg.actual_user_nickname}"退出群聊。' - e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 + if conf().get("group_chat_exit_group", []) == True: + e_context["context"].type = ContextType.TEXT + msg: ChatMessage = e_context["context"]["msg"] + e_context["context"].content = f'请你随机使用一种风格跟其他群用户说他违反规则"{msg.actual_user_nickname}"退出群聊。' + e_context.action = EventAction.BREAK # 事件结束,进入默认处理逻辑 + return + e_context.action = EventAction.BREAK return - + if e_context["context"].type == ContextType.PATPAT: e_context["context"].type = ContextType.TEXT msg: ChatMessage = e_context["context"]["msg"]