mirror of
https://github.com/zhayujie/chatgpt-on-wechat.git
synced 2026-03-19 13:28:11 +08:00
fix: time_check model
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
import hashlib
|
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import config
|
import config
|
||||||
from common.log import logger
|
from common.log import logger
|
||||||
|
|
||||||
@@ -10,31 +8,33 @@ def time_checker(f):
|
|||||||
def _time_checker(self, *args, **kwargs):
|
def _time_checker(self, *args, **kwargs):
|
||||||
_config = config.conf()
|
_config = config.conf()
|
||||||
chat_time_module = _config.get("chat_time_module", False)
|
chat_time_module = _config.get("chat_time_module", False)
|
||||||
|
|
||||||
if chat_time_module:
|
if chat_time_module:
|
||||||
chat_start_time = _config.get("chat_start_time", "00:00")
|
chat_start_time = _config.get("chat_start_time", "00:00")
|
||||||
chat_stopt_time = _config.get("chat_stop_time", "24:00")
|
chat_stop_time = _config.get("chat_stop_time", "24:00")
|
||||||
time_regex = re.compile(r"^([01]?[0-9]|2[0-4])(:)([0-5][0-9])$") # 时间匹配,包含24:00
|
|
||||||
|
|
||||||
starttime_format_check = time_regex.match(chat_start_time) # 检查停止时间格式
|
time_regex = re.compile(r"^([01]?[0-9]|2[0-4])(:)([0-5][0-9])$")
|
||||||
stoptime_format_check = time_regex.match(chat_stopt_time) # 检查停止时间格式
|
|
||||||
chat_time_check = chat_start_time < chat_stopt_time # 确定启动时间<停止时间
|
|
||||||
|
|
||||||
# 时间格式检查
|
if not (time_regex.match(chat_start_time) and time_regex.match(chat_stop_time)):
|
||||||
if not (starttime_format_check and stoptime_format_check and chat_time_check):
|
logger.warning("时间格式不正确,请在config.json中修改CHAT_START_TIME/CHAT_STOP_TIME。")
|
||||||
logger.warn("时间格式不正确,请在config.json中修改您的CHAT_START_TIME/CHAT_STOP_TIME,否则可能会影响您正常使用,开始({})-结束({})".format(starttime_format_check, stoptime_format_check))
|
|
||||||
if chat_start_time > "23:59":
|
|
||||||
logger.error("启动时间可能存在问题,请修改!")
|
|
||||||
|
|
||||||
# 服务时间检查
|
|
||||||
now_time = time.strftime("%H:%M", time.localtime())
|
|
||||||
if chat_start_time <= now_time <= chat_stopt_time: # 服务时间内,正常返回回答
|
|
||||||
f(self, *args, **kwargs)
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
now_time = time.strptime(time.strftime("%H:%M"), "%H:%M")
|
||||||
|
chat_start_time = time.strptime(chat_start_time, "%H:%M")
|
||||||
|
chat_stop_time = time.strptime(chat_stop_time, "%H:%M")
|
||||||
|
# 结束时间小于开始时间,跨天了
|
||||||
|
if chat_stop_time < chat_start_time and (chat_start_time <= now_time or now_time <= chat_stop_time):
|
||||||
|
f(self, *args, **kwargs)
|
||||||
|
# 结束大于开始时间代表,没有跨天
|
||||||
|
elif chat_start_time < chat_stop_time and chat_start_time <= now_time <= chat_stop_time:
|
||||||
|
f(self, *args, **kwargs)
|
||||||
else:
|
else:
|
||||||
if args[0]["Content"] == "#更新配置": # 不在服务时间内也可以更新配置
|
# 定义匹配规则,如果以 #reconf 或者 #更新配置 结尾, 非服务时间可以修改开始/结束时间并重载配置
|
||||||
|
pattern = re.compile(r"^.*#(?:reconf|更新配置)$")
|
||||||
|
if args and pattern.match(args[0].content):
|
||||||
f(self, *args, **kwargs)
|
f(self, *args, **kwargs)
|
||||||
else:
|
else:
|
||||||
logger.info("非服务时间内,不接受访问")
|
logger.info("非服务时间内,不接受访问")
|
||||||
return None
|
return None
|
||||||
else:
|
else:
|
||||||
f(self, *args, **kwargs) # 未开启时间模块则直接回答
|
f(self, *args, **kwargs) # 未开启时间模块则直接回答
|
||||||
|
|||||||
Reference in New Issue
Block a user