-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwhattoeat.py
67 lines (61 loc) · 2.26 KB
/
whattoeat.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import hoshino, random, os, re, filetype
from hoshino import Service, R, priv, aiorequests
from hoshino.config import RES_DIR
from hoshino.typing import CQEvent
from hoshino.util import DailyNumberLimiter
sv_help = '''
[今天吃什么] 看看今天吃啥
'''.strip()
sv = Service(
name = '今天吃什么', #功能名
use_priv = priv.NORMAL, #使用权限
manage_priv = priv.ADMIN, #管理权限
visible = True, #可见性
enable_on_default = True, #默认启用
bundle = '娱乐', #分组归类
help_ = sv_help #帮助说明
)
_lmt = DailyNumberLimiter(5)
imgpath = os.path.join(os.path.expanduser(RES_DIR), 'img', 'foods')
@sv.on_rex(r'^(今天|[早中午晚][上饭餐午]|夜宵)吃(什么|啥|点啥)')
async def net_ease_cloud_word(bot,ev:CQEvent):
uid = ev.user_id
if not _lmt.check(uid):
await bot.finish(ev, '你今天吃的已经够多的了!', at_sender=True)
match = ev['match']
time = match.group(1).strip()
food = random.choice(os.listdir(imgpath))
name = food.split('.')
to_eat = f'{time}去吃{name[0]}吧~\n'
try:
foodimg = R.img(f'foods/{food}').cqcode
to_eat += str(foodimg)
except Exception as e:
hoshino.logger.error(f'读取食物图片时发生错误{type(e)}')
await bot.send(ev, to_eat, at_sender=True)
_lmt.increase(uid)
async def download_async(url: str, name: str):
resp= await aiorequests.get(url, stream=True)
if resp.status_code == 404:
raise ValueError('文件不存在')
content = await resp.content
try:
extension = filetype.guess_mime(content).split('/')[1]
except:
raise ValueError('不是有效文件类型')
abs_path = os.path.join(imgpath, f'{name}.{extension}')
with open(abs_path, 'wb') as f:
f.write(content)
@sv.on_prefix(('添菜','添加菜品'))
@sv.on_suffix(('添菜','添加菜品'))
async def add_food(bot,ev:CQEvent):
if not priv.check_priv(ev, priv.SUPERUSER):
return
food = ev.message.extract_plain_text().strip()
ret = re.search(r"\[CQ:image,file=(.*)?,url=(.*)\]", str(ev.message))
if not ret:
await bot.send(ev,'请附带美食图片~')
return
url = ret[2]
await download_async(url, food)
await bot.send(ev,'食谱已增加~')