From e1db381d2a331ebcbf308490ee2d53fb8a8fc0a1 Mon Sep 17 00:00:00 2001 From: Kyomotoi <1172294279@qq.com> Date: Mon, 9 Nov 2020 14:29:09 +0800 Subject: [Update] MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 调整文件结构; 修复文件读取、创建问题; 重构部分代码; 修复: - 涩图插件名读取错误 - 私聊消息无法发送 新增: - 好友请求处理 - 邀请请求处理 --- utils/utils_history/__init__.py | 96 ++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 44 deletions(-) (limited to 'utils/utils_history/__init__.py') 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} -- cgit v1.2.3