From 45d219e20e908a41aa1f9aaa62e34806480e612c Mon Sep 17 00:00:00 2001 From: flifloo Date: Sun, 30 Aug 2020 00:32:32 +0200 Subject: [PATCH] Setup next day resume for calendar --- db/Calendar.py | 23 +++++++++++++++++++---- extensions/calendar.py | 26 ++++++++++++++------------ 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/db/Calendar.py b/db/Calendar.py index 33013c0..f2b5a7c 100644 --- a/db/Calendar.py +++ b/db/Calendar.py @@ -59,10 +59,20 @@ class Calendar(Base): events.append(e) return list(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)) + def week_embed(self, date: datetime.date): + date -= timedelta(days=date.weekday()) + embed = Embed(title=f"Week calendar: {self.name}", + description=f"{date.strftime('%d/%m/%Y')} - {(date + timedelta(days=4)).strftime('%d/%m/%Y')}") + + for d in range(5): + events = [] + for e in self.events(date, date): + events.append(f"*{e.begin.strftime('%H:%M')} - {e.end.strftime('%H:%M')}*: " + f"**{e.name}** | {e.location} - {e.organizer}") + embed.add_field(name=date.strftime("%d/%m/%Y"), value="\n".join(events) or "Nothing !", inline=False) + date = date + timedelta(days=1) + + return embed class CalendarNotify(Base): @@ -81,3 +91,8 @@ class CalendarNotify(Base): channel = bot.get_channel(self.channel) if channel: await channel.send(embed=embed) + + async def next_day_resume(self, bot: Bot): + channel = bot.get_channel(self.channel) + if channel: + await channel.send(embed=self.calendar.week_embed((datetime.now() + timedelta(days=1)).date())) diff --git a/extensions/calendar.py b/extensions/calendar.py index 9b3882c..6dcbfab 100644 --- a/extensions/calendar.py +++ b/extensions/calendar.py @@ -1,5 +1,5 @@ import re -from datetime import datetime, timedelta +from datetime import datetime, timedelta, time from operator import xor import ics @@ -146,16 +146,9 @@ class Calendar(commands.Cog): date = datetime.strptime(day, "%d/%m/%Y").date() except ValueError: raise BadArgument() - date -= timedelta(days=date.weekday()) - embed = Embed(title=f"Week calendar: {c.name}", - description=f"{date.strftime('%d/%m/%Y')} - {(date + timedelta(days=4)).strftime('%d/%m/%Y')}") - for d in range(5): - events = [] - for e in c.events(date, date): - events.append(f"*{e.begin.strftime('%H:%M')} - {e.end.strftime('%H:%M')}*: " - f"**{e.name}** | {e.location} - {e.organizer}") - embed.add_field(name=date.strftime("%d/%m/%Y"), value="\n".join(events) or "Nothing !", inline=False) - date = date + timedelta(days=1) + + embed = c.week_embed(date) + s = db.Session() if s.is_modified(c): s.add(c) @@ -241,10 +234,19 @@ class Calendar(commands.Cog): s = db.Session() now = datetime.now().astimezone(tz=None) for c in s.query(db.Calendar).all(): + if now.isoweekday() not in [5, 6] and now.time() >= time(hour=20) and\ + c.last_notify.astimezone(tz=None) < now.replace(hour=20, minute=00): + c.last_notify = datetime.now() + for n in c.calendars_notify: + await n.next_day_resume(self.bot) + for e in c.events(now.date(), now.date()): 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)) + c.last_notify = datetime.now() + for n in c.calendars_notify: + await n.notify(self.bot, e) + break if s.is_modified(c): s.add(c) s.commit()