🐛 Fixing initial scheduling
Some checks failed
ci / docker (push) Successful in 4m59s
Python formatting PEP8 / Python-PEP8 (push) Failing after 15s

This commit is contained in:
Finn Christiansen 2024-06-17 21:32:59 +02:00
parent f25cd833f8
commit f5f44852d3

View file

@ -5,6 +5,7 @@ import asyncio
import pytz import pytz
from typing import Dict from typing import Dict
import os import os
import logging
from dotenv import load_dotenv from dotenv import load_dotenv
from .models.user import User from .models.user import User
@ -29,15 +30,22 @@ engine = create_engine(os.getenv("DB_URI"))
Session = sessionmaker(bind=engine) Session = sessionmaker(bind=engine)
session = Session() session = Session()
logging.basicConfig(level=logging.INFO)
@bot.listener.on_reaction_event
def initial_schedule() -> None: @bot.listener.on_startup
users = session.query(User).filter(User.location != "").all() 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: 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) schedule_reminder(user.username)
@bot.listener.on_message_event @bot.listener.on_message_event
async def echo(room, message) -> None: async def echo(room, message) -> None:
match = botlib.MessageMatch(room, message, bot, PREFIX) 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 - **!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) await bot.api.send_markdown_message(room.room_id, response)
@ -122,6 +130,8 @@ async def set_reminder(room, message) -> None:
else: else:
user.reminder_time_in_minutes = minutes user.reminder_time_in_minutes = minutes
user.room_id = room.room_id user.room_id = room.room_id
session.add(user)
session.commit()
response = "Your reminder was set to {} minutes before praying.".format(minutes) response = "Your reminder was set to {} minutes before praying.".format(minutes)
schedule_reminder(username) schedule_reminder(username)
await bot.api.send_markdown_message(room.room_id, response) 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: def schedule_reminder(username) -> None:
logging.info("scheduling reminder for {}".format(username))
now = datetime.datetime.now(datetime.UTC) now = datetime.datetime.now(datetime.UTC)
user = session.query(User).filter_by(username=username).one_or_none() user = session.query(User).filter_by(username=username).one_or_none()
user.current_reminder_date = datetime.date.today() 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 # schedule praying times but skip the ones which already have been passed today
seconds = int((praying_time - now).total_seconds()) seconds = int((praying_time - now).total_seconds())
message = "{} is at {}".format(prayer, praying_time.strftime("%H:%M")) 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)) asyncio.ensure_future(remind(username, message, seconds - user.reminder_time_in_minutes * 60))
# save date of last schedule for user # save date of last schedule for user