This commit is contained in:
HibiKier 2022-05-26 22:49:48 +08:00
parent 134974ea9c
commit 9c987e9aad
3 changed files with 56 additions and 15 deletions

View File

@ -245,7 +245,10 @@ __Docker 最新版本由 [Sakuracio](https://github.com/Sakuracio) 提供__
### 2022/5/26 ### 2022/5/26
* 修复[滴滴滴]会被转义成[滴滴滴]导致无法触发的问题 * 修复\[滴滴滴]会被转义成[滴滴滴]导致无法触发的问题
* 将一些错误以中文提示输出
* 更新BT搜索源地址 [@pull/668](https://github.com/HibiKier/zhenxun_bot/pull/668)
* 更新抽卡插件 [@pull/667](https://github.com/HibiKier/zhenxun_bot/pull/667)
### 2022/5/25 ### 2022/5/25

View File

@ -56,7 +56,9 @@ def init_plugins_config(data_path):
_override=True, _override=True,
) )
else: else:
Config.add_plugin_config(matcher.plugin_name, key, plugin_configs[key]) Config.add_plugin_config(
matcher.plugin_name, key, plugin_configs[key]
)
else: else:
plugin_configs = _data[matcher.plugin_name] plugin_configs = _data[matcher.plugin_name]
for key in plugin_configs: for key in plugin_configs:
@ -96,14 +98,25 @@ def init_plugins_config(data_path):
for plugin in Config.keys(): for plugin in Config.keys():
_tmp_data[plugin] = {} _tmp_data[plugin] = {}
for k in Config[plugin].keys(): for k in Config[plugin].keys():
if _data.get(plugin) and k in _data[plugin].keys(): try:
Config.set_config(plugin, k, _data[plugin][k]) if _data.get(plugin) and k in _data[plugin].keys():
if level2module := Config.get_level2module(plugin, k): Config.set_config(plugin, k, _data[plugin][k])
try: if level2module := Config.get_level2module(plugin, k):
admin_manager.set_admin_level(level2module, _data[plugin][k]) try:
except KeyError: admin_manager.set_admin_level(
logger.warning(f"{level2module} 设置权限等级失败:{_data[plugin][k]}") level2module, _data[plugin][k]
_tmp_data[plugin][k] = Config.get_config(plugin, k) )
except KeyError:
logger.warning(
f"{level2module} 设置权限等级失败:{_data[plugin][k]}"
)
_tmp_data[plugin][k] = Config.get_config(plugin, k)
except AttributeError as e:
raise AttributeError(
f"{e}\n**********************************************\n"
f"****** 可能为config.yaml配置文件填写不规范 ******\n"
f"**********************************************"
)
Config.save() Config.save()
temp_file = Path() / "configs" / "temp_config.yaml" temp_file = Path() / "configs" / "temp_config.yaml"
try: try:

View File

@ -2,6 +2,7 @@ from typing import Optional, Any, Union
from pathlib import Path from pathlib import Path
from ruamel.yaml import YAML from ruamel.yaml import YAML
from ruamel import yaml from ruamel import yaml
from ruamel.yaml.scanner import ScannerError
class ConfigsManager: class ConfigsManager:
@ -21,9 +22,24 @@ class ConfigsManager:
if file.exists(): if file.exists():
with open(file, "r", encoding="utf8") as f: with open(file, "r", encoding="utf8") as f:
self._data = _yaml.load(f) self._data = _yaml.load(f)
if not self._data:
self.file.unlink()
raise ValueError(
"配置文件为空!\n"
"***********************************************************\n"
"****** 配置文件 plugins2config.yaml 为空,已删除,请重启 ******\n"
"***********************************************************"
)
if self._simple_file.exists(): if self._simple_file.exists():
with open(self._simple_file, "r", encoding="utf8") as f: try:
self._simple_data = _yaml.load(f) with open(self._simple_file, "r", encoding="utf8") as f:
self._simple_data = _yaml.load(f)
except ScannerError as e:
raise ScannerError(
f"{e}\n**********************************************\n"
f"****** 可能为config.yaml配置文件填写不规范 ******\n"
f"**********************************************"
)
def add_plugin_config( def add_plugin_config(
self, self,
@ -86,7 +102,10 @@ class ConfigsManager:
:param value: :param value:
""" """
if module in self._data.keys(): if module in self._data.keys():
if self._data[module].get(key) is not None and self._data[module][key] != value: if (
self._data[module].get(key) is not None
and self._data[module][key] != value
):
self._data[module][key]["value"] = value self._data[module][key]["value"] = value
self._simple_data[module][key] = value self._simple_data[module][key] = value
self.save() self.save()
@ -115,7 +134,9 @@ class ConfigsManager:
self._data[module][key]["default_value"] = value self._data[module][key]["default_value"] = value
self.save() self.save()
def get_config(self, module: str, key: str, default: Optional[Any] = None) -> Optional[Any]: def get_config(
self, module: str, key: str, default: Optional[Any] = None
) -> Optional[Any]:
""" """
获取指定配置值 获取指定配置值
:param module: 模块名 :param module: 模块名
@ -161,7 +182,11 @@ class ConfigsManager:
if save_simple_data: if save_simple_data:
with open(self._simple_file, "w", encoding="utf8") as f: with open(self._simple_file, "w", encoding="utf8") as f:
yaml.dump( yaml.dump(
self._simple_data, f, indent=2, Dumper=yaml.RoundTripDumper, allow_unicode=True self._simple_data,
f,
indent=2,
Dumper=yaml.RoundTripDumper,
allow_unicode=True,
) )
path = path if path else self.file path = path if path else self.file
with open(path, "w", encoding="utf8") as f: with open(path, "w", encoding="utf8") as f: