Skip to content

Commit

Permalink
Adjustments to Advent of Code (#211)
Browse files Browse the repository at this point in the history
* Adjusted Advent to give better error when session token expires

* Fixed styling

* Made AOC registrations persist between years

* Fixed styling

* A quick fix until we remove the column from the database

* Fix styling
  • Loading branch information
49Indium authored Nov 29, 2024
1 parent 84cb77c commit 8b82091
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 38 deletions.
54 changes: 17 additions & 37 deletions uqcsbot/advent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import Callable, Dict, Iterable, List, Optional, Literal
import requests
from requests.exceptions import RequestException
from sqlalchemy.sql.expression import and_

import discord
from discord import app_commands
Expand Down Expand Up @@ -69,7 +68,7 @@
member.times[day].get(2, MAXIMUM_TIME_FOR_STAR),
member.times[day].get(1, MAXIMUM_TIME_FOR_STAR),
),
"Total Time": lambda member, dat: (
"Total Time": lambda member, day: (
member.get_total_time(default=MAXIMUM_TIME_FOR_STAR),
-member.star_total,
),
Expand Down Expand Up @@ -229,6 +228,7 @@ def _get_leaderboard_json(self, year: int, code: int) -> Json:
response = requests.get(
LEADERBOARD_URL.format(year=year, code=code),
cookies={"session": self.session_id},
allow_redirects=False, # Will redirct to home page if session token is out of date
)
except RequestException as exception:
raise FatalErrorWithLog(
Expand Down Expand Up @@ -267,14 +267,12 @@ def _get_members(
]
return self.members_cache[year]

def _get_registrations(self, year: int) -> Iterable[AOCRegistrations]:
def _get_registrations(self) -> Iterable[AOCRegistrations]:
"""
Get all registrations linking an AOC id to a discord account.
"""
db_session = self.bot.create_db_session()
registrations = db_session.query(AOCRegistrations).filter(
AOCRegistrations.year == year
)
registrations = db_session.query(AOCRegistrations)
db_session.commit()
db_session.close()
return registrations
Expand Down Expand Up @@ -495,7 +493,7 @@ async def leaderboard_command(
members = self._get_members(year, code)
except InvalidHTTPSCode:
await interaction.edit_original_response(
content="Error fetching leaderboard data. Check the leaderboard code and year."
content="Error fetching leaderboard data. Check the leaderboard code and year. If this keeps occurring, reach out to committee, as this may be due to an invalid session token."
)
return
except AssertionError:
Expand Down Expand Up @@ -577,11 +575,7 @@ async def register_command(self, interaction: discord.Interaction, aoc_name: str

query = (
db_session.query(AOCRegistrations)
.filter(
and_(
AOCRegistrations.year == year, AOCRegistrations.aoc_userid == AOC_id
)
)
.filter(AOCRegistrations.aoc_userid == AOC_id)
.one_or_none()
)
if query is not None:
Expand All @@ -599,10 +593,7 @@ async def register_command(self, interaction: discord.Interaction, aoc_name: str
query = (
db_session.query(AOCRegistrations)
.filter(
and_(
AOCRegistrations.year == year,
AOCRegistrations.discord_userid == discord_id,
)
AOCRegistrations.discord_userid == discord_id,
)
.one_or_none()
)
Expand All @@ -613,8 +604,8 @@ async def register_command(self, interaction: discord.Interaction, aoc_name: str
return

db_session.add(
AOCRegistrations(aoc_userid=AOC_id, year=year, discord_userid=discord_id)
)
AOCRegistrations(aoc_userid=AOC_id, discord_userid=discord_id, year=2024)
) # this is a quick fix unitl we drop the column in the database
db_session.commit()
db_session.close()

Expand Down Expand Up @@ -671,11 +662,7 @@ async def register_admin_command(

query = (
db_session.query(AOCRegistrations)
.filter(
and_(
AOCRegistrations.year == year, AOCRegistrations.aoc_userid == aoc_id
)
)
.filter(AOCRegistrations.aoc_userid == aoc_id)
.one_or_none()
)
if query is not None:
Expand All @@ -690,8 +677,8 @@ async def register_admin_command(
return

db_session.add(
AOCRegistrations(aoc_userid=aoc_id, year=year, discord_userid=discord_id)
)
AOCRegistrations(aoc_userid=aoc_id, discord_userid=discord_id, year=2024)
) # this is a quick fix unitl we drop the column in the database
db_session.commit()
db_session.close()

Expand All @@ -701,7 +688,7 @@ async def register_admin_command(
else:
discord_ping = f"someone who doesn't seem to be in the server (discord id = {discord_id})"
await interaction.edit_original_response(
content=f"Advent of Code name `{aoc_name}` is now registered to {discord_ping} (for {year})."
content=f"Advent of Code name `{aoc_name}` is now registered to {discord_ping}."
)

@advent_command_group.command(name="unregister")
Expand All @@ -712,14 +699,10 @@ async def unregister_command(self, interaction: discord.Interaction):
await interaction.response.defer(thinking=True)

db_session = self.bot.create_db_session()
year = datetime.now().year

discord_id = interaction.user.id
query = db_session.query(AOCRegistrations).filter(
and_(
AOCRegistrations.year == year,
AOCRegistrations.discord_userid == discord_id,
)
AOCRegistrations.discord_userid == discord_id,
)
if (query.one_or_none()) is None:
await interaction.edit_original_response(
Expand Down Expand Up @@ -754,10 +737,7 @@ async def unregister_admin_command(

db_session = self.bot.create_db_session()
query = db_session.query(AOCRegistrations).filter(
and_(
AOCRegistrations.year == year,
AOCRegistrations.discord_userid == discord_id,
)
AOCRegistrations.discord_userid == discord_id,
)
if (query.one_or_none()) is None:
if discord_user:
Expand Down Expand Up @@ -814,7 +794,7 @@ async def previous_winners_command(
)
return

registrations = self._get_registrations(year)
registrations = self._get_registrations()
registered_AOC_ids = [member.aoc_userid for member in registrations]

# TODO would an embed be appropriate?
Expand Down Expand Up @@ -893,7 +873,7 @@ async def add_winners_command(
)
return

registrations = self._get_registrations(year)
registrations = self._get_registrations()
registered_AOC_ids = [member.aoc_userid for member in registrations]

potential_winners = [
Expand Down
4 changes: 3 additions & 1 deletion uqcsbot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ class AOCRegistrations(Base):
"discord_userid", BigInteger, primary_key=True, nullable=False
)
aoc_userid: Mapped[int] = mapped_column("aoc_userid", Integer, nullable=False)
year: Mapped[int] = mapped_column("year", Integer, nullable=False)
year: Mapped[int] = mapped_column(
"year", Integer, nullable=False
) # Try to remove this column from the database at some point


class MCWhitelist(Base):
Expand Down

0 comments on commit 8b82091

Please sign in to comment.