LittlePaimon/Paimon_Plugins/order.py

58 lines
1.9 KiB
Python
Raw Normal View History

import random
from littlepaimon_utils import aiorequests
from littlepaimon_utils.tools import FreqLimiter
from nonebot import on_command
from nonebot.adapters.onebot.v11 import Message, MessageEvent, MessageSegment
from nonebot.params import CommandArg
2022-06-21 16:26:13 +08:00
from nonebot.plugin import PluginMetadata
__plugin_meta__ = PluginMetadata(
name="点餐",
description="点餐查看食物图片",
usage=(
"点餐 食物名"
),
extra={
'type': '娱乐',
'range': ['private', 'group', 'guild'],
"author": "惜月 <277073121@qq.com>",
"version": "1.0.0",
},
)
2022-06-09 21:57:56 +08:00
order_pic = on_command('点菜', aliases={'点餐', '食谱', '我想吃'}, priority=13, block=True)
2022-06-09 21:57:56 +08:00
order_pic.__paimon_help__ = {
"usage": "点餐<食品名>",
"introduce": "群主,来一份派蒙!",
"priority": 11
}
order_lmt = FreqLimiter(10)
@order_pic.handle()
async def order_pic_handler(event: MessageEvent, msg=CommandArg()):
msg = str(msg).strip()
if not msg:
return
if not order_lmt.check(event.user_id):
await order_pic.finish(f'点餐冷却ing(剩余{order_lmt.left_time(event.user_id)}秒)')
else:
url = 'https://api.iyk0.com/shipu/?key=' + msg
res = await aiorequests.get(url=url)
res = res.json()
if res['code'] == 202:
await order_pic.finish('没有找到这种食品哦~')
order_lmt.start_cd(event.user_id, 10)
number = random.randint(1, 3)
recipe_list = []
for i in range(0, number):
recipe = random.choice(res['data'])
if recipe not in recipe_list:
recipe_list.append(recipe)
mes = Message()
for recipe in recipe_list:
mes += MessageSegment.text(recipe['name'] + '\n') + MessageSegment.image(recipe['img']) + '\n'
await order_pic.finish(mes, at_sender=True)