LiteyukiBot-TriM/liteyuki/utils/__init__.py

69 lines
2.1 KiB
Python
Raw Normal View History

import json
import os.path
import platform
import sys
2024-03-24 09:43:34 +08:00
import nonebot
2024-03-24 09:43:34 +08:00
__NAME__ = "LiteyukiBot"
2024-04-05 08:55:24 +08:00
__VERSION__ = "6.2.6" # 60201
import requests
2024-03-24 21:21:57 +08:00
from liteyuki.utils.config import load_from_yaml, config
from .log import init_log
2024-03-26 21:33:40 +08:00
from .data_manager import auto_migrate
2024-03-24 09:43:34 +08:00
major, minor, patch = map(int, __VERSION__.split("."))
__VERSION_I__ = major * 10000 + minor * 100 + patch
def register_bot():
url = "https://api.liteyuki.icu/register"
data = {
"name" : __NAME__,
"version" : __VERSION__,
"version_i": __VERSION_I__,
"python" : f"{platform.python_implementation()} {platform.python_version()}",
"os" : f"{platform.system()} {platform.version()} {platform.machine()}"
}
try:
nonebot.logger.info("Waiting for register to Liteyuki...")
resp = requests.post(url, json=data)
if resp.status_code == 200:
data = resp.json()
if liteyuki_id := data.get("liteyuki_id"):
with open("data/liteyuki/liteyuki.json", "wb") as f:
f.write(json.dumps(data).encode("utf-8"))
nonebot.logger.success(f"Register {liteyuki_id} to Liteyuki successfully")
else:
raise ValueError(f"Register to Liteyuki failed: {data}")
except Exception as e:
nonebot.logger.warning(f"Register to Liteyuki failed, but it's no matter: {e}")
2024-03-24 09:43:34 +08:00
def init():
"""
初始化
Returns:
"""
# 检测python版本是否高于3.10
if sys.version_info < (3, 10):
nonebot.logger.error("This project requires Python3.10+ to run, please upgrade your Python Environment.")
exit(1)
2024-03-26 21:33:40 +08:00
auto_migrate()
2024-03-24 21:21:57 +08:00
# 在加载完成语言后再初始化日志
init_log()
nonebot.logger.info("Liteyuki is initializing...")
if not os.path.exists("data/liteyuki/liteyuki.json"):
register_bot()
2024-03-24 09:43:34 +08:00
nonebot.logger.info(
f"Run Liteyuki with Python{sys.version_info.major}.{sys.version_info.minor}.{sys.version_info.micro} "
f"at {sys.executable}"
)
nonebot.logger.info(f"{__NAME__} {__VERSION__}({__VERSION_I__}) is running")