Skip to content

Commit

Permalink
Made AOC registrations persist between years
Browse files Browse the repository at this point in the history
  • Loading branch information
49Indium committed Nov 26, 2024
1 parent 14526ee commit 625924c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 30 deletions.
41 changes: 12 additions & 29 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 @@ -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
Expand Down Expand Up @@ -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()
)
Expand All @@ -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()
)
Expand All @@ -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()
Expand Down Expand Up @@ -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()
)
Expand All @@ -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()
Expand All @@ -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")
Expand All @@ -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(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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 = [
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

0 comments on commit 625924c

Please sign in to comment.