diff --git a/uqcsbot/advent.py b/uqcsbot/advent.py index c6b0ffb..cb3754f 100644 --- a/uqcsbot/advent.py +++ b/uqcsbot/advent.py @@ -1,7 +1,7 @@ import io import logging import os -from datetime import datetime +from datetime import datetime from random import choices from typing import Callable, Dict, Iterable, List, Optional, Literal import requests @@ -15,7 +15,16 @@ from uqcsbot.bot import UQCSBot from uqcsbot.models import AOCRegistrations, AOCWinners from uqcsbot.utils.err_log_utils import FatalErrorWithLog -from uqcsbot.utils.advent_utils import Member, Day, Json, InvalidHTTPSCode, ADVENT_DAYS, CACHE_TIME, parse_leaderboard_column_string, print_leaderboard +from uqcsbot.utils.advent_utils import ( + Member, + Day, + Json, + InvalidHTTPSCode, + ADVENT_DAYS, + CACHE_TIME, + parse_leaderboard_column_string, + print_leaderboard, +) # Leaderboard API URL with placeholders for year and code. LEADERBOARD_URL = "https://adventofcode.com/{year}/leaderboard/private/view/{code}.json" @@ -267,12 +276,16 @@ async def reminder_fifteen_minutes(self): return role = discord.utils.get(self.bot.uqcs_server.roles, name=self.bot.AOC_ROLE) if role is None: - logging.warning(f"The role @{self.bot.AOC_ROLE} could not be found for an Advent of Code puzzle pre-release ping.") + logging.warning( + f"The role @{self.bot.AOC_ROLE} could not be found for an Advent of Code puzzle pre-release ping." + ) # Still return a message, as it is better to message and not ping than to not message at all. ping = "" else: ping = f"{role.mention} " - await channel.send(f"{ping}Today's Advent of Code puzzle is released in 15 minutes.") + await channel.send( + f"{ping}Today's Advent of Code puzzle is released in 15 minutes." + ) async def reminder_released(self): """ @@ -291,7 +304,9 @@ async def reminder_released(self): return role = discord.utils.get(self.bot.uqcs_server.roles, name=self.bot.AOC_ROLE) if role is None: - logging.warning(f"The role @{self.bot.AOC_ROLE} could not be found for an Advent of Code puzzle release ping.") + logging.warning( + f"The role @{self.bot.AOC_ROLE} could not be found for an Advent of Code puzzle release ping." + ) # Still return a message, as it is better to message and not ping than to not message at all. ping = "" else: @@ -317,9 +332,7 @@ def _add_winners(self, winners: List[Member], year: int, prize: str): """ for winner in winners: db_session = self.bot.create_db_session() - db_session.add( - AOCWinners(aoc_userid=winner.id, year=year, prize=prize) - ) + db_session.add(AOCWinners(aoc_userid=winner.id, year=year, prize=prize)) db_session.commit() db_session.close() @@ -342,7 +355,6 @@ def _random_choices_without_repition( return result - @advent_command_group.command(name="help") @app_commands.describe(command="The command you want to view help about.") async def help_command( @@ -615,9 +627,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, year=year, discord_userid=discord_id) ) db_session.commit() db_session.close() @@ -693,9 +703,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, year=year, discord_userid=discord_id) ) db_session.commit() db_session.close() diff --git a/uqcsbot/models.py b/uqcsbot/models.py index 4d7f884..4b39fd8 100644 --- a/uqcsbot/models.py +++ b/uqcsbot/models.py @@ -19,7 +19,9 @@ class Base(DeclarativeBase): class AOCWinners(Base): __tablename__ = "aoc_winners" - id: Mapped[int] = mapped_column("id", BigInteger, primary_key=True, nullable=False, autoincrement=True) + id: Mapped[int] = mapped_column( + "id", BigInteger, primary_key=True, nullable=False, autoincrement=True + ) aoc_userid: Mapped[int] = mapped_column("aoc_userid", Integer, nullable=False) year: Mapped[int] = mapped_column("year", Integer, nullable=False) prize: Mapped[str] = mapped_column("prize", String, nullable=True) @@ -28,7 +30,9 @@ class AOCWinners(Base): class AOCRegistrations(Base): __tablename__ = "aoc_registrations" - id: Mapped[int] = mapped_column("id", BigInteger, primary_key=True, nullable=False, autoincrement=True) + id: Mapped[int] = mapped_column( + "id", BigInteger, primary_key=True, nullable=False, autoincrement=True + ) aoc_userid: Mapped[int] = mapped_column("aoc_userid", Integer, nullable=False) year: Mapped[int] = mapped_column("year", Integer, nullable=False) discord_userid: Mapped[int] = mapped_column( diff --git a/uqcsbot/utils/advent_utils.py b/uqcsbot/utils/advent_utils.py index ee7e081..b527cef 100644 --- a/uqcsbot/utils/advent_utils.py +++ b/uqcsbot/utils/advent_utils.py @@ -22,8 +22,9 @@ # The time to cache results to limit requests to adventofcode.com. Note that 15 minutes is the recomended minimum time. CACHE_TIME = timedelta(minutes=15) + class InvalidHTTPSCode(Exception): - def __init__(self, message, request_code): + def __init__(self, message: str, request_code: int): super().__init__(message) self.request_code = request_code @@ -166,6 +167,7 @@ def _format_seconds_long(seconds: Optional[int]): def _get_member_star_progress_bar(member: Member): return "".join(_star_char(len(member.times[day])) for day in ADVENT_DAYS) + class LeaderboardColumn: """ A column in a leaderboard. The title is the name of the column as 2 lines and the calculation is a function that determines what is printed for a given member, index and day. The title and calculation should have the same constant width. @@ -363,6 +365,7 @@ def parse_leaderboard_column_string(s: str, bot: UQCSBot) -> List[LeaderboardCol columns.append(LeaderboardColumn.name_column(bot)) return columns + def print_leaderboard( columns: List[LeaderboardColumn], members: List[Member], day: Optional[Day] ): @@ -381,4 +384,3 @@ def print_leaderboard( ) return leaderboard -