LiteyukiBot-TriM/src/utils/config.py

33 lines
997 B
Python
Raw Normal View History

2024-03-19 13:16:25 +08:00
import os
import nonebot
import yaml
from pydantic import BaseModel
2024-03-18 18:21:56 +08:00
2024-03-19 00:27:40 +08:00
config = None
2024-03-18 18:21:56 +08:00
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-03-19 20:38:25 +08:00
nickname: list[str] = ["liteyuki"]
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 13:16:25 +08:00
nonebot.logger.debug("Loading config from %s" % file)
2024-03-19 00:27:40 +08:00
global config
2024-03-19 13:16:25 +08:00
if not os.path.exists(file):
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:
yaml.dump(BasicConfig().dict(), f, default_flow_style=False)
2024-03-18 18:21:56 +08:00
with open(file, 'r', encoding='utf-8') as f:
2024-03-19 13:16:25 +08:00
conf = yaml.load(f, Loader=yaml.FullLoader)
config = conf
if conf is None:
nonebot.logger.warning(f'Config file {file} is empty, use default config. please modify it and restart')
conf = BasicConfig().dict()
return conf