From 33e69d5a44091f7bf3599f41eac4f4a04359df69 Mon Sep 17 00:00:00 2001 From: zhayujie Date: Thu, 23 Mar 2023 00:23:34 +0800 Subject: [PATCH] fix: hot reload repeat msg bug #186 --- .gitignore | 1 + channel/wechat/wechat_channel.py | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 80d1b9bf..1ca0b696 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ go-cqhttp logs/ session.token .vscode +itchat.pkl diff --git a/channel/wechat/wechat_channel.py b/channel/wechat/wechat_channel.py index ac5cc2f1..b5b50232 100644 --- a/channel/wechat/wechat_channel.py +++ b/channel/wechat/wechat_channel.py @@ -4,6 +4,7 @@ wechat channel """ +import time import itchat import json from itchat.content import * @@ -40,10 +41,11 @@ def __init__(self): def startup(self): # login by scan QRCode - if (channel_conf_val(const.WECHAT, 'receive_qrcode_api')): - itchat.auto_login(enableCmdQR=2, hotReload=True, qrCallback=self.login) + hot_reload = channel_conf_val(const.WECHAT, 'hot_reload', True) + if channel_conf_val(const.WECHAT, 'receive_qrcode_api'): + itchat.auto_login(enableCmdQR=2, hot_reload=hot_reload, qrCallback=self.login) else: - itchat.auto_login(enableCmdQR=2, hotReload=True) + itchat.auto_login(enableCmdQR=2, hotReload=hot_reload) # start message listener itchat.run() @@ -59,8 +61,14 @@ def handle(self, msg): from_user_id = msg['FromUserName'] to_user_id = msg['ToUserName'] # 接收人id other_user_id = msg['User']['UserName'] # 对手方id + create_time = msg['CreateTime'] # 消息时间 content = msg['Text'] + hot_reload = channel_conf_val(const.WECHAT, 'hot_reload', True) + if hot_reload == True and int(create_time) < int(time.time()) - 60: # 跳过1分钟前的历史消息 + logger.debug("[WX]history message skipped") + return + # 调用敏感词检测函数 if sw.process_text(content): self.send('请检查您的输入是否有违规内容', from_user_id) @@ -98,6 +106,13 @@ def handle_group(self, msg): logger.debug("[WX]receive group msg: " + json.dumps(msg, ensure_ascii=False)) group_name = msg['User'].get('NickName', None) group_id = msg['User'].get('UserName', None) + create_time = msg['CreateTime'] # 消息时间 + + hot_reload = channel_conf_val(const.WECHAT, 'hot_reload', True) + if hot_reload == True and int(create_time) < int(time.time()) - 60: # 跳过1分钟前的历史消息 + logger.debug("[WX]history message skipped") + return + if not group_name: return None origin_content = msg['Content']