Skip to content

Commit

Permalink
✨ 启动时检查更新 (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
KomoriDev authored Aug 15, 2023
1 parent 1a649ff commit cbc23bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions sora/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from sora import database
from sora.utils import DRIVER
from sora.utils.update import check_update

DBConfigType = dict[str, Any]

Expand Down Expand Up @@ -33,6 +34,7 @@ async def _init(self, db_config: "DBConfigType", create_db: bool):
async def startup():
logger.opt(colors=True).info(logo)
await database.connect()
await check_update()
# await PluginManager.init()
# asyncio.ensure_future(check_resource())

Expand Down
13 changes: 10 additions & 3 deletions sora/utils/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,25 @@ async def check_update():
resp = await AsyncHttpx.get("https://api.github.com/repos/netsora/SoraBot/commits")
data = resp.json()
if not isinstance(data, list):
return "检查更新失败,可能是网络问题,请稍后再试"
result = "检查更新失败,可能是网络问题,请稍后再试"
logger.info("检查更新", result)
return result
try:
repo = Repo(Path().absolute())
except InvalidGitRepositoryError:
return "没有发现git仓库,无法通过git检查更新"
result = "没有发现git仓库,无法通过git检查更新"
logger.info("检查更新", result)
return result
local_commit = repo.head.commit
remote_commit = []
for commit in data:
if str(local_commit) == commit["sha"]:
break
remote_commit.append(commit)
if not remote_commit:
return f"当前已是最新版本:{__version__}"
result = f"当前已是最新版本:{__version__}"
logger.info("检查更新", result)
return result
result = "检查到更新,日志如下:\n"
for i, commit in enumerate(remote_commit, start=1):
time_str = (
Expand All @@ -101,4 +107,5 @@ async def check_update():
.replace(":memo:", "📝")
+ "\n"
)
logger.info("检查更新", result)
return result

0 comments on commit cbc23bb

Please sign in to comment.