From c4d53d58ebcbf53251903d04d3cfec135c77f6e7 Mon Sep 17 00:00:00 2001 From: MingxuanGame Date: Tue, 24 May 2022 01:27:55 +0800 Subject: [PATCH] refactor&fix(manager): modify argument --- .../init_plugin_config/init_plugins_limit.py | 6 ++--- utils/manager/plugins2block_manager.py | 19 +++++--------- utils/manager/plugins2cd_manager.py | 17 ++++--------- utils/manager/plugins2count_manager.py | 14 +++-------- utils/manager/plugins2settings_manager.py | 25 ++++++------------- 5 files changed, 26 insertions(+), 55 deletions(-) diff --git a/basic_plugins/init_plugin_config/init_plugins_limit.py b/basic_plugins/init_plugin_config/init_plugins_limit.py index f1059f8a..3b80a09f 100755 --- a/basic_plugins/init_plugin_config/init_plugins_limit.py +++ b/basic_plugins/init_plugin_config/init_plugins_limit.py @@ -28,7 +28,7 @@ def init_plugins_cd_limit(data_path): _module = _plugin.module plugin_cd_limit = _module.__getattribute__("__plugin_cd_limit__") plugins2cd_manager.add_cd_limit( - matcher.plugin_name, data_dict=plugin_cd_limit + matcher.plugin_name, **plugin_cd_limit ) except AttributeError: pass @@ -76,7 +76,7 @@ def init_plugins_block_limit(data_path): _module = _plugin.module plugin_block_limit = _module.__getattribute__("__plugin_block_limit__") plugins2block_manager.add_block_limit( - matcher.plugin_name, data_dict=plugin_block_limit + matcher.plugin_name, **plugin_block_limit ) except AttributeError: pass @@ -124,7 +124,7 @@ def init_plugins_count_limit(data_path): _module = _plugin.module plugin_count_limit = _module.__getattribute__("__plugin_count_limit__") plugins2count_manager.add_count_limit( - matcher.plugin_name, data_dict=plugin_count_limit + matcher.plugin_name, **plugin_count_limit ) except AttributeError: pass diff --git a/utils/manager/plugins2block_manager.py b/utils/manager/plugins2block_manager.py index a30ff302..d1026186 100755 --- a/utils/manager/plugins2block_manager.py +++ b/utils/manager/plugins2block_manager.py @@ -21,18 +21,17 @@ class Plugins2blockManager(StaticData): with open(file, "r", encoding="utf8") as f: self._data = yaml.load(f) if "PluginBlockLimit" in self._data.keys(): - self._data = ( - self._data["PluginBlockLimit"] if self._data["PluginBlockLimit"] else {} - ) + self._data = self._data["PluginBlockLimit"] or {} def add_block_limit( self, plugin: str, + *, status: Optional[bool] = True, check_type: Optional[str] = "all", limit_type: Optional[str] = "user", rst: Optional[str] = None, - data_dict: Optional[dict] = None, + **kwargs # 用于接收额外实参 ): """ 添加插件调用 block 限制 @@ -41,16 +40,10 @@ class Plugins2blockManager(StaticData): :param check_type: 检查类型 'private'/'group'/'all',限制私聊/群聊/全部 :param limit_type: 限制类型 监听对象,以user_id或group_id作为键来限制,'user':用户id,'group':群id :param rst: 回复的话,为空则不回复 - :param data_dict: 封装好的字典数据 """ - if data_dict: - status = data_dict.get("status") - check_type = data_dict.get("check_type") - limit_type = data_dict.get("limit_type") - rst = data_dict.get("rst") - status = status if status is not None else True - check_type = check_type if check_type else "all" - limit_type = limit_type if limit_type else "user" + status = status or True + check_type = check_type or "all" + limit_type = limit_type or "user" if check_type not in ["all", "group", "private"]: raise ValueError( f"{plugin} 添加block限制错误,‘check_type‘ 必须为 'private'/'group'/'all'" diff --git a/utils/manager/plugins2cd_manager.py b/utils/manager/plugins2cd_manager.py index 2ea245c4..d262e6b7 100755 --- a/utils/manager/plugins2cd_manager.py +++ b/utils/manager/plugins2cd_manager.py @@ -34,7 +34,7 @@ class Plugins2cdManager(StaticData): check_type: Optional[str] = "all", limit_type: Optional[str] = "user", rst: Optional[str] = None, - data_dict: Optional[dict] = None, + **kwargs # 用于接收额外实参 ): """ 添加插件调用 cd 限制 @@ -44,18 +44,11 @@ class Plugins2cdManager(StaticData): :param check_type: 检查类型 'private'/'group'/'all',限制私聊/群聊/全部 :param limit_type: 限制类型 监听对象,以user_id或group_id作为键来限制,'user':用户id,'group':群id :param rst: 回复的话,为空则不回复 - :param data_dict: 封装好的字典数据 """ - if data_dict: - cd = data_dict.get("cd") - status = data_dict.get("status") - check_type = data_dict.get("check_type") - limit_type = data_dict.get("limit_type") - rst = data_dict.get("rst") - cd = cd if cd is not None else 5 - status = status if status is not None else True - check_type = check_type if check_type else "all" - limit_type = limit_type if limit_type else "user" + cd = cd or 5 + status = status or True + check_type = check_type or "all" + limit_type = limit_type or "user" if check_type not in ["all", "group", "private"]: raise ValueError( f"{plugin} 添加cd限制错误,‘check_type‘ 必须为 'private'/'group'/'all'" diff --git a/utils/manager/plugins2count_manager.py b/utils/manager/plugins2count_manager.py index 724c39ac..4ea97784 100755 --- a/utils/manager/plugins2count_manager.py +++ b/utils/manager/plugins2count_manager.py @@ -33,7 +33,7 @@ class Plugins2countManager(StaticData): status: Optional[bool] = True, limit_type: Optional[str] = "user", rst: Optional[str] = None, - data_dict: Optional[dict] = None, + **kwargs # 用于接收额外实参 ): """ 添加插件调用 次数 限制 @@ -42,16 +42,10 @@ class Plugins2countManager(StaticData): :param status: 默认开关状态 :param limit_type: 限制类型 监听对象,以user_id或group_id作为键来限制,'user':用户id,'group':群id :param rst: 回复的话,为空则不回复 - :param data_dict: 封装好的字典数据 """ - if data_dict: - max_count = data_dict.get("max_count") - status = data_dict.get("status") - limit_type = data_dict.get("limit_type") - rst = data_dict.get("rst") - status = status if status is not None else True - limit_type = limit_type if limit_type else "user" - max_count = max_count if max_count is not None else 5 + max_count = max_count or 5 + status = status or True + limit_type = limit_type or "user" if limit_type not in ["user", "group"]: raise ValueError(f"{plugin} 添加count限制错误,‘limit_type‘ 必须为 'user'/'group'") self._data[plugin] = { diff --git a/utils/manager/plugins2settings_manager.py b/utils/manager/plugins2settings_manager.py index c68520be..07017937 100755 --- a/utils/manager/plugins2settings_manager.py +++ b/utils/manager/plugins2settings_manager.py @@ -47,26 +47,17 @@ class Plugins2settingsManager(StaticData): :param plugin_type: 插件类型 :param cost_gold: 需要消费的金币 """ - if kwargs: - level = kwargs.get("level") if kwargs.get("level") is not None else 5 - default_status = ( - kwargs.get("default_status") - if kwargs.get("default_status") is not None - else True - ) - limit_superuser = ( - kwargs.get("limit_superuser") - if kwargs.get("limit_superuser") is not None - else False - ) - cmd = kwargs.get("cmd") if kwargs.get("cmd") is not None else [] - cost_gold = cost_gold if kwargs.get("cost_gold") else 0 + level = level or 5 + cmd = cmd or [] + cost_gold = cost_gold or 0 self._data[plugin] = { "level": level if level is not None else 5, "default_status": default_status if default_status is not None else True, - "limit_superuser": limit_superuser - if limit_superuser is not None - else False, + "limit_superuser": ( + limit_superuser + if limit_superuser is not None + else False + ), "cmd": cmd, "plugin_type": list( plugin_type if plugin_type is not None else ("normal",)