mirror of https://github.com/stijndcl/didier
commit
4de765ef62
|
@ -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]:
|
||||
"""Get the first upcoming event"""
|
||||
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()
|
||||
|
|
|
@ -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.constants import Limits
|
||||
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.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)
|
||||
|
||||
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.add_field(name="Event", value=event.name, inline=False)
|
||||
embed = discord.Embed(title=event.name, colour=discord.Colour.blue())
|
||||
embed.set_author(name="Upcoming Event")
|
||||
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)
|
||||
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import asyncio
|
||||
from datetime import datetime
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Optional
|
||||
|
||||
import discord.utils
|
||||
|
||||
import settings
|
||||
from database.crud.events import get_next_event
|
||||
from database.schemas import Event
|
||||
from didier import Didier
|
||||
|
@ -12,6 +13,9 @@ from didier.utils.types.datetime import tz_aware_now
|
|||
__all__ = ["Timer"]
|
||||
|
||||
|
||||
REMINDER_PREDELAY = timedelta(minutes=settings.REMINDER_PRE)
|
||||
|
||||
|
||||
class Timer:
|
||||
"""Class for scheduled timers"""
|
||||
|
||||
|
@ -58,7 +62,7 @@ class Timer:
|
|||
|
||||
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"""
|
||||
await discord.utils.sleep_until(endtime)
|
||||
await discord.utils.sleep_until(endtime - REMINDER_PREDELAY)
|
||||
self.upcoming_timer = None
|
||||
self.upcoming_event_id = None
|
||||
self.client.dispatch("timer_end", event_id)
|
||||
|
|
|
@ -7,6 +7,7 @@ __all__ = [
|
|||
"LOCAL_TIMEZONE",
|
||||
"forward_to_next_weekday",
|
||||
"int_to_weekday",
|
||||
"localize",
|
||||
"parse_dm_string",
|
||||
"skip_weekends",
|
||||
"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]
|
||||
|
||||
|
||||
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:
|
||||
"""Parse a string to [day]/[month]
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ __all__ = [
|
|||
"YEAR",
|
||||
"MENU_TIMEOUT",
|
||||
"EASTER_EGG_CHANCE",
|
||||
"REMINDER_PRE",
|
||||
"POSTGRES_DB",
|
||||
"POSTGRES_USER",
|
||||
"POSTGRES_PASS",
|
||||
|
@ -47,6 +48,7 @@ SEMESTER: int = env.int("SEMESTER", 2)
|
|||
YEAR: int = env.int("YEAR", 3)
|
||||
MENU_TIMEOUT: int = env.int("MENU_TIMEOUT", 30)
|
||||
EASTER_EGG_CHANCE: int = env.int("EASTER_EGG_CHANCE", 15)
|
||||
REMINDER_PRE: int = env.int("REMINDER_PRE", 15)
|
||||
|
||||
"""Database"""
|
||||
# PostgreSQL
|
||||
|
|
Loading…
Reference in New Issue