LittlePaimon/Paimon_Calendar/__init__.py

149 lines
5.7 KiB
Python
Raw Normal View History

2022-05-29 12:45:41 +08:00
from nonebot import require, get_bot, on_command, logger
2022-06-21 20:37:54 +08:00
from nonebot.adapters.onebot.v11 import MessageEvent, Message, MessageSegment
2022-05-27 21:13:59 +08:00
from nonebot.params import CommandArg
2022-06-21 16:26:13 +08:00
from nonebot.plugin import PluginMetadata
2022-05-27 21:13:59 +08:00
2022-05-29 12:45:41 +08:00
from utils.file_handler import load_json, save_json
from utils.message_util import MessageBuild, get_message_id
2022-05-28 23:30:59 +08:00
from .generate import *
2022-05-27 21:13:59 +08:00
import re
2022-06-21 16:26:13 +08:00
require('nonebot_plugin_apscheduler')
from nonebot_plugin_apscheduler import scheduler
2022-05-27 21:13:59 +08:00
2022-06-21 16:26:13 +08:00
__plugin_meta__ = PluginMetadata(
name="原神日历",
description="查看原神活动日历",
usage=(
"原神日历 : 查看本群订阅服务器日历\n"
"原神日历 on 时间/off : 订阅/取消订阅指定服务器的日历推送\n"
),
extra={
'type': '原神Wiki',
'range': ['private', 'group', 'guild'],
"author": "nicklly <1134741727@qq.com>",
"version": "1.0.0",
},
)
2022-06-09 21:57:56 +08:00
2022-06-21 20:37:54 +08:00
calendar = on_command('原神日历', aliases={"原神日历", '原神日程', 'ysrl', 'ysrc'}, priority=24, block=True)
2022-06-09 21:57:56 +08:00
calendar.__paimon_help__ = {
2022-06-21 16:26:13 +08:00
"usage": "原神日历",
2022-06-09 21:57:56 +08:00
"introduce": "查看原神活动日历后加on时间/off可以开启定时推送",
2022-06-21 16:26:13 +08:00
"priority": 99
2022-06-09 21:57:56 +08:00
}
2022-05-27 21:13:59 +08:00
2022-05-29 12:45:41 +08:00
async def send_calendar(push_id, push_data):
try:
if push_data['type'] == 'private':
api = 'send_private_msg'
data = {'user_id': push_id}
elif push_data['type'] == 'guild':
api = 'send_guild_channel_msg'
2022-05-29 12:51:50 +08:00
data = {'channel_id': push_id, 'guild_id': push_data['guild_id']}
2022-05-27 21:13:59 +08:00
else:
2022-05-29 12:45:41 +08:00
api = 'send_group_msg'
data = {'group_id': push_id}
2022-05-27 21:13:59 +08:00
2022-05-29 12:45:41 +08:00
for server in push_data['server_list']:
im = await generate_day_schedule(server)
2022-06-21 20:37:54 +08:00
data['message'] = MessageSegment.image(im)
2022-05-29 12:45:41 +08:00
await get_bot().call_api(api, **data)
logger.info(f'{push_data["type"]}{push_id}的原神日历推送成功')
except Exception as e:
logger.exception(f'{push_data["type"]}{push_id}的原神日历推送失败:{e}')
2022-05-27 21:13:59 +08:00
def update_group_schedule(group_id, group_data):
group_id = str(group_id)
if group_id not in group_data:
return
scheduler.add_job(
func=send_calendar,
trigger='cron',
args=(group_id, group_data),
id=f'genshin_calendar_{group_id}',
replace_existing=True,
hour=group_data[group_id]['hour'],
minute=group_data[group_id]['minute'],
misfire_grace_time=10
)
@calendar.handle()
2022-05-29 12:45:41 +08:00
async def _(event: MessageEvent, msg: Message = CommandArg()):
2022-05-27 21:13:59 +08:00
server = 'cn'
2022-05-29 12:45:41 +08:00
msg = msg.extract_plain_text().strip()
2022-05-27 21:13:59 +08:00
2022-05-29 12:45:41 +08:00
if not msg:
2022-05-27 21:13:59 +08:00
im = await generate_day_schedule(server)
2022-06-21 20:37:54 +08:00
await calendar.finish( MessageSegment.image(im))
2022-05-29 12:45:41 +08:00
else:
push_data = load_json('calender_push.json')
if msg.startswith(('开启', 'on', '打开')):
# 添加定时推送任务
time_str = re.search(r'(\d{1,2}):(\d{2})', msg)
if time_str:
push_id = str(get_message_id(event))
push_data[push_id] = {
'server_list': [
str(server)
],
'hour': int(time_str.group(1)),
'minute': int(time_str.group(2)),
'type': event.message_type
}
if event.message_type == 'guild':
push_data[push_id]['guild_id'] = event.guild_id
if scheduler.get_job('genshin_calendar_' + push_id):
scheduler.remove_job("genshin_calendar_" + push_id)
save_json(push_data, 'calender_push.json')
scheduler.add_job(
func=send_calendar,
trigger='cron',
hour=int(time_str.group(1)),
minute=int(time_str.group(2)),
id="genshin_calendar_" + push_id,
args=(push_id, push_data[push_id]),
misfire_grace_time=10
)
await calendar.finish('原神日程推送已开启', at_sender=True)
2022-05-27 21:13:59 +08:00
else:
2022-05-29 12:45:41 +08:00
await calendar.finish('请给出正确的时间格式为12:00', at_sender=True)
2022-05-27 21:13:59 +08:00
# 关闭推送功能
2022-05-29 12:45:41 +08:00
elif msg.startswith(('关闭', 'off', 'close')):
if str(get_message_id(event)) in push_data:
del push_data[str(get_message_id(event))]
if scheduler.get_job("genshin_calendar_" + str(get_message_id(event))):
scheduler.remove_job("genshin_calendar_" + str(get_message_id(event)))
save_json(push_data, 'calender_push.json')
2022-05-29 12:45:41 +08:00
await calendar.finish('原神日程推送已关闭', at_sender=True)
elif msg.startswith(('状态', 'status', 'setting')):
if str(get_message_id(event)) not in push_data:
await calendar.finish('当前会话未开启原神日历订阅', at_sender=True)
2022-05-27 21:13:59 +08:00
else:
2022-05-29 12:45:41 +08:00
reply_msg = f'原神日历订阅:\n'
reply_msg += f'推送时间: {push_data[str(get_message_id(event))]["hour"]}:{push_data[str(get_message_id(event))]["minute"]:02d}\n'
reply_msg += f'服务器: {" ".join(push_data[str(get_message_id(event))]["server_list"])}'
await calendar.finish(reply_msg, at_sender=True)
2022-05-27 21:13:59 +08:00
else:
await calendar.finish('指令错误')
2022-05-29 12:45:41 +08:00
2022-05-27 21:13:59 +08:00
# 自动推送任务
2022-05-29 12:45:41 +08:00
for push_id, push_data in load_json('calender_push.json').items():
2022-05-27 21:13:59 +08:00
scheduler.add_job(
func=send_calendar,
trigger='cron',
2022-05-29 12:45:41 +08:00
hour=push_data['hour'],
minute=push_data['minute'],
2022-05-29 12:53:54 +08:00
id="genshin_calendar_" + push_id,
2022-05-29 12:45:41 +08:00
args=(push_id, push_data),
2022-05-27 21:13:59 +08:00
misfire_grace_time=10
)