Skip to content

Commit

Permalink
Merge pull request #54 from Sevenyine/dev
Browse files Browse the repository at this point in the history
增加“最大字符换行比率”配置
  • Loading branch information
LambdaYH committed Sep 3, 2023
2 parents f155721 + f261ba9 commit 58d0a91
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
- `功能冷却`:插件默认 30 秒冷却,可在`data/bottle/permissionsList.json`中修改`cooldownTime`
- 白名单优先级高于黑名单和冷却名单

## 最大字符数量和换行数量配置
## 防刷屏配置
### 最大字符数量
- 最大字符数量配置用于限制漂流瓶最大可投掷的字数。
- 使用`len()`函数,所有汉字、英文字母、空格均被算作1。
Expand All @@ -52,6 +52,16 @@
- 最大换行数量,代表漂流瓶内容最大可拥有换行的数量。
- 可防止用户不断换行,没有触发最大字符数量限制,但是还会刷屏。
- 该配置项设置为0时不限制换行数量。默认设置为0。
### 最大字符换行比率
- 最大字符换行比率,是漂流瓶总字符数量和其换行数量的比值。
- 仅当漂流瓶换行数量在“最大换行数量”之下时触发,可用于检测是否涉及到无意义换行刷屏。
- 该配置项的数字(x)代表每x个字符(包含)就可以有1个换行。
- 例如我发了一个字符数量有100的文本,同时拥有10个换行。那么我需要将该配置设置为10和10以下的数字来允许这个漂流瓶内容被发出。
- 该配置项设置为0时不限制字符换行比率。默认设置为0,推荐设置为9以上的数值。
- 效果图如下(效果图中,该配置项为10)
![89fbe85ef97f9fe7d87b7526462e62cc](https://github.com/Sevenyine/nonebot_plugin_bottle/assets/65720409/7be1ec95-4ae7-4c08-a14b-4bdc72782093)
![69ca733b21ad6c38052fe1e6a9136b92](https://github.com/Sevenyine/nonebot_plugin_bottle/assets/65720409/86a9eaa1-194e-42da-b039-b74203ebabf4)


## 文字审核API配置(可选`百度审核``简单屏蔽词审核`
- 百度审核
Expand All @@ -77,6 +87,7 @@
| 是否缓存图片 | nonebot_plugin_bottle_local_storage | bool | True |
| 漂流瓶最大字符数量 | nonebot_plugin_bottle_max_length | int | 0 |
| 漂流瓶最大换行数量 | nonebot_plugin_bottle_max_return | int | 0 |
| 漂流瓶字符换行比率 | nonebot_plugin_bottle_rt_rate | int | 0 |


## 更新日志
Expand Down Expand Up @@ -139,4 +150,4 @@
![image](https://user-images.githubusercontent.com/97968466/191049794-1b409436-fd70-43d9-8dcb-3575e82fd69b.png)
![image](https://user-images.githubusercontent.com/97968466/213113862-e6c7568b-8686-4e97-8f83-7354ff1cb704.png)
![image](https://user-images.githubusercontent.com/97968466/191052704-1b5ec89d-7a49-40d6-a5d9-b0a0171c730e.png)
![image](https://user-images.githubusercontent.com/97968466/191049649-2e8d8555-f285-470f-9f7b-f5a0994341ee.png)
![image](https://user-images.githubusercontent.com/97968466/191049649-2e8d8555-f285-470f-9f7b-f5a0994341ee.png)
14 changes: 10 additions & 4 deletions nonebot_plugin_bottle/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
)

from .model import Bottle
from .config import Config, maxlen, maxrt
from .config import Config, maxlen, maxrt, rtrate
from .data_source import (
ba,
text_audit,
Expand Down Expand Up @@ -199,6 +199,8 @@ async def _(

message_id = event.message_id

ba.add("cooldown", event.user_id)

if "__has_content__" not in state and message_text in cancel:
await throw.finish(MessageSegment.reply(message_id)+"已取消扔漂流瓶操作。")

Expand All @@ -207,13 +209,17 @@ async def _(
MessageSegment.reply(message_id)
+ f"您漂流瓶中的字符数量超过了最大字符限制:{maxlen}。您可以尝试减少漂流瓶内容。\n当前字符数量:{msg_len}"
)
ba.add("cooldown", event.user_id)
if maxrt != 0 and ((rt_cnt := message_text.count("\n")) > maxrt):
await throw.finish(
MessageSegment.reply(message_id)
+ f"您漂流瓶中的换行数量超过了最大换行限制:{maxrt}。您可尝试减少换行数量。\n当前换行数量:{rt_cnt}"
)
ba.add("cooldown", event.user_id)
else:
if rtrate != 0 and (msg_len / rt_cnt) <= rtrate:
await throw.finish(
MessageSegment.reply(message_id)
+ f"您漂流瓶中的字符换行比率超过了最大字符换行比率限制。\n字符换行比率,是您发送的漂流瓶总字符数量和换行的比值。为了防止刷屏,请您尝试减少漂流瓶的换行数量或增加有意义漂流瓶字符。"
)
audit = await text_audit(text=message_text)
if not audit == "pass":
if audit == "Error":
Expand Down Expand Up @@ -287,7 +293,7 @@ async def _(
ba.add("cooldown", event.user_id)
await get.send(
MessageSegment.reply(message_id)
+ f"【漂流瓶No.{bottle.id}\n来自群:{group_name}】的“{user_name}”!\n"
+ f"【漂流瓶No.{bottle.id}\n来自{group_name}】的“{user_name}”!\n"
+ f"时间:{bottle.time.strftime('%Y-%m-%d')}\n"
+ f"内容:\n"
+ deserialize_message(bottle.content)
Expand Down
5 changes: 4 additions & 1 deletion nonebot_plugin_bottle/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ class Config(BaseModel, extra=Extra.ignore):
nonebot_plugin_bottle_max_length: int = 0
# 最大换行数量
nonebot_plugin_bottle_max_return: int = 0
# 字符与换行的比率
nonebot_plugin_bottle_rt_rate: int = 0


config = Config.parse_obj(get_driver().config.dict())
api_key = config.nonebot_plugin_bottle_api_key
secret_key = config.nonebot_plugin_bottle_secret_key
local_storage = config.nonebot_plugin_bottle_local_storage
maxlen = config.nonebot_plugin_bottle_max_length
maxrt = config.nonebot_plugin_bottle_max_return
maxrt = config.nonebot_plugin_bottle_max_return
rtrate = config.nonebot_plugin_bottle_rt_rate

0 comments on commit 58d0a91

Please sign in to comment.