🐛 Fixing initial scheduling
This commit is contained in:
parent
f25cd833f8
commit
f5f44852d3
1 changed files with 17 additions and 5 deletions
|
@ -5,6 +5,7 @@ import asyncio
|
|||
import pytz
|
||||
from typing import Dict
|
||||
import os
|
||||
import logging
|
||||
from dotenv import load_dotenv
|
||||
from .models.user import User
|
||||
|
||||
|
@ -29,15 +30,22 @@ engine = create_engine(os.getenv("DB_URI"))
|
|||
Session = sessionmaker(bind=engine)
|
||||
session = Session()
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
|
||||
@bot.listener.on_reaction_event
|
||||
def initial_schedule() -> None:
|
||||
users = session.query(User).filter(User.location != "").all()
|
||||
|
||||
@bot.listener.on_startup
|
||||
async def initial_schedule(room_id):
|
||||
logging.info("starting praying times bot")
|
||||
users = session.query(User).filter(User.location != "", User.reminder_time_in_minutes is not None).all()
|
||||
for user in users:
|
||||
logging.info("scheduling initial reminder for {}".format(user.username))
|
||||
# reset last reminder schedule time
|
||||
user.current_reminder_date = None
|
||||
session.add(user)
|
||||
session.commit()
|
||||
schedule_reminder(user.username)
|
||||
|
||||
|
||||
|
||||
@bot.listener.on_message_event
|
||||
async def echo(room, message) -> None:
|
||||
match = botlib.MessageMatch(room, message, bot, PREFIX)
|
||||
|
@ -79,7 +87,7 @@ async def usage(room, message) -> None:
|
|||
- **!set-reminder** <X> where X is the number of minutes you want to receive the reminder before praying
|
||||
"""
|
||||
|
||||
if match.is_not_from_this_bot() and not match.prefix():
|
||||
if match.is_not_from_this_bot() and not match.prefix() and room.room_id:
|
||||
await bot.api.send_markdown_message(room.room_id, response)
|
||||
|
||||
|
||||
|
@ -122,6 +130,8 @@ async def set_reminder(room, message) -> None:
|
|||
else:
|
||||
user.reminder_time_in_minutes = minutes
|
||||
user.room_id = room.room_id
|
||||
session.add(user)
|
||||
session.commit()
|
||||
response = "Your reminder was set to {} minutes before praying.".format(minutes)
|
||||
schedule_reminder(username)
|
||||
await bot.api.send_markdown_message(room.room_id, response)
|
||||
|
@ -139,6 +149,7 @@ def get_praying_times(date: datetime.date, username) -> Dict[str, str]:
|
|||
|
||||
|
||||
def schedule_reminder(username) -> None:
|
||||
logging.info("scheduling reminder for {}".format(username))
|
||||
now = datetime.datetime.now(datetime.UTC)
|
||||
user = session.query(User).filter_by(username=username).one_or_none()
|
||||
user.current_reminder_date = datetime.date.today()
|
||||
|
@ -151,6 +162,7 @@ def schedule_reminder(username) -> None:
|
|||
# schedule praying times but skip the ones which already have been passed today
|
||||
seconds = int((praying_time - now).total_seconds())
|
||||
message = "{} is at {}".format(prayer, praying_time.strftime("%H:%M"))
|
||||
logging.info("scheduling reminder for {} with message: {}".format(username, message))
|
||||
asyncio.ensure_future(remind(username, message, seconds - user.reminder_time_in_minutes * 60))
|
||||
# save date of last schedule for user
|
||||
|
||||
|
|
Loading…
Reference in a new issue