mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-29 00:32:06 +08:00
🎨 代码优化
This commit is contained in:
@@ -1,18 +1,9 @@
|
||||
import ujson as json
|
||||
from nonebot.utils import is_coroutine_callable
|
||||
from tortoise import Tortoise
|
||||
from tortoise.connection import connections
|
||||
from tortoise.models import Model as Model_
|
||||
|
||||
from zhenxun.configs.config import (
|
||||
address,
|
||||
bind,
|
||||
database,
|
||||
password,
|
||||
port,
|
||||
sql_name,
|
||||
user,
|
||||
)
|
||||
from zhenxun.configs.config import BotConfig
|
||||
from zhenxun.configs.path_config import DATA_PATH
|
||||
|
||||
from .log import logger
|
||||
@@ -27,7 +18,7 @@ class Model(Model_):
|
||||
自动添加模块
|
||||
|
||||
Args:
|
||||
Model_ (_type_): Model
|
||||
Model_: Model
|
||||
"""
|
||||
|
||||
def __init_subclass__(cls, **kwargs):
|
||||
@@ -38,38 +29,13 @@ class Model(Model_):
|
||||
|
||||
|
||||
async def init():
|
||||
if DATABASE_SETTING_FILE.exists():
|
||||
with open(DATABASE_SETTING_FILE, "r", encoding="utf-8") as f:
|
||||
setting_data = json.load(f)
|
||||
else:
|
||||
i_bind = bind
|
||||
if not i_bind and any([user, password, address, port, database]):
|
||||
i_bind = f"{sql_name}://{user}:{password}@{address}:{port}/{database}"
|
||||
setting_data = {
|
||||
"bind": i_bind,
|
||||
"sql_name": sql_name,
|
||||
"user": user,
|
||||
"password": password,
|
||||
"address": address,
|
||||
"port": port,
|
||||
"database": database,
|
||||
}
|
||||
with open(DATABASE_SETTING_FILE, "w", encoding="utf-8") as f:
|
||||
json.dump(setting_data, f, ensure_ascii=False, indent=4)
|
||||
i_bind = setting_data.get("bind")
|
||||
_sql_name = setting_data.get("sql_name")
|
||||
_user = setting_data.get("user")
|
||||
_password = setting_data.get("password")
|
||||
_address = setting_data.get("address")
|
||||
_port = setting_data.get("port")
|
||||
_database = setting_data.get("database")
|
||||
if not i_bind and not any([_user, _password, _address, _port, _database]):
|
||||
raise ValueError("\n数据库配置未填写...")
|
||||
if not i_bind:
|
||||
i_bind = f"{_sql_name}://{_user}:{_password}@{_address}:{_port}/{_database}"
|
||||
if not BotConfig.db_url:
|
||||
raise Exception(f"数据库配置为空,请在.env.dev中配置DB_URL...")
|
||||
try:
|
||||
await Tortoise.init(
|
||||
db_url=i_bind, modules={"models": MODELS}, timezone="Asia/Shanghai"
|
||||
db_url=BotConfig.db_url,
|
||||
modules={"models": MODELS},
|
||||
timezone="Asia/Shanghai",
|
||||
)
|
||||
if SCRIPT_METHOD:
|
||||
db = Tortoise.get_connection("default")
|
||||
|
||||
Reference in New Issue
Block a user