From 8b8209181f8b218edf936c776bbf6b6410c242f5 Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Fri, 29 Nov 2024 16:36:01 +1000 Subject: [PATCH] Adjustments to Advent of Code (#211) * 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 --- uqcsbot/advent.py | 54 +++++++++++++++-------------------------------- uqcsbot/models.py | 4 +++- 2 files changed, 20 insertions(+), 38 deletions(-) diff --git a/uqcsbot/advent.py b/uqcsbot/advent.py index 5f53d60..9fe2747 100644 --- a/uqcsbot/advent.py +++ b/uqcsbot/advent.py @@ -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 @@ -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, ), @@ -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( @@ -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 @@ -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: @@ -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: @@ -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() ) @@ -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() @@ -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: @@ -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() @@ -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") @@ -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( @@ -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: @@ -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? @@ -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 = [ diff --git a/uqcsbot/models.py b/uqcsbot/models.py index 87744b7..4c3e614 100644 --- a/uqcsbot/models.py +++ b/uqcsbot/models.py @@ -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):