Merge pull request #169 from stijndcl/events-qol

Events QOL changes
pull/172/head v3.5.0
Stijn De Clercq 2023-02-17 00:08:06 +00:00 committed by GitHub
commit 4de765ef62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 6 deletions

View File

@ -47,4 +47,4 @@ async def get_events(session: AsyncSession, *, now: datetime.datetime) -> list[E
async def get_next_event(session: AsyncSession, *, now: datetime.datetime) -> Optional[Event]: async def get_next_event(session: AsyncSession, *, now: datetime.datetime) -> Optional[Event]:
"""Get the first upcoming event""" """Get the first upcoming event"""
statement = select(Event).where(Event.timestamp > now).order_by(Event.timestamp) statement = select(Event).where(Event.timestamp > now).order_by(Event.timestamp)
return (await session.execute(statement)).scalar_one_or_none() return (await session.execute(statement)).scalars().first()

View File

@ -19,7 +19,7 @@ from didier.utils.discord import colours
from didier.utils.discord.assets import get_author_avatar, get_user_avatar from didier.utils.discord.assets import get_author_avatar, get_user_avatar
from didier.utils.discord.constants import Limits from didier.utils.discord.constants import Limits
from didier.utils.timer import Timer from didier.utils.timer import Timer
from didier.utils.types.datetime import str_to_date, tz_aware_now from didier.utils.types.datetime import localize, str_to_date, tz_aware_now
from didier.utils.types.string import abbreviate, leading from didier.utils.types.string import abbreviate, leading
from didier.views.modals import CreateBookmark from didier.views.modals import CreateBookmark
@ -63,10 +63,14 @@ class Discord(commands.Cog):
return await self.client.log_error(f"Unable to find event with id {event_id}", log_to_discord=True) return await self.client.log_error(f"Unable to find event with id {event_id}", log_to_discord=True)
channel = self.client.get_channel(event.notification_channel) channel = self.client.get_channel(event.notification_channel)
human_readable_time = localize(event.timestamp).strftime("%A, %B %d %Y - %H:%M")
embed = discord.Embed(title="Upcoming Events", colour=discord.Colour.blue()) embed = discord.Embed(title=event.name, colour=discord.Colour.blue())
embed.add_field(name="Event", value=event.name, inline=False) embed.set_author(name="Upcoming Event")
embed.description = event.description embed.description = event.description
embed.add_field(
name="Time", value=f"{human_readable_time} (<t:{round(event.timestamp.timestamp())}:R>)", inline=False
)
await channel.send(embed=embed) await channel.send(embed=embed)

View File

@ -1,9 +1,10 @@
import asyncio import asyncio
from datetime import datetime from datetime import datetime, timedelta
from typing import Optional from typing import Optional
import discord.utils import discord.utils
import settings
from database.crud.events import get_next_event from database.crud.events import get_next_event
from database.schemas import Event from database.schemas import Event
from didier import Didier from didier import Didier
@ -12,6 +13,9 @@ from didier.utils.types.datetime import tz_aware_now
__all__ = ["Timer"] __all__ = ["Timer"]
REMINDER_PREDELAY = timedelta(minutes=settings.REMINDER_PRE)
class Timer: class Timer:
"""Class for scheduled timers""" """Class for scheduled timers"""
@ -58,7 +62,7 @@ class Timer:
async def end_timer(self, *, endtime: datetime, event_id: int): async def end_timer(self, *, endtime: datetime, event_id: int):
"""Wait until a timer runs out, and then trigger an event to send the message""" """Wait until a timer runs out, and then trigger an event to send the message"""
await discord.utils.sleep_until(endtime) await discord.utils.sleep_until(endtime - REMINDER_PREDELAY)
self.upcoming_timer = None self.upcoming_timer = None
self.upcoming_event_id = None self.upcoming_event_id = None
self.client.dispatch("timer_end", event_id) self.client.dispatch("timer_end", event_id)

View File

@ -7,6 +7,7 @@ __all__ = [
"LOCAL_TIMEZONE", "LOCAL_TIMEZONE",
"forward_to_next_weekday", "forward_to_next_weekday",
"int_to_weekday", "int_to_weekday",
"localize",
"parse_dm_string", "parse_dm_string",
"skip_weekends", "skip_weekends",
"str_to_date", "str_to_date",
@ -42,6 +43,14 @@ def int_to_weekday(number: int) -> str: # pragma: no cover # it's useless to wr
return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"][number] return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"][number]
def localize(dt_instance: datetime.datetime, *, default_timezone="UTC") -> datetime.datetime:
"""Localize a datetime instance to my local timezone"""
if dt_instance.tzinfo is None:
dt_instance = dt_instance.replace(tzinfo=zoneinfo.ZoneInfo(default_timezone))
return dt_instance.astimezone(LOCAL_TIMEZONE)
def parse_dm_string(argument: str) -> datetime.date: def parse_dm_string(argument: str) -> datetime.date:
"""Parse a string to [day]/[month] """Parse a string to [day]/[month]

View File

@ -16,6 +16,7 @@ __all__ = [
"YEAR", "YEAR",
"MENU_TIMEOUT", "MENU_TIMEOUT",
"EASTER_EGG_CHANCE", "EASTER_EGG_CHANCE",
"REMINDER_PRE",
"POSTGRES_DB", "POSTGRES_DB",
"POSTGRES_USER", "POSTGRES_USER",
"POSTGRES_PASS", "POSTGRES_PASS",
@ -47,6 +48,7 @@ SEMESTER: int = env.int("SEMESTER", 2)
YEAR: int = env.int("YEAR", 3) YEAR: int = env.int("YEAR", 3)
MENU_TIMEOUT: int = env.int("MENU_TIMEOUT", 30) MENU_TIMEOUT: int = env.int("MENU_TIMEOUT", 30)
EASTER_EGG_CHANCE: int = env.int("EASTER_EGG_CHANCE", 15) EASTER_EGG_CHANCE: int = env.int("EASTER_EGG_CHANCE", 15)
REMINDER_PRE: int = env.int("REMINDER_PRE", 15)
"""Database""" """Database"""
# PostgreSQL # PostgreSQL