-
Notifications
You must be signed in to change notification settings - Fork 411
/
setu.py
53 lines (43 loc) · 1.54 KB
/
setu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import os
import random
from nonebot.exceptions import CQHttpError
from hoshino import R, Service, priv
from hoshino.util import FreqLimiter, DailyNumberLimiter
_max = 5
EXCEED_NOTICE = f'您今天已经冲过{_max}次了,请明早5点后再来!'
_nlmt = DailyNumberLimiter(_max)
_flmt = FreqLimiter(5)
sv = Service('setu', manage_priv=priv.SUPERUSER, enable_on_default=True, visible=False)
setu_folder = R.img('setu/').path
def setu_gener():
while True:
filelist = os.listdir(setu_folder)
random.shuffle(filelist)
for filename in filelist:
if os.path.isfile(os.path.join(setu_folder, filename)):
yield R.img('setu/', filename)
setu_gener = setu_gener()
def get_setu():
return setu_gener.__next__()
@sv.on_rex(r'不够[涩瑟色]|[涩瑟色]图|来一?[点份张].*[涩瑟色]|再来[点份张]|看过了|铜')
async def setu(bot, ev):
"""随机叫一份涩图,对每个用户有冷却时间"""
uid = ev['user_id']
if not _nlmt.check(uid):
await bot.send(ev, EXCEED_NOTICE, at_sender=True)
return
if not _flmt.check(uid):
await bot.send(ev, '您冲得太快了,请稍候再冲', at_sender=True)
return
_flmt.start_cd(uid)
_nlmt.increase(uid)
# conditions all ok, send a setu.
pic = get_setu()
try:
await bot.send(ev, pic.cqcode)
except CQHttpError:
sv.logger.error(f"发送图片{pic.path}失败")
try:
await bot.send(ev, '涩图太涩,发不出去勒...')
except:
pass