mirror of
https://github.com/TriM-Organization/LiteyukiBot-TriM.git
synced 2024-11-11 01:27:29 +08:00
📝 使用 driver_manager自动管理启用的驱动器,无需手动配置环境变量 (当配置了环境变量时环境变量优先)
This commit is contained in:
parent
309397b72c
commit
a72eeb4c3f
6
liteyuki/driver_manager/__init__.py
Normal file
6
liteyuki/driver_manager/__init__.py
Normal file
@ -0,0 +1,6 @@
|
||||
from .auto_set_env import auto_set_env
|
||||
|
||||
|
||||
def init(config: dict):
|
||||
auto_set_env(config)
|
||||
return
|
20
liteyuki/driver_manager/auto_set_env.py
Normal file
20
liteyuki/driver_manager/auto_set_env.py
Normal file
@ -0,0 +1,20 @@
|
||||
import os
|
||||
|
||||
import dotenv
|
||||
import nonebot
|
||||
|
||||
from .defines import *
|
||||
|
||||
|
||||
def auto_set_env(config: dict):
|
||||
dotenv.load_dotenv()
|
||||
if os.getenv("DRIVER", None) is not None:
|
||||
nonebot.logger.info("Driver already set in environment variable, skip auto configure.")
|
||||
return
|
||||
if config.get("enable_satori", False):
|
||||
os.environ["DRIVER"] = get_driver_string(ASGI_DRIVER, HTTPX_DRIVER, WEBSOCKETS_DRIVER)
|
||||
nonebot.logger.info("Enable Satori, set driver to ASGI+HTTPX+WEBSOCKETS")
|
||||
else:
|
||||
os.environ["DRIVER"] = get_driver_string(ASGI_DRIVER)
|
||||
nonebot.logger.info("Disable Satori, set driver to ASGI")
|
||||
return
|
17
liteyuki/driver_manager/defines.py
Normal file
17
liteyuki/driver_manager/defines.py
Normal file
@ -0,0 +1,17 @@
|
||||
ASGI_DRIVER = "~fastapi"
|
||||
HTTPX_DRIVER = "~httpx"
|
||||
WEBSOCKETS_DRIVER = "~websockets"
|
||||
|
||||
|
||||
def get_driver_string(*argv):
|
||||
output_string = ""
|
||||
if ASGI_DRIVER in argv:
|
||||
output_string += ASGI_DRIVER
|
||||
for arg in argv:
|
||||
if arg != ASGI_DRIVER:
|
||||
output_string = f"{output_string}+{arg}"
|
||||
return output_string
|
||||
|
||||
|
||||
def get_driver_full_string(*argv):
|
||||
return f"DRIVER={get_driver_string(argv)}"
|
4
main.py
4
main.py
@ -5,12 +5,14 @@ from liteyuki.utils import init
|
||||
from liteyuki.utils.base.config import load_from_yaml
|
||||
from liteyuki.utils.base.data_manager import StoredConfig, common_db
|
||||
from liteyuki.utils.base.ly_api import liteyuki_api
|
||||
from liteyuki import driver_manager
|
||||
|
||||
if __name__ == "__mp_main__":
|
||||
init()
|
||||
store_config: dict = common_db.where_one(StoredConfig(), default=StoredConfig()).config
|
||||
static_config = load_from_yaml("config.yml")
|
||||
store_config.update(static_config)
|
||||
driver_manager.init(config=store_config)
|
||||
nonebot.init(**store_config)
|
||||
|
||||
if not store_config.get("enable_satori", False):
|
||||
@ -18,7 +20,7 @@ if __name__ == "__mp_main__":
|
||||
else:
|
||||
adapters = [v11.Adapter, v12.Adapter, satori.Adapter]
|
||||
driver = nonebot.get_driver()
|
||||
|
||||
# print(driver.__dict__)
|
||||
for adapter in adapters:
|
||||
driver.register_adapter(adapter)
|
||||
|
||||
|
@ -23,3 +23,7 @@ importlib_metadata~=7.0.2
|
||||
requests~=2.31.0
|
||||
watchdog~=4.0.0
|
||||
pillow~=10.2.0
|
||||
|
||||
pip~=23.2.1
|
||||
fastapi~=0.110.0
|
||||
python-dotenv~=1.0.1
|
Loading…
Reference in New Issue
Block a user