2024-03-19 13:16:25 +08:00
|
|
|
|
import os
|
2024-05-17 17:51:42 +08:00
|
|
|
|
from typing import List
|
2024-03-19 13:16:25 +08:00
|
|
|
|
|
|
|
|
|
import nonebot
|
|
|
|
|
import yaml
|
|
|
|
|
from pydantic import BaseModel
|
2024-03-18 18:21:56 +08:00
|
|
|
|
|
2024-04-27 02:20:44 +08:00
|
|
|
|
from .data_manager import StoredConfig, TempConfig, common_db
|
2024-04-14 21:39:27 +08:00
|
|
|
|
from .ly_typing import T_Bot
|
|
|
|
|
from ..message.tools import random_hex_string
|
2024-04-06 04:23:01 +08:00
|
|
|
|
|
|
|
|
|
config = {} # 全局配置,确保加载后读取
|
2024-03-19 00:27:40 +08:00
|
|
|
|
|
2024-03-18 18:21:56 +08:00
|
|
|
|
|
2024-05-17 17:51:42 +08:00
|
|
|
|
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()]
|
|
|
|
|
|
|
|
|
|
|
2024-03-19 13:16:25 +08:00
|
|
|
|
class BasicConfig(BaseModel):
|
|
|
|
|
host: str = "127.0.0.1"
|
|
|
|
|
port: int = 20216
|
|
|
|
|
superusers: list[str] = []
|
|
|
|
|
command_start: list[str] = ["/", ""]
|
2024-04-06 04:23:01 +08:00
|
|
|
|
nickname: list[str] = [f"LiteyukiBot-{random_hex_string(6)}"]
|
2024-05-17 17:51:42 +08:00
|
|
|
|
satori: SatoriConfig = SatoriConfig()
|
2024-03-19 13:16:25 +08:00
|
|
|
|
|
|
|
|
|
|
2024-03-18 18:21:56 +08:00
|
|
|
|
def load_from_yaml(file: str) -> dict:
|
2024-03-19 00:27:40 +08:00
|
|
|
|
global config
|
2024-03-24 21:21:57 +08:00
|
|
|
|
nonebot.logger.debug("Loading config from %s" % file)
|
2024-03-19 13:16:25 +08:00
|
|
|
|
if not os.path.exists(file):
|
2024-03-26 17:14:41 +08:00
|
|
|
|
nonebot.logger.warning(f"Config file {file} not found, created default config, please modify it and restart")
|
|
|
|
|
with open(file, "w", encoding="utf-8") as f:
|
2024-03-19 13:16:25 +08:00
|
|
|
|
yaml.dump(BasicConfig().dict(), f, default_flow_style=False)
|
|
|
|
|
|
2024-03-26 17:14:41 +08:00
|
|
|
|
with open(file, "r", encoding="utf-8") as f:
|
2024-03-31 09:03:28 +08:00
|
|
|
|
conf = init_conf(yaml.load(f, Loader=yaml.FullLoader))
|
2024-03-19 13:16:25 +08:00
|
|
|
|
config = conf
|
|
|
|
|
if conf is None:
|
2024-03-26 17:14:41 +08:00
|
|
|
|
nonebot.logger.warning(f"Config file {file} is empty, use default config. please modify it and restart")
|
2024-03-19 13:16:25 +08:00
|
|
|
|
conf = BasicConfig().dict()
|
|
|
|
|
return conf
|
2024-03-31 09:03:28 +08:00
|
|
|
|
|
|
|
|
|
|
2024-04-20 12:46:49 +08:00
|
|
|
|
def get_config(key: str, default=None):
|
2024-04-11 13:15:29 +08:00
|
|
|
|
"""获取配置项,优先级:bot > config > db > yaml"""
|
2024-04-20 12:46:49 +08:00
|
|
|
|
try:
|
|
|
|
|
bot = nonebot.get_bot()
|
|
|
|
|
except:
|
|
|
|
|
bot = None
|
|
|
|
|
|
2024-04-11 13:15:29 +08:00
|
|
|
|
if bot is None:
|
|
|
|
|
bot_config = {}
|
|
|
|
|
else:
|
|
|
|
|
bot_config = bot.config.dict()
|
2024-04-20 12:46:49 +08:00
|
|
|
|
|
2024-04-11 13:15:29 +08:00
|
|
|
|
if key in bot_config:
|
|
|
|
|
return bot_config[key]
|
|
|
|
|
|
|
|
|
|
elif key in config:
|
|
|
|
|
return config[key]
|
|
|
|
|
|
2024-05-12 00:18:53 +08:00
|
|
|
|
elif key in common_db.where_one(StoredConfig(), default=StoredConfig()).config:
|
|
|
|
|
return common_db.where_one(StoredConfig(), default=StoredConfig()).config[key]
|
2024-04-11 13:15:29 +08:00
|
|
|
|
|
|
|
|
|
elif key in load_from_yaml("config.yml"):
|
|
|
|
|
return load_from_yaml("config.yml")[key]
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
return default
|
|
|
|
|
|
|
|
|
|
|
2024-04-27 02:20:44 +08:00
|
|
|
|
def set_stored_config(key: str, value):
|
2024-05-12 00:18:53 +08:00
|
|
|
|
temp_config: TempConfig = common_db.where_one(TempConfig(), default=TempConfig())
|
2024-04-27 02:20:44 +08:00
|
|
|
|
temp_config.data[key] = value
|
|
|
|
|
common_db.save(temp_config)
|
|
|
|
|
|
|
|
|
|
|
2024-03-31 09:03:28 +08:00
|
|
|
|
def init_conf(conf: dict) -> dict:
|
2024-04-17 17:45:32 +08:00
|
|
|
|
"""
|
|
|
|
|
初始化配置文件,确保配置文件中的必要字段存在,且不会冲突
|
|
|
|
|
Args:
|
|
|
|
|
conf:
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
# 若command_start中无"",则添加必要命令头,开启alconna_use_command_start防止冲突
|
2024-03-31 09:03:28 +08:00
|
|
|
|
if "" not in conf.get("command_start", []):
|
|
|
|
|
conf["alconna_use_command_start"] = True
|
|
|
|
|
return conf
|