diff --git a/main.py b/main.py index 8633f574..45576bc2 100644 --- a/main.py +++ b/main.py @@ -8,7 +8,9 @@ import logging import sys import mirai.exceptions +import requests import websockets.exceptions +from urllib3.exceptions import InsecureRequestWarning try: import colorlog @@ -170,6 +172,17 @@ def main(first_time_init=False): else: logging.info('热重载完成') + time.sleep(5) + import pkg.utils.updater + try: + if pkg.utils.updater.is_new_version_available(): + pkg.utils.context.get_qqbot_manager().notify_admin("新版本可用,请发送 !update 进行自动更新") + else: + logging.info("当前已是最新版本") + + except Exception as e: + logging.error("检查更新失败:{}".format(e)) + while True: try: time.sleep(10) @@ -225,5 +238,5 @@ if __name__ == '__main__': # import pkg.utils.configmgr # # pkg.utils.configmgr.set_config_and_reload("quote_origin", False) - + requests.packages.urllib3.disable_warnings(InsecureRequestWarning) main(True) diff --git a/pkg/utils/updater.py b/pkg/utils/updater.py index 38b9b0c6..f169f9ca 100644 --- a/pkg/utils/updater.py +++ b/pkg/utils/updater.py @@ -58,3 +58,24 @@ def get_commit_id_and_time_and_msg() -> str: dt = datetime.datetime.fromtimestamp(entry.commit.commit_time, tz) return str(entry.commit.id)[2:9] + " " + dt.strftime('%Y-%m-%d %H:%M:%S') + " [" + str(entry.commit.message, encoding="utf-8").strip()+"]" + +def is_new_version_available() -> bool: + """检查是否有新版本""" + try: + import dulwich + except ModuleNotFoundError: + raise Exception("dulwich模块未安装,请查看 https://github.com/RockChinQ/QChatGPT/issues/77") + + from dulwich import porcelain + + repo = porcelain.open_repo('.') + fetch_res = porcelain.ls_remote(porcelain.get_remote_repo(repo, "origin")[1]) + + current_commit_id = "" + for entry in repo.get_walker(): + current_commit_id = str(entry.commit.id)[2:9] + break + + latest_commit_id = str(fetch_res[b'HEAD'])[2:9] + + return current_commit_id != latest_commit_id