1
0
Fork 0

Setup next day resume for calendar

This commit is contained in:
Ethanell 2020-08-30 00:32:32 +02:00
parent 1c11e4cf17
commit 45d219e20e
2 changed files with 33 additions and 16 deletions

View file

@ -59,10 +59,20 @@ class Calendar(Base):
events.append(e) events.append(e)
return list(events) return list(events)
async def notify(self, bot: Bot, event: ics.Event): def week_embed(self, date: datetime.date):
self.last_notify = datetime.now() date -= timedelta(days=date.weekday())
for n in self.calendars_notify: embed = Embed(title=f"Week calendar: {self.name}",
bot.loop.create_task(n.notify(bot, event)) 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): class CalendarNotify(Base):
@ -81,3 +91,8 @@ class CalendarNotify(Base):
channel = bot.get_channel(self.channel) channel = bot.get_channel(self.channel)
if channel: if channel:
await channel.send(embed=embed) 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()))

View file

@ -1,5 +1,5 @@
import re import re
from datetime import datetime, timedelta from datetime import datetime, timedelta, time
from operator import xor from operator import xor
import ics import ics
@ -146,16 +146,9 @@ class Calendar(commands.Cog):
date = datetime.strptime(day, "%d/%m/%Y").date() date = datetime.strptime(day, "%d/%m/%Y").date()
except ValueError: except ValueError:
raise BadArgument() raise BadArgument()
date -= timedelta(days=date.weekday())
embed = Embed(title=f"Week calendar: {c.name}", embed = c.week_embed(date)
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)
s = db.Session() s = db.Session()
if s.is_modified(c): if s.is_modified(c):
s.add(c) s.add(c)
@ -241,10 +234,19 @@ class Calendar(commands.Cog):
s = db.Session() s = db.Session()
now = datetime.now().astimezone(tz=None) now = datetime.now().astimezone(tz=None)
for c in s.query(db.Calendar).all(): 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()): for e in c.events(now.date(), now.date()):
if xor(c.last_notify.astimezone(tz=None) < e.begin - timedelta(minutes=30) <= now, 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): 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): if s.is_modified(c):
s.add(c) s.add(c)
s.commit() s.commit()