Parcourir la source

fix: unable to use plugin when group nickname is set

zhayujie il y a 2 ans
Parent
commit
2386eb8fc2

+ 6 - 2
channel/chat_channel.py

@@ -108,8 +108,12 @@ class ChatChannel(Channel):
                     if not conf().get("group_at_off", False):
                         flag = True
                     pattern = f"@{re.escape(self.name)}(\u2005|\u0020)"
-                    content = re.sub(pattern, r"", content)
-
+                    subtract_res = re.sub(pattern, r"", content)
+                    if subtract_res == content and context["msg"].self_display_name:
+                        # 前缀移除后没有变化,使用群昵称再次移除
+                        pattern = f"@{re.escape(context['msg'].self_display_name)}(\u2005|\u0020)"
+                        subtract_res = re.sub(pattern, r"", content)
+                    content = subtract_res
                 if not flag:
                     if context["origin_ctype"] == ContextType.VOICE:
                         logger.info("[WX]receive group voice, but checkprefix didn't match")

+ 2 - 3
channel/chat_message.py

@@ -24,9 +24,7 @@ is_at: 是否被at
 - (群消息时,一般会存在实际发送者,是群内某个成员的id和昵称,下列项仅在群消息时存在)
 actual_user_id: 实际发送者id (群聊必填)
 actual_user_nickname:实际发送者昵称
-
-
-
+self_display_name: 自身的展示名,设置群昵称时,该字段表示群昵称
 
 _prepare_fn: 准备函数,用于准备消息的内容,比如下载图片等,
 _prepared: 是否已经调用过准备函数
@@ -49,6 +47,7 @@ class ChatMessage(object):
     other_user_id = None
     other_user_nickname = None
     my_msg = False
+    self_display_name = None
 
     is_group = False
     is_at = False

+ 5 - 1
channel/wechat/wechat_message.py

@@ -57,7 +57,8 @@ class WechatMessage(ChatMessage):
             self.from_user_nickname = nickname
         if self.to_user_id == user_id:
             self.to_user_nickname = nickname
-        try:  # 陌生人时候, 'User'字段可能不存在
+        try:  # 陌生人时候, User字段可能不存在
+            # my_msg 为True是表示是自己发送的消息
             self.my_msg = itchat_msg["ToUserName"] == itchat_msg["User"]["UserName"] and \
                           itchat_msg["ToUserName"] != itchat_msg["FromUserName"]
             self.other_user_id = itchat_msg["User"]["UserName"]
@@ -66,6 +67,9 @@ class WechatMessage(ChatMessage):
                 self.from_user_nickname = self.other_user_nickname
             if self.other_user_id == self.to_user_id:
                 self.to_user_nickname = self.other_user_nickname
+            if itchat_msg["User"].get("Self"):
+                # 自身的展示名,当设置了群昵称时,该字段表示群昵称
+                self.self_display_name = itchat_msg["User"].get("Self").get("DisplayName")
         except KeyError as e:  # 处理偶尔没有对方信息的情况
             logger.warn("[WX]get other_user_id failed: " + str(e))
             if self.from_user_id == user_id:

+ 1 - 1
plugins/linkai/README.md

@@ -33,7 +33,7 @@
 
 ## 插件使用
 
-> 使用插件中的知识库管理功能需要首先开启`linkai`对话,依赖全局 `config.json` 中的 `use_linkai` 和 `linkai_api_key` 配置;而midjourney绘画功能则只需填写 `linkai_api_key` 配置。具体可参考 [详细文档](https://link-ai.tech/platform/link-app/wechat)。
+> 使用插件中的知识库管理功能需要首先开启`linkai`对话,依赖全局 `config.json` 中的 `use_linkai` 和 `linkai_api_key` 配置;而midjourney绘画功能则只需填写 `linkai_api_key` 配置,`use_linkai` 无论是否关闭均可使用。具体可参考 [详细文档](https://link-ai.tech/platform/link-app/wechat)。
 
 完成配置后运行项目,会自动运行插件,输入 `#help linkai` 可查看插件功能。