🐛 !set-reminder now works!

This commit is contained in:
Finn Christiansen 2024-06-09 21:22:58 +02:00
parent cf88a8c46f
commit 7d46f4923c

13
bot.py
View file

@ -1,7 +1,7 @@
import simplematrixbotlib as botlib import simplematrixbotlib as botlib
import requests import requests
import datetime import datetime
import threading import asyncio
import pytz import pytz
import os import os
from dotenv import load_dotenv from dotenv import load_dotenv
@ -51,8 +51,7 @@ async def times(room, message):
async def usage(room, message): async def usage(room, message):
match = botlib.MessageMatch(room, message, bot, PREFIX) match = botlib.MessageMatch(room, message, bot, PREFIX)
response = """ response = """usage:
usage:
- **!set-location** <City, Country> - **!set-location** <City, Country>
- **!times** - **!times**
- **!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
@ -109,15 +108,17 @@ def get_praying_times(date: datetime.date, username):
def schedule_reminder(username): def schedule_reminder(username):
times = get_praying_times(datetime.date.today(), 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(): for prayer, time in times.items():
praying_time = datetime.datetime.fromisoformat(time) praying_time = datetime.datetime.fromisoformat(time)
if praying_time > now: if praying_time > now:
seconds = int((praying_time - now).total_seconds()) 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) await bot.api.send_markdown_message(user_room_ids[username], message)