From f5f44852d3bc081fc87f5d61054435d598e3d678 Mon Sep 17 00:00:00 2001 From: Finn Christiansen Date: Mon, 17 Jun 2024 21:32:59 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fixing=20initial=20scheduling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- matrix_bot_praying_times/__main__.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/matrix_bot_praying_times/__main__.py b/matrix_bot_praying_times/__main__.py index 6b3e87c..017e00d 100644 --- a/matrix_bot_praying_times/__main__.py +++ b/matrix_bot_praying_times/__main__.py @@ -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** 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