From 7cae371b51a14c626ce184987eea2392e15430b9 Mon Sep 17 00:00:00 2001 From: Kyomotoi <1172294279@qq.com> Date: Sat, 7 Nov 2020 13:54:57 +0800 Subject: [Update] --- utils/utils_rule/__init__.py | 154 +++++++++++++++++++++++++++++++++++ utils/utils_rule/ban_list_group.json | 1 + utils/utils_rule/ban_list_user.json | 1 + utils/utils_rule/switch.json | 1 + 4 files changed, 157 insertions(+) create mode 100644 utils/utils_rule/__init__.py create mode 100644 utils/utils_rule/ban_list_group.json create mode 100644 utils/utils_rule/ban_list_user.json create mode 100644 utils/utils_rule/switch.json (limited to 'utils/utils_rule') diff --git a/utils/utils_rule/__init__.py b/utils/utils_rule/__init__.py new file mode 100644 index 0000000..aa7295e --- /dev/null +++ b/utils/utils_rule/__init__.py @@ -0,0 +1,154 @@ +#!/usr/bin/env python3 +# -*- encoding: utf-8 -*- +''' +@File : __init__.py +@Time : 2020/11/06 19:27:00 +@Author : Kyomotoi +@Contact : kyomotoiowo@gmail.com +@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}') + 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" + + 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) \ No newline at end of file diff --git a/utils/utils_rule/ban_list_group.json b/utils/utils_rule/ban_list_group.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/utils/utils_rule/ban_list_group.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/utils/utils_rule/ban_list_user.json b/utils/utils_rule/ban_list_user.json new file mode 100644 index 0000000..9e26dfe --- /dev/null +++ b/utils/utils_rule/ban_list_user.json @@ -0,0 +1 @@ +{} \ No newline at end of file diff --git a/utils/utils_rule/switch.json b/utils/utils_rule/switch.json new file mode 100644 index 0000000..88bc6b9 --- /dev/null +++ b/utils/utils_rule/switch.json @@ -0,0 +1 @@ +{"anime-setu": "True", "anime-pic-search": "True", "anime-vid-search": "True", "all-off-anime-setu": "True", "ai-face": "True", "pixiv-pic-search": "True", "pixiv-author-search": "True", "pixiv-rank": "True", "one-key-adult": "True"} \ No newline at end of file -- cgit v1.2.3