zhenxun_bot/plugins/yiqing/__init__.py

59 lines
2.0 KiB
Python
Raw Normal View History

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