Skip to content

Commit

Permalink
v0.2.3🥳
Browse files Browse the repository at this point in the history
  • Loading branch information
KafCoppelia committed Apr 19, 2022
1 parent 3b0e2fe commit 1cd926a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 32 deletions.
31 changes: 15 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ _🎇 反闪照 🎇_
<p align="center">

<a href="https://github.com/MinatoAquaCrews/nonebot_plugin_antiflash/blob/beta/LICENSE">
<img src="https://img.shields.io/badge/license-MIT-informational">
<img src="https://img.shields.io/github/license/MinatoAquaCrews/nonebot_plugin_antiflash?color=blue">
</a>

<a href="https://github.com/nonebot/nonebot2">
<img src="https://img.shields.io/badge/nonebot2-2.0.0beta.2-green">
</a>

<a href="">
<img src="https://img.shields.io/badge/release-v0.2.2-orange">
<a href="https://github.com/MinatoAquaCrews/nonebot_plugin_antiflash/releases/tag/v0.2.3">
<img src="https://img.shields.io/github/v/release/MinatoAquaCrews/nonebot_plugin_antiflash?color=orange">
</a>

</p>
Expand All @@ -28,26 +28,29 @@ _🎇 反闪照 🎇_

## 版本

v0.2.2
v0.2.3

⚠ 适配nonebot2-2.0.0beta.2
⚠ 适配nonebot2-2.0.0beta.2

👉 适配alpha.16版本参见[alpha.16分支](https://github.com/MinatoAquaCrews/nonebot_plugin_antiflash/tree/alpha.16)

[更新日志](https://github.com/MinatoAquaCrews/nonebot_plugin_antiflash/releases/tag/v0.2.2)
[更新日志](https://github.com/MinatoAquaCrews/nonebot_plugin_antiflash/releases/tag/v0.2.3)

## 安装

1. 通过`pip``nb`安装;

2.`env`内设置:

```python
ANTI_FLASH_ON=true # 全局开关
ANTI_FLASH_GROUP=["123456789", "987654321"] # 默认开启的群聊,但可通过指令开关
```
```python
ANTI_FLASH_ON=true # 全局开关
ANTI_FLASH_GROUP=["123456789", "987654321"] # 默认开启的群聊,但可通过指令开关
ANTI_FLASH_PATH="your-path-to-config.json" # 配置文件路径,默认同插件代码路径
```

**修改** 全局开启时,群聊列表可以为空。
`ANTI_FLASH_GROUP`会在每次初始化时写入配置文件,在群组启用反闪照,可通过指令更改。

**修改** 配置文件即读即改,可后台修改。

## 功能

Expand All @@ -57,8 +60,4 @@ ANTI_FLASH_GROUP=["123456789", "987654321"] # 默认开启的群聊,但可通

## 命令

开启/启用/禁用反闪照

## 本插件改自

忘记出处了,找到了马上补上。
开启/启用/禁用反闪照
6 changes: 3 additions & 3 deletions nonebot_plugin_antiflash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
from .config import handler

__anti_flash_vsrsion__ = "v0.2.2"
__anti_flash_vsrsion__ = "v0.2.3"
__anti_flash_notes__ = f'''
群聊反闪照 {__anti_flash_vsrsion__}
开启/启用/禁用反闪照
Expand All @@ -16,8 +16,8 @@ async def _checker(event: GroupMessageEvent) -> bool:
msg = str(event.get_message())
return True if 'type=flash' in msg and handler.on else False

anti_flash_on = on_command(cmd="开启反闪照", aliases={"启用反闪照"}, permission=SUPERUSER|GROUP_ADMIN|GROUP_OWNER, priority=10, block=True)
anti_flash_off = on_command(cmd="禁用反闪照", permission=SUPERUSER|GROUP_ADMIN|GROUP_OWNER, priority=10, block=True)
anti_flash_on = on_command(cmd="开启反闪照", aliases={"启用反闪照"}, permission=SUPERUSER | GROUP_ADMIN| GROUP_OWNER, priority=10, block=True)
anti_flash_off = on_command(cmd="禁用反闪照", permission=SUPERUSER | GROUP_ADMIN | GROUP_OWNER, priority=10, block=True)

flashimg = on_message(priority=1, rule=Rule(_checker))
@flashimg.handle()
Expand Down
64 changes: 52 additions & 12 deletions nonebot_plugin_antiflash/config.py
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)
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nonebot-plugin-antiflash"
version = "0.2.2"
version = "0.2.3"
description = "Anti flash pictures in groups"
authors = ["KafCoppelia <k740677208@gmail.com>"]
license = "MIT"
Expand Down

0 comments on commit 1cd926a

Please sign in to comment.