116 lines
6.4 KiB
Python
Raw Normal View History

2022-04-15 12:32:36 +08:00
import re
2022-04-23 23:51:40 +08:00
from aiocqhttp.exceptions import ActionFailed
2022-04-26 18:50:56 +08:00
from json import JSONDecodeError
from hoshino import Service,get_bot, logger
from ..util import get_uid_in_msg
2022-03-13 21:25:42 +08:00
from ..get_data import get_daily_note_data
from .get_img import draw_daily_note_card
2022-04-15 12:32:36 +08:00
from ..db_util import update_note_remind2, update_note_remind, get_note_remind, delete_note_remind, update_day_remind_count
from datetime import datetime, timedelta
from asyncio import sleep
from ..config import remind_time_start, remind_time_end, remind_check_time, remind_limit_count
2022-03-13 21:25:42 +08:00
help_msg='''
[ssbq/实时便签 (uid)]查询当前树脂洞天宝钱派遣状况等
2022-04-15 12:32:36 +08:00
[ssbq (uid) 开启提醒(树脂数)/关闭提醒]开启/关闭树脂提醒达到树脂数时会在群里艾特你
*绑定私人cookie之后才能使用
'''
sv = Service('派蒙实时便签', bundle='派蒙', help_=help_msg)
2022-03-13 21:25:42 +08:00
@sv.on_prefix(('ssbq','实时便笺','实时便签'))
async def main(bot,ev):
if ev.message_type == 'group':
gid = str(ev.group_id)
else:
gid = str(ev.user_id)
uid, msg, user_id, use_cache = await get_uid_in_msg(ev)
2022-04-16 19:33:29 +08:00
find_remind_enable = re.search(r'(?P<action>开启提醒|关闭提醒|删除提醒)\s*((?P<num>\d{1,3})|(?:.*))', msg)
2022-04-15 12:32:36 +08:00
if find_remind_enable:
if ev.message_type == 'guild':
await bot.send(ev, '实时便签提醒功能暂时无法在频道内使用哦')
return
2022-04-15 12:32:36 +08:00
if find_remind_enable.group('action') == '开启提醒':
if find_remind_enable.group('num'):
await update_note_remind2(user_id, uid, gid, 1, find_remind_enable.group('num'))
await bot.send(ev, f'开启提醒成功,派蒙会在你的树脂达到{find_remind_enable.group("num")}时提醒你的', at_sender=True)
2022-04-15 12:32:36 +08:00
else:
await update_note_remind2(user_id, uid, gid, 1)
2022-04-15 12:32:36 +08:00
await bot.send(ev, '开启提醒成功', at_sender=True)
elif find_remind_enable.group('action') == '关闭提醒':
await bot.send(ev, '关闭提醒成功', at_sender=True)
await update_note_remind2(user_id, uid, gid, 0)
2022-04-15 12:32:36 +08:00
elif find_remind_enable.group('action') == '删除提醒':
await bot.send(ev, '删除提醒成功', at_sender=True)
await delete_note_remind(user_id, uid)
else:
try:
data = await get_daily_note_data(uid)
if isinstance(data, str):
await bot.send(ev, data, at_sender=True)
else:
daily_note_card = await draw_daily_note_card(data, uid)
await bot.send(ev, daily_note_card, at_sender=True)
except ActionFailed:
logger.exception('账号可能被风控')
await bot.send(ev, '派蒙可能被风控,发不出消息')
except TypeError or AttributeError:
await bot.send(ev, '派蒙好像没有该UID的绑定信息')
except IndexError or KeyError:
await bot.send(ev, '派蒙获取信息失败可能是米游社API有变动请联系开发者')
except JSONDecodeError:
await bot.send(ev, '派蒙获取信息失败,重试一下吧')
2022-04-15 12:32:36 +08:00
except Exception as e:
await bot.send(ev, f'派蒙出现了问题:{e}')
2022-04-15 12:32:36 +08:00
@sv.scheduled_job('cron', minute=f'*/{remind_check_time}')
2022-04-15 12:32:36 +08:00
async def check_note():
now_time = datetime.now()
start_time = datetime(now_time.year, now_time.month, now_time.day, remind_time_start, 0, 0)
end_time = datetime(now_time.year, now_time.month, now_time.day, remind_time_end, 0, 0)
if start_time < now_time < end_time:
return
2022-04-15 12:32:36 +08:00
data = await get_note_remind()
if data:
logger.info('---派蒙开始检查实时便签树脂提醒---')
for user_id, uid, count, remind_group, enable, last_remind_time, today_remind_count in data:
if last_remind_time:
last_remind_time = datetime.strptime(last_remind_time, '%Y%m%d %H:%M:%S')
if now_time - last_remind_time > timedelta(minutes=30):
time_e = True
else:
time_e = False
else:
time_e = True
if enable and ((today_remind_count and today_remind_count < remind_limit_count) or not today_remind_count) and time_e:
2022-04-15 12:32:36 +08:00
now_data = await get_daily_note_data(uid)
if isinstance(now_data, str):
try:
await delete_note_remind(user_id, uid)
if user_id == remind_group:
await get_bot().send_private_msg(user_id=user_id, message=f'[CQ:at,qq={user_id}]你的cookie失效了哦,派蒙没办法帮你检查树脂,请重新添加ck后再叫派蒙开启提醒')
else:
await get_bot().send_group_msg(group_id=remind_group, message=f'[CQ:at,qq={user_id}]你的cookie失效了哦,派蒙没办法帮你检查树脂,请重新添加ck后再叫派蒙开启提醒')
2022-04-15 12:32:36 +08:00
except Exception as e:
logger.error(f'---派蒙发送树脂提醒失败:{e}---')
else:
if now_data['data']['current_resin'] >= count:
logger.info(f'---用户{user_id}的uid{uid}的树脂已经达到阈值了,发送提醒---')
if today_remind_count:
today_remind_count += 1
else:
today_remind_count = 1
now_time_str = now_time.strftime('%Y%m%d %H:%M:%S')
try:
await update_note_remind(user_id, uid, count, remind_group, enable, now_time_str, today_remind_count)
if user_id == remind_group:
await get_bot().send_private_msg(user_id=user_id, message=f'[CQ:at,qq={user_id}]⚠️你的树脂已经达到了{now_data["data"]["current_resin"]},记得清理哦!⚠️')
else:
await get_bot().send_group_msg(group_id=remind_group, message=f'[CQ:at,qq={user_id}]⚠️你的树脂已经达到了{now_data["data"]["current_resin"]},记得清理哦!⚠️')
2022-04-15 12:32:36 +08:00
except Exception as e:
logger.error(f'---派蒙发送树脂提醒失败:{e}---')
await sleep(3)
2022-04-15 12:32:36 +08:00
@sv.scheduled_job('cron', hour='0')
async def delete_day_limit():
logger.info('---清空今日树脂提醒限制---')
await update_day_remind_count()