From 974b97b74423e660ff904eff7c7ab223d95b5f8d Mon Sep 17 00:00:00 2001 From: Expliyh Date: Fri, 17 May 2024 17:51:42 +0800 Subject: [PATCH] =?UTF-8?q?:pencil:=20=E5=88=9D=E6=AC=A1=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E7=94=9F=E6=88=90=E9=BB=98=E8=AE=A4=E9=85=8D=E7=BD=AE=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=97=B6=E6=B7=BB=E5=8A=A0=20satori=20=E7=9B=B8?= =?UTF-8?q?=E5=85=B3=E9=85=8D=E7=BD=AE=20:pencil:=20=E5=B0=86=E9=80=82?= =?UTF-8?q?=E9=85=8D=E5=99=A8=E9=85=8D=E7=BD=AE=E5=88=9D=E5=A7=8B=E5=8C=96?= =?UTF-8?q?=E5=92=8C=E6=B3=A8=E5=86=8C=E7=A7=BB=E5=8A=A8=E5=88=B0=20utils.?= =?UTF-8?q?adapter=5Fmanager?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteyuki/utils/adapter_manager/__init__.py | 14 ++++++++++ liteyuki/utils/adapter_manager/onebot.py | 12 ++++++++ liteyuki/utils/adapter_manager/satori.py | 28 +++++++++++++++++++ liteyuki/utils/base/config.py | 15 ++++++++++ liteyuki/utils/driver_manager/auto_set_env.py | 2 +- main.py | 23 +++++++++------ 6 files changed, 84 insertions(+), 10 deletions(-) create mode 100644 liteyuki/utils/adapter_manager/__init__.py create mode 100644 liteyuki/utils/adapter_manager/onebot.py create mode 100644 liteyuki/utils/adapter_manager/satori.py diff --git a/liteyuki/utils/adapter_manager/__init__.py b/liteyuki/utils/adapter_manager/__init__.py new file mode 100644 index 00000000..daf48023 --- /dev/null +++ b/liteyuki/utils/adapter_manager/__init__.py @@ -0,0 +1,14 @@ +from . import ( + satori, + onebot +) + + +def init(config: dict): + onebot.init() + satori.init(config) + + +def register(): + onebot.register() + satori.register() diff --git a/liteyuki/utils/adapter_manager/onebot.py b/liteyuki/utils/adapter_manager/onebot.py new file mode 100644 index 00000000..23d2a59c --- /dev/null +++ b/liteyuki/utils/adapter_manager/onebot.py @@ -0,0 +1,12 @@ +import nonebot +from nonebot.adapters.onebot import v11, v12 + + +def init(): + pass + + +def register(): + driver = nonebot.get_driver() + driver.register_adapter(v11.Adapter) + driver.register_adapter(v12.Adapter) diff --git a/liteyuki/utils/adapter_manager/satori.py b/liteyuki/utils/adapter_manager/satori.py new file mode 100644 index 00000000..86d741e1 --- /dev/null +++ b/liteyuki/utils/adapter_manager/satori.py @@ -0,0 +1,28 @@ +import json +import os + +import nonebot +from nonebot.adapters import satori + + +def init(config: dict): + if config.get("satori", None) is None: + nonebot.logger.info("Satori config not found, skip Satori init.") + return None + satori_config = config.get("satori") + if not satori_config.get("enable", False): + nonebot.logger.info("Satori not enabled, skip Satori init.") + return None + if os.getenv("SATORI_CLIENTS", None) is not None: + nonebot.logger.info("Satori clients already set in environment variable, skip.") + os.environ["SATORI_CLIENTS"] = json.dumps(satori_config.get("hosts", []), ensure_ascii=False) + config['satori_clients'] = satori_config.get("hosts", []) + print(json.dumps(satori_config.get("hosts", []), ensure_ascii=False)) + return + + +def register(): + if os.getenv("SATORI_CLIENTS", None) is not None: + print(os.getenv("SATORI_CLIENTS", None)) + driver = nonebot.get_driver() + driver.register_adapter(satori.Adapter) diff --git a/liteyuki/utils/base/config.py b/liteyuki/utils/base/config.py index b7ad1e19..eaccbf4d 100644 --- a/liteyuki/utils/base/config.py +++ b/liteyuki/utils/base/config.py @@ -1,4 +1,5 @@ import os +from typing import List import nonebot import yaml @@ -11,12 +12,26 @@ from ..message.tools import random_hex_string config = {} # 全局配置,确保加载后读取 +class SatoriNodeConfig(BaseModel): + host: str = "" + port: str = "5500" + path: str = "" + token: str = "" + + +class SatoriConfig(BaseModel): + comment: str = "These features are still in development. Do not enable in production environment." + enable: bool = False + hosts: List[SatoriNodeConfig] = [SatoriNodeConfig()] + + class BasicConfig(BaseModel): host: str = "127.0.0.1" port: int = 20216 superusers: list[str] = [] command_start: list[str] = ["/", ""] nickname: list[str] = [f"LiteyukiBot-{random_hex_string(6)}"] + satori: SatoriConfig = SatoriConfig() def load_from_yaml(file: str) -> dict: diff --git a/liteyuki/utils/driver_manager/auto_set_env.py b/liteyuki/utils/driver_manager/auto_set_env.py index 2568d80f..88466d24 100644 --- a/liteyuki/utils/driver_manager/auto_set_env.py +++ b/liteyuki/utils/driver_manager/auto_set_env.py @@ -11,7 +11,7 @@ def auto_set_env(config: dict): 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): + if config.get("satori", {'enable': False}).get("enable", 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: diff --git a/main.py b/main.py index c7248751..c795d25b 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ import nonebot from nonebot.adapters.onebot import v11, v12 from nonebot.adapters import satori -from liteyuki.utils import init, driver_manager +from liteyuki.utils import init, driver_manager, adapter_manager 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 @@ -12,16 +12,21 @@ if __name__ == "__mp_main__": static_config = load_from_yaml("config.yml") store_config.update(static_config) driver_manager.init(config=store_config) + adapter_manager.init(store_config) nonebot.init(**store_config) - if not store_config.get("enable_satori", False): - adapters = [v11.Adapter, v12.Adapter] - else: - adapters = [v11.Adapter, v12.Adapter, satori.Adapter] - driver = nonebot.get_driver() - # print(driver.__dict__) - for adapter in adapters: - driver.register_adapter(adapter) + adapter_manager.register() + + # print(nonebot.get_adapters()['Satori'].__dict__) + + # if not store_config.get("enable_satori", False): + # adapters = [v11.Adapter, v12.Adapter] + # else: + # adapters = [v11.Adapter, v12.Adapter, satori.Adapter] + # driver = nonebot.get_driver() + # # print(driver.__dict__) + # for adapter in adapters: + # driver.register_adapter(adapter) try: nonebot.load_plugin("liteyuki.liteyuki_main")