Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjustments to Advent of Code #211

Merged
merged 6 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 15 additions & 39 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 @@ -612,9 +603,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)
)
db_session.add(AOCRegistrations(aoc_userid=AOC_id, discord_userid=discord_id))
db_session.commit()
db_session.close()

Expand Down Expand Up @@ -671,11 +660,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 @@ -689,9 +674,7 @@ async def register_admin_command(
)
return

db_session.add(
AOCRegistrations(aoc_userid=aoc_id, year=year, discord_userid=discord_id)
)
db_session.add(AOCRegistrations(aoc_userid=aoc_id, discord_userid=discord_id))
db_session.commit()
db_session.close()

Expand All @@ -701,7 +684,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 +695,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 +733,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 +790,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 +869,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
1 change: 0 additions & 1 deletion uqcsbot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down