mirror of
https://github.com/RockChinQ/QChatGPT.git
synced 2024-11-16 11:42:44 +08:00
feat: 添加公告功能
This commit is contained in:
parent
ed5bf8100f
commit
7be368ae8c
12
main.py
12
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
|
||||
|
||||
|
||||
|
|
44
pkg/utils/announcement.py
Normal file
44
pkg/utils/announcement.py
Normal file
|
@ -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()
|
Loading…
Reference in New Issue
Block a user