Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新增自定义群命令冷却 #50

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from hoshino import HoshinoBot, Service, priv
from hoshino.aiorequests import run_sync_func
from hoshino.typing import CQEvent, MessageSegment, Message
from hoshino.util import DailyNumberLimiter, FreqLimiter
from meme_generator.download import check_resources
from meme_generator.exception import (
TextOverLength,
Expand All @@ -26,7 +27,8 @@
from meme_generator.utils import TextProperties, render_meme_list
from pypinyin import Style, pinyin

from .config import memes_prompt_params_error, meme_command_start
from .config import memes_prompt_params_error, meme_command_start, group_lmt, user_single_limit, SINGLE_EXCEED_NOTICE, \
symmetry_lmt, SYMMETRY_EXCEED_NOTICE
from .data_source import ImageSource, User, UserInfo
from .depends import split_msg_v11
from .exception import NetworkError, PlatformUnsupportError
Expand All @@ -35,6 +37,11 @@

memes_cache_dir = Path(os.path.join(os.path.dirname(__file__), "memes_cache_dir"))

# 生成表情包的群命令冷却
lmt = FreqLimiter(group_lmt)
single_limit = DailyNumberLimiter(user_single_limit)
symmetry_limit = DailyNumberLimiter(symmetry_lmt)

sv_help = """
[表情包制作] 发送全部功能帮助
[表情帮助 + 表情] 发送选定表情功能帮助
Expand Down Expand Up @@ -379,6 +386,19 @@ async def handle(bot: HoshinoBot, ev: CQEvent):
)
return

if not lmt.check(ev.group_id):
await bot.send(ev, f'头像表情包功能冷却中(剩余 {int(lmt.left_time(ev.group_id)) + 1}秒)', at_sender=True)
return
if not single_limit.check(ev.user_id):
await bot.send(ev, SINGLE_EXCEED_NOTICE, at_sender=True)
return
if '对称' in str(ev.message):
if not symmetry_limit.check(ev.user_id):
await bot.send(ev, SYMMETRY_EXCEED_NOTICE, at_sender=True)
return
symmetry_limit.increase(ev.user_id, 1)
lmt.start_cd(ev.group_id)
single_limit.increase(ev.user_id, 1)
await process(bot, ev, meme, image_sources, texts, users, args)


Expand Down
6 changes: 6 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@
baidu_trans_appid: str = ""
baidu_trans_apikey: str = ""

group_lmt: int = 10 # 生成表情包的群命令冷却 | 默认1秒
user_single_limit: int = 10 # 每日每人的次数限制
SINGLE_EXCEED_NOTICE: str = "您今天已经使用过10次头像表情包功能了,请明早5点后再来哦!"
symmetry_lmt: int = 1 # 对称功能次数限制
SYMMETRY_EXCEED_NOTICE: str = "您今天已经使用过1次对称表情包了,请明早5点后再来哦!"

meme_config.translate.baidu_trans_appid = baidu_trans_appid
meme_config.translate.baidu_trans_apikey = baidu_trans_apikey
meme_config.dump()