diff --git a/README.md b/README.md index 2b74732..392a8ff 100644 --- a/README.md +++ b/README.md @@ -71,12 +71,15 @@ pip install nonebot-plugin-nsfw[nsfw-model] ## 插件配置项 -| | 默认值 | 可选值 | 说明 | -| --------------- | --------------------------------- | ------------------------------ | ------------------------------------- | -| model | "safety_checker" | "safety_checker", "nsfw_model" | | -| device | "cpu" | "cpu", "cuda", etc. | | -| withdraw | True | True, False | 撤回检测到 NSFW 图片的消息 | -| nsfw_model_path | cwd() / nsfw_mobilenet2_v1.2.0.h5 | .h5 or SavedModel path | nsfw-model 模型路径,没配置则自动下载 | +| | 默认值 | 可选值 | 说明 | +| ------------------------ | --------------------------------- | ------------------------------ | -------------------------------------------------- | +| nsfw\_\_model | "safety_checker" | "safety_checker", "nsfw_model" | | +| nsfw\_\_device | "cpu" | "cpu", "cuda", etc. | | +| nsfw\_\_withdraw | True | True, False | 撤回检测到 NSFW 图片的消息 | +| nsfw\_\_nsfw_model_path | cwd() / nsfw_mobilenet2_v1.2.0.h5 | .h5 or SavedModel path | nsfw-model 模型路径,没配置则自动下载 | +| nsfw\_\_warning_capacity | 3 | 正整数 | 一天内警告 N 次后禁言,0 不警告,ban=True 直接禁言 | +| nsfw\_\_ban | True | True, False | 是否启用禁言 | +| nsfw\_\_ban_time | 1800 | 正整数 | 禁言时长,单位为秒数 | 更多配置正在开发中... diff --git a/nonebot_plugin_nsfw/__init__.py b/nonebot_plugin_nsfw/__init__.py index 4b76eb2..36fc4ba 100644 --- a/nonebot_plugin_nsfw/__init__.py +++ b/nonebot_plugin_nsfw/__init__.py @@ -32,4 +32,9 @@ async def _( await bot.delete_msg(message_id=event.message_id) if config.warning_capacity != 0: user.warning_count += 1 - await bot.send(event, f"不许色色,警告 {user.warning_count} 次!", at_sender=True) + if config.ban == True and user.warning_count > config.warning_capacity: + await bot.set_group_ban( + group_id=event.group_id, user_id=event.user_id, duration=config.ban_time + ) + else: + await bot.send(event, f"涩涩哒咩,警告 {user.warning_count} 次!", at_sender=True) diff --git a/nonebot_plugin_nsfw/config.py b/nonebot_plugin_nsfw/config.py index e4b7625..f8d28bf 100644 --- a/nonebot_plugin_nsfw/config.py +++ b/nonebot_plugin_nsfw/config.py @@ -3,7 +3,7 @@ import nonebot from nonebot import logger -from pydantic import BaseModel, NonNegativeInt +from pydantic import BaseModel, NonNegativeInt, PositiveInt DEFAULT_NSFW_MODEL_PATH = str(Path.cwd() / "nsfw_mobilenet2_v1.2.0.h5") DEFAULT_NSFW_MODEL_URI = "https://github.com/iyume/nonebot-plugin-nsfw/releases/download/v0.1/nsfw_mobilenet2_v1.2.0.h5" @@ -11,7 +11,7 @@ T_AVAILABLE_MODEL = Literal["safety_checker", "nsfw_model"] -class Config(BaseModel): +class PluginScopedConfig(BaseModel): model: T_AVAILABLE_MODEL = "nsfw_model" """要使用的模型。默认为 NSFW Model。""" @@ -25,13 +25,21 @@ class Config(BaseModel): 默认值: `cwd()/nsfw_mobilenet2_v1.2.0.h5` """ - warning_capacity: NonNegativeInt = 5 - """一天内警告 N 次后禁言,0 表示不警告,ban=True 则直接禁言。默认为 5.""" + warning_capacity: NonNegativeInt = 3 + """一天内警告 N 次后禁言,0 表示不警告,ban=True 则直接禁言。默认为 3.""" - ban: bool = False + ban: bool = True + """是否禁言。默认为 True。""" + ban_time: PositiveInt = 30 * 60 # maybe list of increasing time + """禁言时长秒数。默认为 1800。""" -config = Config.parse_obj(nonebot.get_driver().config) + +class PluginConfig(BaseModel): + nsfw: PluginScopedConfig + + +config = PluginConfig.parse_obj(nonebot.get_driver().config).nsfw if config.nsfw_model_path is not None and config.model != "nsfw_model": logger.warning("Provided nsfw_model_path without using its model") diff --git a/nonebot_plugin_nsfw/deps.py b/nonebot_plugin_nsfw/deps.py index 7664480..cea9a41 100644 --- a/nonebot_plugin_nsfw/deps.py +++ b/nonebot_plugin_nsfw/deps.py @@ -67,10 +67,6 @@ def refresh(self) -> None: self.date_ = today self.warning_count = 0 - @property - def should_ban(self) -> bool: - return self.warning_count > config.warning_capacity - # simple database users: Dict[str, User] = {} diff --git a/pyproject.toml b/pyproject.toml index 4cb6547..c41fc46 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "nonebot-plugin-nsfw" -version = "0.1.0" +version = "0.2" description = "群聊 NSFW 图片检测插件,带有撤回、禁言等功能。使用 Safety Checker / NSFW Model。" authors = [{ name = "iyume", email = "iyumelive@gmail.com" }] requires-python = ">=3.8"