1
0
Fork 0

Fix calendar loop

This commit is contained in:
Ethanell 2020-05-28 17:25:13 +02:00
parent cd3710b49d
commit 79aa9f632d
2 changed files with 4 additions and 1 deletions

View file

@ -29,6 +29,7 @@ class Calendar(Base):
server = Column(BigInteger, nullable=False)
calendar = Column(Text)
calendar_update = Column(DateTime)
last_notify = Column(DateTime, nullable=False, default=datetime.now())
calendars_notify = relationship("CalendarNotify", backref="calendar", lazy="subquery")
@ -58,6 +59,7 @@ class Calendar(Base):
return list(filter(lambda x: x.begin.date() >= first_date and x.end.date() <= last_date, events))
async def notify(self, bot: Bot, event: ics.Event):
self.last_notify = datetime.now()
for n in self.calendars_notify:
bot.loop.create_task(n.notify(bot, event))

View file

@ -242,7 +242,8 @@ class Calendar(commands.Cog):
now = datetime.now().replace(tzinfo=timezone.utc).astimezone(tz=None)
for c in s.query(db.Calendar).all():
for e in c.events(now.date(), now.date()):
if xor(e.begin >= now - timedelta(minutes=30), e.begin >= now - timedelta(minutes=10)):
if xor(c.last_notify < e.begin - timedelta(minutes=30) <= now,
c.last_notify < e.begin - timedelta(minutes=10) <= now):
self.bot.loop.create_task(await c.notify(self.bot, e))
if s.is_modified(c):
s.add(c)