summaryrefslogtreecommitdiff
path: root/utils/utils_rule/__init__.py
blob: 333fb5dc0b95103c4fbd3c595e70e02588cd25da (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   __init__.py
@Time    :   2020/11/06 19:27:00
@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 nonebot.rule import Rule
from nonebot.typing import Bot, Event


def check_banlist() -> Rule:
    '''
    检查目标是否存在于封禁名单

    :return: bool
    '''
    async def _chech_banlist(bot: Bot, event: Event, state: dict) -> bool:
        # 获取目标信息
        user = str(event.user_id)
        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'

        # 检查文件是否存在,如不存在,自动创建并写入默认值
        if not file_user.is_file():
            file = open(file_user, 'w')
            file.write(json.dumps({}))
            file.close()

        if not file_group.is_file():
            file = open(file_group, 'w')
            file.write(json.dumps({}))
            file.close()

        # 读取文件
        with open(file_user, 'r') as f:
            data_user = json.load(f)

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

        # 判断目标
        if user:
            if user not in data_user:
                if group:
                    if group not in data_group:
                        return True
                    else:
                        return False
                else:
                    return True
            else:
                return False

        elif group:
            if group not in data_group:
                if user:
                    if user not in data_user:
                        return True
                    else:
                        return False
                else:
                    return True
            else:
                return False
        else:
            return False

    return Rule(_chech_banlist)


def check_switch(func_name: str) -> Rule:
    '''
    检查目标功能是否开启

    :return: bool
    '''
    async def _check_switch(bot: Bot, event: Event, state: dict) -> bool:
        # 获取目标信息
        group = str(event.group_id)

        # 文件目录
        file_switch_all = Path('.') / 'utils' / 'utils_rule' / 'switch.json'
        file_switch_alone = 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"

            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 = {}

            # 检查目标文件夹是否存在,如不存在自动创建
            try:
                os.mkdir(
                    Path('.') / 'ATRI' / 'data' / 'data_Group' / f'{group}')

            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"

            file = open(file_switch_alone, 'w')
            file.write(json.dumps(data_switch_alone))
            file.close()

        # 读取文件
        with open(file_switch_all, 'r') as f:
            data_all = json.load(f)

        with open(file_switch_alone, 'r') as f:
            data_alone = json.load(f)

        # 判断目标
        if data_all[func_name] == "True":
            if data_alone[func_name] == "True":
                return True
            else:
                return False
        else:
            await bot.send(event, f"Service-{func_name} has been closed.")
            return False

    return Rule(_check_switch)