mirror of
https://github.com/LiteyukiStudio/LiteyukiBot.git
synced 2024-11-13 14:27:26 +08:00
📝 初次启动生成默认配置文件时添加 satori 相关配置
📝 将适配器配置初始化和注册移动到 utils.adapter_manager
This commit is contained in:
parent
c914ddc0ee
commit
974b97b744
14
liteyuki/utils/adapter_manager/__init__.py
Normal file
14
liteyuki/utils/adapter_manager/__init__.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from . import (
|
||||||
|
satori,
|
||||||
|
onebot
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def init(config: dict):
|
||||||
|
onebot.init()
|
||||||
|
satori.init(config)
|
||||||
|
|
||||||
|
|
||||||
|
def register():
|
||||||
|
onebot.register()
|
||||||
|
satori.register()
|
12
liteyuki/utils/adapter_manager/onebot.py
Normal file
12
liteyuki/utils/adapter_manager/onebot.py
Normal 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)
|
28
liteyuki/utils/adapter_manager/satori.py
Normal file
28
liteyuki/utils/adapter_manager/satori.py
Normal 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)
|
@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
from typing import List
|
||||||
|
|
||||||
import nonebot
|
import nonebot
|
||||||
import yaml
|
import yaml
|
||||||
@ -11,12 +12,26 @@ from ..message.tools import random_hex_string
|
|||||||
config = {} # 全局配置,确保加载后读取
|
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):
|
class BasicConfig(BaseModel):
|
||||||
host: str = "127.0.0.1"
|
host: str = "127.0.0.1"
|
||||||
port: int = 20216
|
port: int = 20216
|
||||||
superusers: list[str] = []
|
superusers: list[str] = []
|
||||||
command_start: list[str] = ["/", ""]
|
command_start: list[str] = ["/", ""]
|
||||||
nickname: list[str] = [f"LiteyukiBot-{random_hex_string(6)}"]
|
nickname: list[str] = [f"LiteyukiBot-{random_hex_string(6)}"]
|
||||||
|
satori: SatoriConfig = SatoriConfig()
|
||||||
|
|
||||||
|
|
||||||
def load_from_yaml(file: str) -> dict:
|
def load_from_yaml(file: str) -> dict:
|
||||||
|
@ -11,7 +11,7 @@ def auto_set_env(config: dict):
|
|||||||
if os.getenv("DRIVER", None) is not None:
|
if os.getenv("DRIVER", None) is not None:
|
||||||
nonebot.logger.info("Driver already set in environment variable, skip auto configure.")
|
nonebot.logger.info("Driver already set in environment variable, skip auto configure.")
|
||||||
return
|
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)
|
os.environ["DRIVER"] = get_driver_string(ASGI_DRIVER, HTTPX_DRIVER, WEBSOCKETS_DRIVER)
|
||||||
nonebot.logger.info("Enable Satori, set driver to ASGI+HTTPX+WEBSOCKETS")
|
nonebot.logger.info("Enable Satori, set driver to ASGI+HTTPX+WEBSOCKETS")
|
||||||
else:
|
else:
|
||||||
|
23
main.py
23
main.py
@ -1,7 +1,7 @@
|
|||||||
import nonebot
|
import nonebot
|
||||||
from nonebot.adapters.onebot import v11, v12
|
from nonebot.adapters.onebot import v11, v12
|
||||||
from nonebot.adapters import satori
|
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.config import load_from_yaml
|
||||||
from liteyuki.utils.base.data_manager import StoredConfig, common_db
|
from liteyuki.utils.base.data_manager import StoredConfig, common_db
|
||||||
from liteyuki.utils.base.ly_api import liteyuki_api
|
from liteyuki.utils.base.ly_api import liteyuki_api
|
||||||
@ -12,16 +12,21 @@ if __name__ == "__mp_main__":
|
|||||||
static_config = load_from_yaml("config.yml")
|
static_config = load_from_yaml("config.yml")
|
||||||
store_config.update(static_config)
|
store_config.update(static_config)
|
||||||
driver_manager.init(config=store_config)
|
driver_manager.init(config=store_config)
|
||||||
|
adapter_manager.init(store_config)
|
||||||
nonebot.init(**store_config)
|
nonebot.init(**store_config)
|
||||||
|
|
||||||
if not store_config.get("enable_satori", False):
|
adapter_manager.register()
|
||||||
adapters = [v11.Adapter, v12.Adapter]
|
|
||||||
else:
|
# print(nonebot.get_adapters()['Satori'].__dict__)
|
||||||
adapters = [v11.Adapter, v12.Adapter, satori.Adapter]
|
|
||||||
driver = nonebot.get_driver()
|
# if not store_config.get("enable_satori", False):
|
||||||
# print(driver.__dict__)
|
# adapters = [v11.Adapter, v12.Adapter]
|
||||||
for adapter in adapters:
|
# else:
|
||||||
driver.register_adapter(adapter)
|
# adapters = [v11.Adapter, v12.Adapter, satori.Adapter]
|
||||||
|
# driver = nonebot.get_driver()
|
||||||
|
# # print(driver.__dict__)
|
||||||
|
# for adapter in adapters:
|
||||||
|
# driver.register_adapter(adapter)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
nonebot.load_plugin("liteyuki.liteyuki_main")
|
nonebot.load_plugin("liteyuki.liteyuki_main")
|
||||||
|
Loading…
Reference in New Issue
Block a user