-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b0e2fe
commit 1cd926a
Showing
4 changed files
with
71 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,83 @@ | ||
from nonebot.log import logger | ||
from typing import List | ||
from typing import List, Dict | ||
import os | ||
from pathlib import Path | ||
from pydantic import BaseModel, Extra | ||
import nonebot | ||
try: | ||
import ujson as json | ||
except ModuleNotFoundError: | ||
import json | ||
|
||
class AntiFlashConfig(BaseModel, extra=Extra.ignore): | ||
|
||
anti_flash_on: bool = True | ||
anti_flash_group: List[str] = [] | ||
anti_flash_path: str = os.path.dirname(__file__) | ||
|
||
class AntiFlashHandler: | ||
|
||
def __init__(self, config): | ||
self.on: bool = config.anti_flash_on | ||
self.group_on: List[str] = config.anti_flash_group | ||
self.group_on: List[str] = list(set(config.anti_flash_group)) | ||
self.path: Path = Path(config.anti_flash_path) / "config.json" | ||
|
||
if not self.on: | ||
logger.warning(f"已全局禁用群聊反闪照,指令:开启/禁用反闪照") | ||
|
||
if len(self.group_on) == 0 and self.on == True: | ||
logger.warning(f"Anti-flash on but group is empty!") | ||
else: | ||
if not self.path.exists(): | ||
with open(self.path, "w", encoding="utf-8") as f: | ||
f.write(json.dumps(dict())) | ||
f.close() | ||
|
||
self.check_default_groups() | ||
|
||
def check_default_groups(self) -> None: | ||
''' | ||
初始化更新默认开启群组 | ||
''' | ||
file = self.load_json(self.path) | ||
for gid in self.group_on: | ||
file.update({gid: True}) | ||
|
||
self.save_json(self.path, file) | ||
|
||
def check_permission(self, gid: str) -> bool: | ||
return False if gid not in self.group_on else True | ||
''' | ||
检测群组是否已开启功能 | ||
''' | ||
file = self.load_json(self.path) | ||
return False if gid not in file else file[gid] | ||
|
||
def add_group(self, gid: str) -> None: | ||
''' | ||
已有则不进行操作,说明已启用 | ||
Update覆盖 | ||
''' | ||
if gid not in self.group_on: | ||
self.group_on.append(gid) | ||
file = self.load_json(self.path) | ||
file.update({gid: True}) | ||
self.save_json(self.path, file) | ||
|
||
def remove_group(self, gid: str) -> None: | ||
''' | ||
本不存在则不进行操作,说明已禁用 | ||
尝试Pop | ||
''' | ||
file = self.load_json(self.path) | ||
try: | ||
self.group_on.remove(gid) | ||
except ValueError: | ||
file.pop(gid) | ||
except KeyError: | ||
pass | ||
|
||
self.save_json(self.path, file) | ||
|
||
def save_json(self, file: Path, data: Dict[str, bool]) -> None: | ||
if file.exists(): | ||
with open(file, 'w', encoding='utf-8') as f: | ||
json.dump(data, f, ensure_ascii=False, indent=4) | ||
|
||
def load_json(self, file: Path) -> Dict[str, bool]: | ||
if file.exists(): | ||
with open(file, "r", encoding="utf-8") as f: | ||
return json.load(f) | ||
|
||
config: AntiFlashConfig = AntiFlashConfig.parse_obj(nonebot.get_driver().config.dict()) | ||
handler = AntiFlashHandler(config) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters