From 7be368ae8c58ee400c631778c3cda8b8a7db9b34 Mon Sep 17 00:00:00 2001 From: Rock Chin <1010553892@qq.com> Date: Wed, 15 Mar 2023 15:43:36 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E5=85=AC=E5=91=8A?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 12 +++++++++-- pkg/utils/announcement.py | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 pkg/utils/announcement.py diff --git a/main.py b/main.py index fec0fde..21d8fb3 100644 --- a/main.py +++ b/main.py @@ -33,7 +33,7 @@ log_colors_config = { 'INFO': 'white', 'WARNING': 'yellow', 'ERROR': 'red', - 'CRITICAL': 'bold_red', + 'CRITICAL': 'cyan', } @@ -307,13 +307,21 @@ def main(first_time_init=False): import pkg.utils.updater try: if pkg.utils.updater.is_new_version_available(): - pkg.utils.context.get_qqbot_manager().notify_admin("新版本可用,请发送 !update 进行自动更新\n更新日志:\n{}".format("\n".join(pkg.utils.updater.get_rls_notes()))) + logging.info("新版本可用,请发送 !update 进行自动更新\n更新日志:\n{}".format("\n".join(pkg.utils.updater.get_rls_notes()))) else: logging.info("当前已是最新版本") except Exception as e: logging.warning("检查更新失败:{}".format(e)) + try: + import pkg.utils.announcement as announcement + new_announcement = announcement.fetch_new() + if new_announcement != "": + logging.critical("[公告] {}".format(new_announcement)) + except Exception as e: + logging.warning("获取公告失败:{}".format(e)) + return qqbot diff --git a/pkg/utils/announcement.py b/pkg/utils/announcement.py new file mode 100644 index 0000000..75a8269 --- /dev/null +++ b/pkg/utils/announcement.py @@ -0,0 +1,44 @@ +import base64 +import os + +import requests + + +def read_latest() -> str: + resp = requests.get( + url="https://api.github.com/repos/RockChinQ/QChatGPT/contents/res/announcement", + ) + obj_json = resp.json() + b64_content = obj_json["content"] + # 解码 + content = base64.b64decode(b64_content).decode("utf-8") + return content + + +def read_saved() -> str: + # 已保存的在res/announcement_saved + # 检查是否存在 + if not os.path.exists("res/announcement_saved"): + with open("res/announcement_saved", "w") as f: + f.write("") + + with open("res/announcement_saved", "r") as f: + content = f.read() + + return content + + +def write_saved(content: str): + # 已保存的在res/announcement_saved + with open("res/announcement_saved", "w") as f: + f.write(content) + + +def fetch_new() -> str: + latest = read_latest() + saved = read_saved() + if latest.replace(saved, "").strip() == "": + return "" + else: + write_saved(latest) + return latest.replace(saved, "").strip()