Skip to content

Commit

Permalink
fixed event timing
Browse files Browse the repository at this point in the history
  • Loading branch information
Mole1424 committed Oct 2, 2023
1 parent 2be6dd3 commit 8038ff9
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions cogs/commands/event_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from discord.ext import commands
from discord.ext.commands import Bot, Context
from icalendar import Calendar
from pytz import timezone

from models import db_session
from models.event_sync import EventLink
Expand All @@ -24,30 +25,45 @@ def __init__(self, bot: Bot):

@commands.hybrid_command(help=LONG_HELP_TEXT, brief=SHORT_HELP_TEXT)
@commands.check(is_compsoc_exec_in_guild)
@wait_react
async def event(self, ctx: Context, before: str = None, after: str = None):
before = None if before is None else parse_time(before)
after = None if after is None else parse_time(after)
async def event(self, ctx: Context, days: int = 7):
now = datetime.datetime.now(timezone("Europe/London"))
before = now + datetime.timedelta(days=days)

await ctx.send(f"Syncing events from {now} to {before}")

# Fetch and parse ical from website
r = await get_from_url(ICAL_URL)
await ctx.send("Got ical")
c = io.BytesIO(r)
cal = Calendar.from_ical(c.getvalue())
db_links = db_session.query(EventLink).all()
links = {e.uid: e for e in db_links}

dc_events = await ctx.guild.fetch_scheduled_events()
dc_events = {e.id: e for e in dc_events}

# Add events
for ev in cal.walk():
if ev.name != "VEVENT":
continue
t = ev.decoded("dtstart").replace(tzinfo=None)
print(ev.get("summary"), t, before, after)
if before is not None and t < before:
t = (
ev.decoded("dtstart")
.replace(tzinfo=None)
.astimezone(timezone("Europe/London"))
)
print(ev.get("summary"), t, before)
if t < now:
await ctx.send(
f"Skipping event {ev.get('summary')} as it is in the past"
)
continue
if after is not None and t > after:
if t > before:
await ctx.send(
f"Breaking at {ev.get('summary')} at time {t} as it is after the sync period"
)
continue
await self.update_event(ctx, ev, links, dc_events)
await ctx.send("Done :)")

async def update_event(self, ctx, ev, links, dc_events):
"""Check discord events for match, update if existing, otherwise create it"""
Expand Down Expand Up @@ -115,7 +131,7 @@ def check_event_equiv(event_args, event):
@staticmethod
def update_db_link(uid, event_id, link):
"""Update database record. If new, create, otherwise update"""
now = datetime.datetime.now()
now = datetime.datetime.now(timezone("Europe/London"))
if not link:
link = EventLink(uid=uid, discord_event=event_id, last_modified=now)
db_session.add(link)
Expand Down

0 comments on commit 8038ff9

Please sign in to comment.