From 1c11e4cf1777b666eeadbccc30753264cf31cc89 Mon Sep 17 00:00:00 2001 From: flifloo Date: Sat, 29 Aug 2020 23:48:16 +0200 Subject: [PATCH] Some bugs fix for calendar notify --- db/Calendar.py | 13 ++++++++----- extensions/calendar.py | 10 +++++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/db/Calendar.py b/db/Calendar.py index c3bc0da..33013c0 100644 --- a/db/Calendar.py +++ b/db/Calendar.py @@ -52,11 +52,12 @@ class Calendar(Base): def events(self, first_date: datetime.date, last_date: datetime.date) -> [ics.Event]: events = [] for e in sorted(list(self.cal().events), key=lambda x: x.begin): - e.begin = e.begin.replace(tzinfo=timezone.utc).astimezone(tz=None) - e.end = e.begin.replace(tzinfo=timezone.utc).astimezone(tz=None) + e.begin = e.begin.astimezone(tz=None) + e.end = e.end.astimezone(tz=None) e.organizer = name_re.findall(e.description)[0] - events.append(e) - return list(filter(lambda x: x.begin.date() >= first_date and x.end.date() <= last_date, events)) + if e.begin.date() >= first_date and e.end.date() <= last_date: + events.append(e) + return list(events) async def notify(self, bot: Bot, event: ics.Event): self.last_notify = datetime.now() @@ -77,4 +78,6 @@ class CalendarNotify(Base): embed = Embed(title="Event is coming !") embed.add_field(name=f"{event.begin.strftime('%H:%M')} - {event.end.strftime('%H:%M')}", value=f"{event.name} | {event.location} - {event.organizer}") - await bot.get_channel(self.channel).send(embed=embed) + channel = bot.get_channel(self.channel) + if channel: + await channel.send(embed=embed) diff --git a/extensions/calendar.py b/extensions/calendar.py index 7b968e7..9b3882c 100644 --- a/extensions/calendar.py +++ b/extensions/calendar.py @@ -1,5 +1,5 @@ import re -from datetime import datetime, timedelta, timezone +from datetime import datetime, timedelta from operator import xor import ics @@ -239,12 +239,12 @@ class Calendar(commands.Cog): @tasks.loop(minutes=1) async def calendar_notify_loop(self): s = db.Session() - now = datetime.now().replace(tzinfo=timezone.utc).astimezone(tz=None) + now = datetime.now().astimezone(tz=None) for c in s.query(db.Calendar).all(): for e in c.events(now.date(), now.date()): - 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 xor(c.last_notify.astimezone(tz=None) < e.begin - timedelta(minutes=30) <= now, + c.last_notify.astimezone(tz=None) < e.begin - timedelta(minutes=10) <= now): + self.bot.loop.create_task(c.notify(self.bot, e)) if s.is_modified(c): s.add(c) s.commit()