summaryrefslogtreecommitdiff
path: root/utils/utils_history/__init__.py
blob: 6afc1e0be6e7b6256bd4c90be652137847663e45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   __init__.py
@Time    :   2020/10/11 14:43:20
@Author  :   Kyomotoi
@Contact :   [email protected]
@Github  :   https://github.com/Kyomotoi
@License :   Copyright © 2018-2020 Kyomotoi, All Rights Reserved.
'''
__author__ = 'kyomotoi'

import os
import json
from pathlib import Path
from typing import Optional

def saveMessage(message_id: str, message: 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 = {}
    
    if group:
        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()
    else:
        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()

def getMessage(message_id: str, group: Optional[str] = None) -> dict:
    '''
    传入消息id以获取对应信息
    
    :return: dict
    '''
    file_group = Path('.') / 'ATRI' / 'data' / 'data_Group' / f'{group}' / 'message.json'
    file_private = Path('.') / 'ATRI' / 'data' / 'data_Log' / 'message_private.json'

    if group:
        try:
            with open(file_group, 'r') as f:
                data_group = json.load(f)
            return data_group

        except:
            return {"status": "None"}
    else:
        try:
            with open(file_private, 'r') as f:
                data_private = json.load(f)
            return data_private

        except:
            return {"status": "None"}