From cbaa309976b8973124c7d66fbe3d50b970727905 Mon Sep 17 00:00:00 2001 From: Kyomotoi <0w0@imki.moe> Date: Fri, 6 Jan 2023 17:09:12 +0800 Subject: =?UTF-8?q?=F0=9F=8E=A8=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ATRI/plugins/anti_effort/data_source.py | 2 +- ATRI/plugins/console/driver/data_source.py | 4 +-- ATRI/plugins/help/data_source.py | 4 +-- ATRI/plugins/manage/data_source.py | 12 ++++---- ATRI/plugins/polaroid/data_source.py | 2 +- ATRI/plugins/saucenao/__init__.py | 2 +- ATRI/plugins/twitter/data_source.py | 2 +- ATRI/service.py | 44 ++++++++++++++---------------- 8 files changed, 34 insertions(+), 38 deletions(-) (limited to 'ATRI') diff --git a/ATRI/plugins/anti_effort/data_source.py b/ATRI/plugins/anti_effort/data_source.py index 2d04823..89b8ae2 100644 --- a/ATRI/plugins/anti_effort/data_source.py +++ b/ATRI/plugins/anti_effort/data_source.py @@ -258,7 +258,7 @@ async def init_source(): with open(file_path, "wb") as f: f.write(data.read()) # type: ignore except Exception: - ServiceTools.service_controller("谁是卷王", False) + ServiceTools("谁是卷王").service_controller(False) log.error(f"插件 anti_effort 装载资源失败. 已自动禁用") log.success("插件 anti_effort 装载资源完成") diff --git a/ATRI/plugins/console/driver/data_source.py b/ATRI/plugins/console/driver/data_source.py index 12fb938..45b07e6 100644 --- a/ATRI/plugins/console/driver/data_source.py +++ b/ATRI/plugins/console/driver/data_source.py @@ -87,7 +87,7 @@ def get_service_list() -> dict: def edit_service( service: str, global_enabled: int, enabled: bool, user_id: str, group_id: str ): - data = ServiceTools.load_service(service) + data = ServiceTools(service).load_service() if global_enabled != 2 and global_enabled: data.enabled = bool(global_enabled) @@ -115,7 +115,7 @@ def edit_service( else: return {"detail": "群已存在于禁用名单"} - ServiceTools.save_service(data.dict(), service) + ServiceTools(service).save_service(data.dict()) return {"detail": "操作完成~"} diff --git a/ATRI/plugins/help/data_source.py b/ATRI/plugins/help/data_source.py index 3386a74..fb99313 100644 --- a/ATRI/plugins/help/data_source.py +++ b/ATRI/plugins/help/data_source.py @@ -84,7 +84,7 @@ class Helper: @staticmethod def service_info(service: str) -> str: try: - data = ServiceTools().load_service(service) + data = ServiceTools(service).load_service() except Exception: return "请检查是否输入错误呢...@bot 帮助 (服务)" @@ -106,7 +106,7 @@ class Helper: @staticmethod def cmd_info(service: str, cmd: str) -> str: try: - data = ServiceTools().load_service(service) + data = ServiceTools(service).load_service() except Exception: return "请检查是否输入错误..." diff --git a/ATRI/plugins/manage/data_source.py b/ATRI/plugins/manage/data_source.py index e015805..ff344e7 100644 --- a/ATRI/plugins/manage/data_source.py +++ b/ATRI/plugins/manage/data_source.py @@ -144,11 +144,11 @@ class Manage: Only SUPERUSER. """ try: - data = ServiceTools().load_service(service) + data = ServiceTools(service).load_service() except Exception: return False data.enabled = is_enabled - ServiceTools().save_service(data.dict(), service) + ServiceTools(service).save_service(data.dict()) return True @staticmethod @@ -157,7 +157,7 @@ class Manage: Only SUPERUSER. """ try: - data = ServiceTools().load_service(service) + data = ServiceTools(service).load_service() except Exception: return False temp_list: list = data.disable_user @@ -174,7 +174,7 @@ class Manage: temp_list.append(user_id) data.disable_user = temp_list - ServiceTools().save_service(data.dict(), service) + ServiceTools(service).save_service(data.dict()) return True @staticmethod @@ -184,7 +184,7 @@ class Manage: Only current group. """ try: - data = ServiceTools().load_service(service) + data = ServiceTools(service).load_service() except Exception: return False temp_list: list = data.disable_group @@ -201,7 +201,7 @@ class Manage: temp_list.append(group_id) data.disable_group = temp_list - ServiceTools().save_service(data.dict(), service) + ServiceTools(service).save_service(data.dict()) return True @staticmethod diff --git a/ATRI/plugins/polaroid/data_source.py b/ATRI/plugins/polaroid/data_source.py index e994c5e..37f3904 100644 --- a/ATRI/plugins/polaroid/data_source.py +++ b/ATRI/plugins/polaroid/data_source.py @@ -47,7 +47,7 @@ async def init_source(): with open(path, "wb") as w: w.write(data.read()) # type: ignore except Exception: - ServiceTools.service_controller("拍立得", False) + ServiceTools("拍立得").service_controller(False) log.error(f"插件 polaroid 装载资源失败. 已自动禁用") log.success("插件 polaroid 装载资源完成") diff --git a/ATRI/plugins/saucenao/__init__.py b/ATRI/plugins/saucenao/__init__.py index b653a13..52cd93e 100644 --- a/ATRI/plugins/saucenao/__init__.py +++ b/ATRI/plugins/saucenao/__init__.py @@ -24,7 +24,7 @@ async def _deal_search(event: MessageEvent): # cache fix if not conf.SauceNAO.key: - ServiceTools.service_controller("以图搜图", False) + ServiceTools("以图搜图").service_controller(False) log.warning("插件 SauceNao 所需的 key 未配置,将被全局禁用,后续填写请手动启用") user_id = event.get_user_id() diff --git a/ATRI/plugins/twitter/data_source.py b/ATRI/plugins/twitter/data_source.py index c735b1f..d1dd8d5 100644 --- a/ATRI/plugins/twitter/data_source.py +++ b/ATRI/plugins/twitter/data_source.py @@ -146,7 +146,7 @@ async def _check_status(): await _regot_in_need() scheduler.add_job(_regot_in_need, "interval", name="刷新推特凭据", minutes=30, misfire_grace_time=10) # type: ignore except Exception: - ServiceTools.service_controller("推特动态订阅", False) + ServiceTools("推特动态订阅").service_controller(False) log.warning("无法连接至 Twitter,这将导致相关插件无法工作. 已自动禁用.") diff --git a/ATRI/service.py b/ATRI/service.py index 5a9b3b1..166cf21 100644 --- a/ATRI/service.py +++ b/ATRI/service.py @@ -369,37 +369,34 @@ class Service: class ServiceTools: """针对服务的工具类""" + def __init__(self, service: str): + self.service = service - @staticmethod - def save_service(service_data: dict, service: str): - path = SERVICES_DIR / f"{service}.json" + def save_service(self, service_data: dict): + path = SERVICES_DIR / f"{self.service}.json" if not path.is_file(): raise ReadFileError( - f"Can't find service file: {service}\n" - "Please delete all file in data/service/services\n" - "And reboot bot." + f"无法找到服务 {self.service} 对应的信息文件\n" + "请删除此目录下的文件: data/service/services\n" + "接着重新启动" ) with open(path, "w", encoding="utf-8") as w: w.write(json.dumps(service_data, indent=4)) - @staticmethod - def load_service(service: str) -> ServiceInfo: - path = SERVICES_DIR / f"{service}.json" + def load_service(self) -> ServiceInfo: + path = SERVICES_DIR / f"{self.service}.json" if not path.is_file(): raise ReadFileError( - f"Can't find service file: {service}\n" - "Please delete all file in data/service/services\n" - "And reboot bot." + f"无法找到服务 {self.service} 对应的信息文件\n" + "请删除此目录下的文件: data/service/services\n" + "接着重新启动" ) - with open(path, "r", encoding="utf-8") as r: - data = json.loads(r.read()) return ServiceInfo.parse_file(path) - @classmethod - def auth_service(cls, service, user_id: str = str(), group_id: str = str()) -> bool: - data = cls.load_service(service) + def auth_service(self, user_id: str = str(), group_id: str = str()) -> bool: + data = self.load_service() auth_global = data.enabled auth_user = data.disable_user @@ -414,27 +411,26 @@ class ServiceTools: return auth_global - @classmethod - def service_controller(cls, service: str, is_enabled: bool): - data = cls.load_service(service) + def service_controller(self, is_enabled: bool): + data = self.load_service() data.enabled = is_enabled - cls.save_service(data.dict(), service) + self.save_service(data.dict()) def is_in_service(service: str) -> Rule: async def _is_in_service(bot: Bot, event: Event) -> bool: - result = ServiceTools().auth_service(service) + result = ServiceTools(service).auth_service() if not result: return False if isinstance(event, PrivateMessageEvent): user_id = event.get_user_id() - result = ServiceTools().auth_service(service, user_id) + result = ServiceTools(service).auth_service(user_id) return result elif isinstance(event, GroupMessageEvent): user_id = event.get_user_id() group_id = str(event.group_id) - result = ServiceTools().auth_service(service, user_id, group_id) + result = ServiceTools(service).auth_service(user_id, group_id) return result else: return True -- cgit v1.2.3