zhenxun_bot/plugins/yiqing/__init__.py
2021-07-30 21:21:51 +08:00

59 lines
2.0 KiB
Python

from nonebot import on_command
from .data_source import get_yiqing_data, clear_data
from services.log import logger
from nonebot.adapters.cqhttp import Bot, MessageEvent, GroupMessageEvent
from nonebot.typing import T_State
from .config import city_list
from utils.utils import scheduler
__plugin_name__ = "疫情查询"
__plugin_usage__ = "查询疫情帮助:\n\t对我说 查询疫情 省份/城市,我会回复疫情的实时数据\n\t示例: 查询疫情 温州"
yiqing = on_command("疫情", aliases={"查询疫情", "疫情查询"}, priority=5, block=True)
@yiqing.handle()
async def _(bot: Bot, event: MessageEvent, state: T_State):
msg = str(event.get_message()).strip()
if not msg or msg in ["帮助"]:
await yiqing.finish(__plugin_usage__)
if msg:
if msg in city_list.keys():
province = msg
city = ""
else:
for key in city_list.keys():
if msg in city_list.get(key):
province = key
city = msg
break
else:
await yiqing.finish(__plugin_usage__)
try:
result = await get_yiqing_data(province, city)
if result:
await yiqing.send(result)
logger.info(
f"(USER {event.user_id}, GROUP "
f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'}) 查询疫情:"
+ result
)
else:
await yiqing.send("查询失败!!!!", at_sender=True)
logger.info(
f"(USER {event.user_id}, GROUP "
f"{event.group_id if isinstance(event, GroupMessageEvent) else 'private'}) 查询疫情失败"
)
except UnboundLocalError:
await yiqing.finish("参数正确吗?只要一个参数啊", at_sender=True)
@scheduler.scheduled_job(
"cron",
hour=0,
minute=1,
)
async def _():
clear_data()