1
0
forked from bot/app

📝 初次启动生成默认配置文件时添加 satori 相关配置

📝 将适配器配置初始化和注册移动到 utils.adapter_manager
This commit is contained in:
Expliyh 2024-05-17 17:51:42 +08:00
parent c914ddc0ee
commit 974b97b744
No known key found for this signature in database
GPG Key ID: F30DE0CFF6157916
6 changed files with 84 additions and 10 deletions

View File

@ -0,0 +1,14 @@
from . import (
satori,
onebot
)
def init(config: dict):
onebot.init()
satori.init(config)
def register():
onebot.register()
satori.register()

View File

@ -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)

View File

@ -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)

View File

@ -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:

View File

@ -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:

23
main.py
View File

@ -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")