Some bugs fix for calendar notify
This commit is contained in:
parent
79aa9f632d
commit
1c11e4cf17
2 changed files with 13 additions and 10 deletions
|
@ -52,11 +52,12 @@ class Calendar(Base):
|
||||||
def events(self, first_date: datetime.date, last_date: datetime.date) -> [ics.Event]:
|
def events(self, first_date: datetime.date, last_date: datetime.date) -> [ics.Event]:
|
||||||
events = []
|
events = []
|
||||||
for e in sorted(list(self.cal().events), key=lambda x: x.begin):
|
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.begin = e.begin.astimezone(tz=None)
|
||||||
e.end = e.begin.replace(tzinfo=timezone.utc).astimezone(tz=None)
|
e.end = e.end.astimezone(tz=None)
|
||||||
e.organizer = name_re.findall(e.description)[0]
|
e.organizer = name_re.findall(e.description)[0]
|
||||||
events.append(e)
|
if e.begin.date() >= first_date and e.end.date() <= last_date:
|
||||||
return list(filter(lambda x: x.begin.date() >= first_date and x.end.date() <= last_date, events))
|
events.append(e)
|
||||||
|
return list(events)
|
||||||
|
|
||||||
async def notify(self, bot: Bot, event: ics.Event):
|
async def notify(self, bot: Bot, event: ics.Event):
|
||||||
self.last_notify = datetime.now()
|
self.last_notify = datetime.now()
|
||||||
|
@ -77,4 +78,6 @@ class CalendarNotify(Base):
|
||||||
embed = Embed(title="Event is coming !")
|
embed = Embed(title="Event is coming !")
|
||||||
embed.add_field(name=f"{event.begin.strftime('%H:%M')} - {event.end.strftime('%H:%M')}",
|
embed.add_field(name=f"{event.begin.strftime('%H:%M')} - {event.end.strftime('%H:%M')}",
|
||||||
value=f"{event.name} | {event.location} - {event.organizer}")
|
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)
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import re
|
import re
|
||||||
from datetime import datetime, timedelta, timezone
|
from datetime import datetime, timedelta
|
||||||
from operator import xor
|
from operator import xor
|
||||||
|
|
||||||
import ics
|
import ics
|
||||||
|
@ -239,12 +239,12 @@ class Calendar(commands.Cog):
|
||||||
@tasks.loop(minutes=1)
|
@tasks.loop(minutes=1)
|
||||||
async def calendar_notify_loop(self):
|
async def calendar_notify_loop(self):
|
||||||
s = db.Session()
|
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 c in s.query(db.Calendar).all():
|
||||||
for e in c.events(now.date(), now.date()):
|
for e in c.events(now.date(), now.date()):
|
||||||
if xor(c.last_notify < e.begin - timedelta(minutes=30) <= now,
|
if xor(c.last_notify.astimezone(tz=None) < e.begin - timedelta(minutes=30) <= now,
|
||||||
c.last_notify < e.begin - timedelta(minutes=10) <= now):
|
c.last_notify.astimezone(tz=None) < e.begin - timedelta(minutes=10) <= now):
|
||||||
self.bot.loop.create_task(await c.notify(self.bot, e))
|
self.bot.loop.create_task(c.notify(self.bot, e))
|
||||||
if s.is_modified(c):
|
if s.is_modified(c):
|
||||||
s.add(c)
|
s.add(c)
|
||||||
s.commit()
|
s.commit()
|
||||||
|
|
Reference in a new issue