You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.9KB

  1. import hashlib
  2. import re
  3. import time
  4. import config
  5. from common.log import logger
  6. def time_checker(f):
  7. def _time_checker(self, *args, **kwargs):
  8. _config = config.conf()
  9. chat_time_module = _config.get("chat_time_module", False)
  10. if chat_time_module:
  11. chat_start_time = _config.get("chat_start_time", "00:00")
  12. chat_stopt_time = _config.get("chat_stop_time", "24:00")
  13. time_regex = re.compile(r"^([01]?[0-9]|2[0-4])(:)([0-5][0-9])$") # 时间匹配,包含24:00
  14. starttime_format_check = time_regex.match(chat_start_time) # 检查停止时间格式
  15. stoptime_format_check = time_regex.match(chat_stopt_time) # 检查停止时间格式
  16. chat_time_check = chat_start_time < chat_stopt_time # 确定启动时间<停止时间
  17. # 时间格式检查
  18. if not (starttime_format_check and stoptime_format_check and chat_time_check):
  19. logger.warn("时间格式不正确,请在config.json中修改您的CHAT_START_TIME/CHAT_STOP_TIME,否则可能会影响您正常使用,开始({})-结束({})".format(starttime_format_check, stoptime_format_check))
  20. if chat_start_time > "23:59":
  21. logger.error("启动时间可能存在问题,请修改!")
  22. # 服务时间检查
  23. now_time = time.strftime("%H:%M", time.localtime())
  24. if chat_start_time <= now_time <= chat_stopt_time: # 服务时间内,正常返回回答
  25. f(self, *args, **kwargs)
  26. return None
  27. else:
  28. if args[0]["Content"] == "#更新配置": # 不在服务时间内也可以更新配置
  29. f(self, *args, **kwargs)
  30. else:
  31. logger.info("非服务时间内,不接受访问")
  32. return None
  33. else:
  34. f(self, *args, **kwargs) # 未开启时间模块则直接回答
  35. return _time_checker