Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Truncate passed events on days where there are too many events #26

Merged
merged 1 commit into from
Feb 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions src/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ def sanitized_location(self) -> str:
def at_mil(self) -> bool:
return self.location in ("", "MALA 3001")

def upcoming(self) -> bool:
return self.end >= datetime.datetime.now().astimezone()

def embed_str(self) -> str:
location_str = (
f"(location: {self.sanitized_location})" if not self.at_mil() else ""
Expand Down Expand Up @@ -234,6 +237,17 @@ async def update_channel_name(self, events: list[Event]) -> None:
if channel.name != (new_name := self.current_status(events).value):
await channel.edit(name=new_name)

def events_list_str(self, events: list[Event]) -> str:
strs = [event.embed_str() for event in events]
estimated_length = sum(len(s + "\n") for s in strs) + len("_... (truncated)_")
if estimated_length > 1024:
strs = [event.embed_str() for event in events if event.upcoming()]
if events[0].embed_str() != strs[0]:
# This means that the first event will not be shown, so we should
# mark that
strs.insert(0, "_... (truncated)_")
return capped_str(strs) or "No events scheduled."

@tasks.loop(minutes=5)
async def calendar(self):
await self.bot.wait_until_ready()
Expand All @@ -257,10 +271,7 @@ async def calendar(self):
]
embed.add_field(
name="__Today's Events__",
value=capped_str(
[event.embed_str() for event in today_events],
)
or "No events.",
value=self.events_list_str(today_events),
inline=False,
)
tomorrow_events = [
Expand All @@ -270,16 +281,14 @@ async def calendar(self):
]
embed.add_field(
name="__Tomorrow's Events__",
value=capped_str([event.embed_str() for event in tomorrow_events])
or "No events.",
value=self.events_list_str(tomorrow_events),
inline=False,
)
two_days = datetime.date.today() + datetime.timedelta(days=2)
two_days_events = [event for event in events if event.start.date() == two_days]
embed.add_field(
name=f"__{two_days.strftime('%A')}'s Events__ (in 2 days)",
value=capped_str([event.embed_str() for event in two_days_events])
or "No events.",
value=self.events_list_str(two_days_events),
inline=False,
)
three_days = datetime.date.today() + datetime.timedelta(days=3)
Expand All @@ -288,8 +297,7 @@ async def calendar(self):
]
embed.add_field(
name=f"__{three_days.strftime('%A')}'s Events__ (in 3 days)",
value=capped_str([event.embed_str() for event in three_days_events])
or "No events.",
value=self.events_list_str(three_days_events),
inline=False,
)
four_days = datetime.date.today() + datetime.timedelta(days=4)
Expand All @@ -298,8 +306,7 @@ async def calendar(self):
]
embed.add_field(
name=f"__{four_days.strftime('%A')}'s Events__ (in 4 days)",
value=capped_str([event.embed_str() for event in four_days_events])
or "No events.",
value=self.events_list_str(four_days_events),
inline=False,
)
# Date formatted: Feb 2, 2024 03:56PM
Expand Down
Loading