diff options
Diffstat (limited to 'utils')
-rw-r--r-- | utils/utils_history/__init__.py | 96 | ||||
-rw-r--r-- | utils/utils_rule/__init__.py | 85 |
2 files changed, 88 insertions, 93 deletions
diff --git a/utils/utils_history/__init__.py b/utils/utils_history/__init__.py index 1ee14f0..4453e11 100644 --- a/utils/utils_history/__init__.py +++ b/utils/utils_history/__init__.py @@ -12,6 +12,7 @@ __author__ = 'kyomotoi' import os import json +import datetime from pathlib import Path from typing import Optional @@ -21,54 +22,58 @@ def saveMessage(message_id: str, user: str, group: Optional[str] = None) -> None: """储存消息""" - file_group = Path( - '.') / 'ATRI' / 'data' / 'data_Group' / f'{group}' / 'message.json' - file_private = Path( - '.') / 'ATRI' / 'data' / 'data_Log' / 'message_private.json' - - try: - with open(file_group, 'r') as f: - data_group = json.load(f) - except: - data_group = {} - - try: - with open(file_private, 'r') as f: - data_private = json.load(f) - except: - data_private = {} + GROUP_PATH = Path( + '.' + ) / 'ATRI' / 'data' / 'data_Group' / f'{group}' / f"{datetime.datetime.now().strftime('%Y-%m-%d')}-message.json" + PRIVATE_PATH = Path( + '.' + ) / 'ATRI' / 'data' / 'data_Private_Message' / f"{datetime.datetime.now().strftime('%Y-%m-%d')}-private-message.json" + # 检查目标文件目录 + if not GROUP_PATH.is_file(): + try: + os.mkdir(Path('.') / 'ATRI' / 'data' / 'data_Group' / f'{group}') + except: + pass + + with open(GROUP_PATH, 'w') as f: + f.write(json.dumps({})) + + if not PRIVATE_PATH.is_file(): + try: + os.mkdir(Path('.') / 'ATRI' / 'data' / 'data_Private_Message') + except: + pass + + with open(PRIVATE_PATH, 'w') as f: + f.write(json.dumps({})) + + # 加载目标文件 + with open(GROUP_PATH, 'r') as f: + DATA_GROUP = json.load(f) + + with open(PRIVATE_PATH, 'r') as f: + DATA_PRIVATE = json.load(f) + + # 写入 if group: - data_group[f"{message_id}"] = { + DATA_GROUP[f"{message_id}"] = { "message": f"{message}", "user_id": f"{user}", "group_id": f"{group}" } - try: - with open(file_group, 'w') as f: - f.write(json.dumps(data_group)) - f.close() - except: - os.mkdir(Path('.') / 'ATRI' / 'data' / 'data_Group' / f'{group}') - with open(file_group, 'w') as f: - f.write(json.dumps(data_group)) - f.close() + with open(GROUP_PATH, 'w') as f: + f.write(json.dumps(DATA_GROUP)) + else: - data_private[f"{message_id}"] = { + DATA_PRIVATE[f"{message_id}"] = { "message": f"{message}", "user_id": f"{user}" } - try: - with open(file_private, 'w') as f: - f.write(json.dumps(data_private)) - f.close() - except: - os.mkdir(Path('.') / 'ATRI' / 'data' / 'data_Log') - with open(file_private, 'w') as f: - f.write(json.dumps(data_private)) - f.close() + with open(PRIVATE_PATH, 'w') as f: + f.write(json.dumps(DATA_PRIVATE)) def getMessage(message_id: str, group: Optional[str] = None) -> dict: @@ -77,24 +82,27 @@ def getMessage(message_id: str, group: Optional[str] = None) -> dict: :return: dict ''' - file_group = Path( - '.') / 'ATRI' / 'data' / 'data_Group' / f'{group}' / 'message.json' - file_private = Path( - '.') / 'ATRI' / 'data' / 'data_Log' / 'message_private.json' + GROUP_PATH = Path( + '.' + ) / 'ATRI' / 'data' / 'data_Group' / f'{group}' / f"{datetime.datetime.now().strftime('%Y-%m-%d')}-message.json" + PRIVATE_PATH = Path( + '.' + ) / 'ATRI' / 'data' / 'data_Private_Message' / f"{datetime.datetime.now().strftime('%Y-%m-%d')}-private-message.json" if group: try: - with open(file_group, 'r') as f: + with open(GROUP_PATH, 'r') as f: data_group = json.load(f) return data_group[message_id] except: - return {"status": "None"} + return {"status": 0} + else: try: - with open(file_private, 'r') as f: + with open(PRIVATE_PATH, 'r') as f: data_private = json.load(f) return data_private[message_id] except: - return {"status": "None"} + return {"status": 0} diff --git a/utils/utils_rule/__init__.py b/utils/utils_rule/__init__.py index c1d0757..1ec09db 100644 --- a/utils/utils_rule/__init__.py +++ b/utils/utils_rule/__init__.py @@ -29,25 +29,25 @@ def check_banlist() -> Rule: group = str(event.group_id) # 名单目录 - file_user = Path('.') / 'utils' / 'utils_rule' / 'ban_list_user.json' - file_group = Path('.') / 'utils' / 'utils_rule' / 'ban_list_group.json' + BAN_LIST_USER_PATH = Path( + '.') / 'utils' / 'utils_rule' / 'ban_list_user.json' + BAN_LIST_GROUP_PATH = Path( + '.') / 'utils' / 'utils_rule' / 'ban_list_group.json' # 检查文件是否存在,如不存在,自动创建并写入默认值 - if not file_user.is_file(): - file = open(file_user, 'w') - file.write(json.dumps({})) - file.close() + if not BAN_LIST_USER_PATH.is_file(): + with open(BAN_LIST_USER_PATH, 'w') as f: + f.write(json.dumps({})) - if not file_group.is_file(): - file = open(file_group, 'w') - file.write(json.dumps({})) - file.close() + if not BAN_LIST_GROUP_PATH.is_file(): + with open(BAN_LIST_GROUP_PATH, 'w') as f: + f.write(json.dumps({})) # 读取文件 - with open(file_user, 'r') as f: + with open(BAN_LIST_USER_PATH, 'r') as f: data_user = json.load(f) - with open(file_group, 'r') as f: + with open(BAN_LIST_GROUP_PATH, 'r') as f: data_group = json.load(f) # 判断目标 @@ -91,58 +91,45 @@ def check_switch(func_name: str) -> Rule: group = str(event.group_id) # 文件目录 - file_switch_all = Path('.') / 'utils' / 'utils_rule' / 'switch.json' - file_switch_alone = Path( + SWITCH_ALL_PATH = Path('.') / 'utils' / 'utils_rule' / 'switch.json' + SWITCH_ALONE_PATH = Path( '.') / 'ATRI' / 'data' / 'data_Group' / f'{group}' / 'switch.json' # 检查文件是否存在,如不存在,自动创建并写入默认值 - if not file_switch_all.is_file(): - data_switch_all = {} - data_switch_all["anime-setu"] = "True" - data_switch_all["anime-pic-search"] = "True" - data_switch_all["anime-vid-search"] = "True" - data_switch_all["ai-face"] = "True" - data_switch_all["pixiv-pic-search"] = "True" - data_switch_all["pixiv-author-search"] = "True" - data_switch_all["pixiv-rank"] = "True" - data_switch_all["one-key-adult"] = "True" - data_switch_all["genshin-search"] = "True" - - file = open(file_switch_all, 'w') - file.write(json.dumps(data_switch_all)) - file.close() - - if not file_switch_alone.is_file(): - data_switch_alone = {} - - # 检查目标文件夹是否存在,如不存在自动创建 + if not SWITCH_ALL_PATH.is_file(): + with open(SWITCH_ALL_PATH, 'ws') as f: + f.write(json.dumps({})) + + if not SWITCH_ALONE_PATH.is_file(): try: os.mkdir( Path('.') / 'ATRI' / 'data' / 'data_Group' / f'{group}') except: pass - data_switch_alone["anime-setu"] = "True" - data_switch_alone["anime-pic-search"] = "True" - data_switch_alone["anime-vid-search"] = "True" - data_switch_alone["ai-face"] = "True" - data_switch_alone["pixiv-pic-search"] = "True" - data_switch_alone["pixiv-author-search"] = "True" - data_switch_alone["pixiv-rank"] = "True" - data_switch_alone["one-key-adult"] = "True" - data_switch_alone["genshin-search"] = "True" - - file = open(file_switch_alone, 'w') - file.write(json.dumps(data_switch_alone)) - file.close() + with open(SWITCH_ALONE_PATH, 'w') as f: + f.write(json.dumps({})) # 读取文件 - with open(file_switch_all, 'r') as f: + with open(SWITCH_ALL_PATH, 'r') as f: data_all = json.load(f) - with open(file_switch_alone, 'r') as f: + with open(SWITCH_ALONE_PATH, 'r') as f: data_alone = json.load(f) + # 判断目标是否存在于将要读取的文件,如不存在,写入 + # 此项举措是为了适应以后版本更新出现新插件的状况 + # 不至于每次都需要修改 + if func_name not in data_all: + data_all[func_name] = "True" + with open(SWITCH_ALL_PATH, 'w') as f: + f.write(json.dumps(data_all)) + + if func_name not in data_alone: + data_alone[func_name] = "True" + with open(SWITCH_ALONE_PATH, 'w') as f: + f.write(json.dumps(data_alone)) + # 判断目标 if data_all[func_name] == "True": if data_alone[func_name] == "True": |