From 23497410cc5db0dfe72fe9c559d172bf98104d9a Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Wed, 20 Nov 2024 15:31:29 +1000 Subject: [PATCH 1/6] Adjusted Advent to give better error when session token expires --- uqcsbot/advent.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/uqcsbot/advent.py b/uqcsbot/advent.py index 5f53d60..cbb0494 100644 --- a/uqcsbot/advent.py +++ b/uqcsbot/advent.py @@ -229,6 +229,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( @@ -495,7 +496,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: From 14526ee82e19c2cfd08ee89d0e37864266a00b7d Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Wed, 20 Nov 2024 15:33:42 +1000 Subject: [PATCH 2/6] Fixed styling --- uqcsbot/advent.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uqcsbot/advent.py b/uqcsbot/advent.py index cbb0494..8f7ce64 100644 --- a/uqcsbot/advent.py +++ b/uqcsbot/advent.py @@ -229,7 +229,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 + allow_redirects=False, # Will redirct to home page if session token is out of date ) except RequestException as exception: raise FatalErrorWithLog( From 625924ca5ce6070c163669cd9bb970defeaac339 Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Tue, 26 Nov 2024 12:33:54 +1000 Subject: [PATCH 3/6] Made AOC registrations persist between years --- uqcsbot/advent.py | 41 ++++++++++++----------------------------- uqcsbot/models.py | 1 - 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/uqcsbot/advent.py b/uqcsbot/advent.py index 8f7ce64..043a3dc 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, ), @@ -268,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 @@ -579,9 +576,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 - ) + AOCRegistrations.aoc_userid == AOC_id ) .one_or_none() ) @@ -600,10 +595,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, - ) ) .one_or_none() ) @@ -614,7 +606,7 @@ 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) ) db_session.commit() db_session.close() @@ -673,9 +665,7 @@ async def register_admin_command( query = ( db_session.query(AOCRegistrations) .filter( - and_( - AOCRegistrations.year == year, AOCRegistrations.aoc_userid == aoc_id - ) + AOCRegistrations.aoc_userid == aoc_id ) .one_or_none() ) @@ -691,7 +681,7 @@ 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) ) db_session.commit() db_session.close() @@ -702,7 +692,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") @@ -713,14 +703,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( @@ -755,10 +741,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: @@ -815,7 +798,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? @@ -894,7 +877,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..f117a9b 100644 --- a/uqcsbot/models.py +++ b/uqcsbot/models.py @@ -34,7 +34,6 @@ 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) class MCWhitelist(Base): From d63fa84ed48cc96bf3aa931da2ee312503349167 Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Tue, 26 Nov 2024 12:34:53 +1000 Subject: [PATCH 4/6] Fixed styling --- uqcsbot/advent.py | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/uqcsbot/advent.py b/uqcsbot/advent.py index 043a3dc..f0e4385 100644 --- a/uqcsbot/advent.py +++ b/uqcsbot/advent.py @@ -575,9 +575,7 @@ async def register_command(self, interaction: discord.Interaction, aoc_name: str query = ( db_session.query(AOCRegistrations) - .filter( - AOCRegistrations.aoc_userid == AOC_id - ) + .filter(AOCRegistrations.aoc_userid == AOC_id) .one_or_none() ) if query is not None: @@ -595,7 +593,7 @@ async def register_command(self, interaction: discord.Interaction, aoc_name: str query = ( db_session.query(AOCRegistrations) .filter( - AOCRegistrations.discord_userid == discord_id, + AOCRegistrations.discord_userid == discord_id, ) .one_or_none() ) @@ -605,9 +603,7 @@ async def register_command(self, interaction: discord.Interaction, aoc_name: str ) return - db_session.add( - AOCRegistrations(aoc_userid=AOC_id, discord_userid=discord_id) - ) + db_session.add(AOCRegistrations(aoc_userid=AOC_id, discord_userid=discord_id)) db_session.commit() db_session.close() @@ -664,9 +660,7 @@ async def register_admin_command( query = ( db_session.query(AOCRegistrations) - .filter( - AOCRegistrations.aoc_userid == aoc_id - ) + .filter(AOCRegistrations.aoc_userid == aoc_id) .one_or_none() ) if query is not None: @@ -680,9 +674,7 @@ async def register_admin_command( ) return - db_session.add( - AOCRegistrations(aoc_userid=aoc_id, discord_userid=discord_id) - ) + db_session.add(AOCRegistrations(aoc_userid=aoc_id, discord_userid=discord_id)) db_session.commit() db_session.close() From 276faf321606924ac1488ef9f5c02582f8ca3882 Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Fri, 29 Nov 2024 16:31:20 +1000 Subject: [PATCH 5/6] A quick fix until we remove the column from the database --- uqcsbot/advent.py | 4 ++-- uqcsbot/models.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/uqcsbot/advent.py b/uqcsbot/advent.py index f0e4385..da5759e 100644 --- a/uqcsbot/advent.py +++ b/uqcsbot/advent.py @@ -603,7 +603,7 @@ async def register_command(self, interaction: discord.Interaction, aoc_name: str ) return - db_session.add(AOCRegistrations(aoc_userid=AOC_id, discord_userid=discord_id)) + db_session.add(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() @@ -674,7 +674,7 @@ async def register_admin_command( ) return - db_session.add(AOCRegistrations(aoc_userid=aoc_id, discord_userid=discord_id)) + db_session.add(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() diff --git a/uqcsbot/models.py b/uqcsbot/models.py index f117a9b..6211f7a 100644 --- a/uqcsbot/models.py +++ b/uqcsbot/models.py @@ -34,7 +34,7 @@ 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) # Try to remove this column from the database at some point class MCWhitelist(Base): __tablename__ = "mc_whitelisted" From 07d97fafba467fedb59ce83a9cac3d220cb63429 Mon Sep 17 00:00:00 2001 From: Isaac Beh Date: Fri, 29 Nov 2024 16:32:16 +1000 Subject: [PATCH 6/6] Fix styling --- uqcsbot/advent.py | 8 ++++++-- uqcsbot/models.py | 5 ++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/uqcsbot/advent.py b/uqcsbot/advent.py index da5759e..9fe2747 100644 --- a/uqcsbot/advent.py +++ b/uqcsbot/advent.py @@ -603,7 +603,9 @@ async def register_command(self, interaction: discord.Interaction, aoc_name: str ) return - db_session.add(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.add( + 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() @@ -674,7 +676,9 @@ async def register_admin_command( ) return - db_session.add(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.add( + 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() diff --git a/uqcsbot/models.py b/uqcsbot/models.py index 6211f7a..4c3e614 100644 --- a/uqcsbot/models.py +++ b/uqcsbot/models.py @@ -34,7 +34,10 @@ 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) # Try to remove this column from the database at some point + year: Mapped[int] = mapped_column( + "year", Integer, nullable=False + ) # Try to remove this column from the database at some point + class MCWhitelist(Base): __tablename__ = "mc_whitelisted"