From 7d46f4923c721cd0c6be3a5d617eed11bd990c68 Mon Sep 17 00:00:00 2001 From: Finn Christiansen Date: Sun, 9 Jun 2024 21:22:58 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20!set-reminder=20now=20works!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bot.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/bot.py b/bot.py index 110ccbc..1f243d0 100644 --- a/bot.py +++ b/bot.py @@ -1,7 +1,7 @@ import simplematrixbotlib as botlib import requests import datetime -import threading +import asyncio import pytz import os from dotenv import load_dotenv @@ -51,11 +51,10 @@ async def times(room, message): async def usage(room, message): match = botlib.MessageMatch(room, message, bot, PREFIX) - response = """ - usage: - - **!set-location** - - **!times** - - **!set-reminder** where X is the number of minutes you want to receive the reminder before praying + response = """usage: +- **!set-location** +- **!times** +- **!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(): @@ -109,15 +108,17 @@ def get_praying_times(date: datetime.date, username): def schedule_reminder(username): times = get_praying_times(datetime.date.today(), username) - now = utc.localize(datetime.datetime.now(datetime.UTC)) + now = datetime.datetime.now(datetime.UTC) for prayer, time in times.items(): praying_time = datetime.datetime.fromisoformat(time) if praying_time > now: seconds = int((praying_time - now).total_seconds()) - threading.Timer(seconds - user_reminders[username] * 60, remind, [username]).start() + message = "{} is at {}".format(prayer, praying_time.strftime("%H:%M")) + asyncio.ensure_future(remind(username, message, seconds - user_reminders[username] * 60)) -async def remind(username, message): +async def remind(username, message, seconds): + await asyncio.sleep(seconds) await bot.api.send_markdown_message(user_room_ids[username], message)