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

Reworked advent of code. #183

Merged
merged 12 commits into from
Nov 30, 2023
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ build-backend = "poetry.core.masonry.api"
[tool.pyright]
strict = ["**"]
exclude = [
"**/advent.py",
"**/bot.py",
"**/error_handler.py",
"**/events.py",
Expand Down
85 changes: 35 additions & 50 deletions uqcsbot/advent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import io
import logging
import os
from datetime import datetime
from random import choices
Expand Down Expand Up @@ -201,6 +200,27 @@ def __init__(self, bot: UQCSBot):
bot, "Unable to find AoC session ID. Not loading advent cog."
)

@commands.Cog.listener()
async def on_ready(self):
channel = discord.utils.get(
self.bot.uqcs_server.channels, name=self.bot.AOC_CNAME
)
if isinstance(channel, discord.TextChannel):
self.channel = channel
else:
raise FatalErrorWithLog(
self.bot,
f"Could not find channel #{self.bot.AOC_CNAME} for advent of code cog.",
)
role = discord.utils.get(self.bot.uqcs_server.roles, name=self.bot.AOC_ROLE)
if isinstance(role, discord.Role):
self.role = role
else:
raise FatalErrorWithLog(
self.bot,
f"Could not find role @{self.bot.AOC_ROLE} for advent of code cog",
)

def _get_leaderboard_json(self, year: int, code: int) -> Json:
"""
Returns a json dump of the leaderboard
Expand Down Expand Up @@ -263,56 +283,16 @@ async def reminder_fifteen_minutes(self):
"""
The function used within the AOC reminder 15 minutes before each challenge starts.
"""
channel = discord.utils.get(
self.bot.uqcs_server.channels, name=self.bot.AOC_CNAME
)
if channel is None:
logging.warning(f"Could not find required channel #{self.bot.AOC_CNAME}")
return
if not isinstance(channel, discord.TextChannel):
logging.warning(
f"Channel #{self.bot.AOC_CNAME} was expected to be a text channel, but was not"
)
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."
)
# 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 self.channel.send(
f"{self.role.mention}Today's Advent of Code puzzle is released in 15 minutes."
)

async def reminder_released(self):
"""
The function used within the AOC reminder when each challenge starts.
"""
channel = discord.utils.get(
self.bot.uqcs_server.channels, name=self.bot.AOC_CNAME
)
if channel is None:
logging.warning(f"Could not find required channel #{self.bot.AOC_CNAME}")
return
if not isinstance(channel, discord.TextChannel):
logging.warning(
f"Channel #{self.bot.AOC_CNAME} was expected to be a text channel, but was not"
)
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."
)
# 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 has been released. Good luck!"
await self.channel.send(
f"{self.role.mention}Today's Advent of Code puzzle has been released. Good luck!"
)

def _get_previous_winner_aoc_ids(self, year: int) -> List[int]:
Expand Down Expand Up @@ -640,21 +620,22 @@ async def register_command(self, interaction: discord.Interaction, aoc_name: str
@advent_command_group.command(name="register-force")
@app_commands.describe(
year="The year of Advent of Code this registration is for.",
discord_id="The discord ID number of the user. Note that this is not their username.",
discord_id_str="The discord ID number of the user. Note that this is not their username.",
aoc_name="The name shown on Advent of Code.",
aoc_id="The AOC id of the user.",
)
async def register_admin_command(
self,
interaction: discord.Interaction,
year: int,
discord_id: int,
discord_id_str: str, # str as discord can't handle integers this big
andrewj-brown marked this conversation as resolved.
Show resolved Hide resolved
aoc_name: Optional[str] = None,
aoc_id: Optional[int] = None,
):
"""
Forces a registration entry to be created. For admin use only. Either aoc_name or aoc_id should be given.
"""
discord_id = int(discord_id_str)
andrewj-brown marked this conversation as resolved.
Show resolved Hide resolved
if (aoc_name is None and aoc_id is None) or (
aoc_name is not None and aoc_id is not None
):
Expand Down Expand Up @@ -752,15 +733,16 @@ async def unregister_command(self, interaction: discord.Interaction):
@advent_command_group.command(name="unregister-force")
@app_commands.describe(
year="Year that the registration is for",
discord_id="The discord id to remove. Note that this is not the username.",
discord_id_str="The discord id to remove. Note that this is not the username.",
)
async def unregister_admin_command(
self, interaction: discord.Interaction, year: int, discord_id: int
self, interaction: discord.Interaction, year: int, discord_id_str: str
):
"""
Forces a registration entry to be removed.
For admin use only; assumes you know what you are doing.
"""
discord_id = int(discord_id_str)
await interaction.response.defer(thinking=True)
discord_user = self.bot.uqcs_server.get_member(discord_id)

Expand Down Expand Up @@ -930,6 +912,9 @@ async def add_winners_command(
content=f"There were not enough eligible users to select winners (at least {required_number_of_potential_winners} needed; only {len(potential_winners)} found)."
)
return
number_of_potential_winners = len(
potential_winners
) # potential winners will be changed ahead, so we store this value for the award message

match weights:
case "Stars":
Expand Down Expand Up @@ -977,7 +962,7 @@ async def add_winners_command(
winners_message += " and "

await interaction.edit_original_response(
content=f"The results are in! Out of {len(potential_winners)} potential participants, {winners_message} have recieved a prize from participating in Advent of Code: {prize}"
content=f"The results are in! Out of {number_of_potential_winners} potential participants, {winners_message} have recieved a prize from participating in Advent of Code: {prize}"
)

@app_commands.checks.has_permissions(manage_guild=True)
Expand Down
7 changes: 2 additions & 5 deletions uqcsbot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,11 @@ class AOCWinners(Base):
class AOCRegistrations(Base):
__tablename__ = "aoc_registrations"

id: Mapped[int] = mapped_column(
"id", Integer, primary_key=True, nullable=False, autoincrement=True
discord_userid: Mapped[int] = mapped_column(
"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)
discord_userid: Mapped[int] = mapped_column(
"discord_userid", BigInteger, nullable=False
)


class MCWhitelist(Base):
Expand Down