Skip to content

Commit

Permalink
fix recurring reminders being repeted after downtime
Browse files Browse the repository at this point in the history
  • Loading branch information
mralext20 committed Nov 17, 2023
1 parent d22ced5 commit e10a0c6
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion alexBot/cogs/reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import Coroutine, Dict, List, Optional

import discord
import pytz
from discord import app_commands
from discord.ext.commands import Paginator
from sqlalchemy import and_, or_, select
Expand Down Expand Up @@ -59,6 +58,7 @@ async def reminder_loop(self):
reminders = await session.scalars(stmt)
for reminder in reminders:
if reminder.id not in self.tasks:
if
self.tasks[reminder.id] = self.bot.loop.create_task(self.remind(reminder))
await asyncio.sleep(60)

Expand All @@ -68,6 +68,21 @@ async def remind(self, reminder: Reminder):
now = datetime.datetime.utcnow()
if now > reminder.next_remind:
log.warning(f"reminder {reminder} is overdue")
if reminder.frequency:
#ok, we should skip the missed recurres, and just do the next one
while now > reminder.next_remind:
reminder.next_remind = reminder.next_remind + reminder.frequency

async with db.async_session() as session:
async with session.begin():
edited = await session.scalar(select(Reminder).where(Reminder.id == reminder.id))
if not edited:
log.error(f"reminder {reminder} not found in database")
return
edited.next_remind = reminder.next_remind
session.add(edited)
await session.commit()
return
else:
await asyncio.sleep((reminder.next_remind - now).total_seconds())
allowedMentions = discord.AllowedMentions.none()
Expand Down

0 comments on commit e10a0c6

Please sign in to comment.