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

Turn AoC commands into hybrid commands #101

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
24 changes: 9 additions & 15 deletions bot/exts/advent_of_code/_cog.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import json
import logging
from datetime import UTC, datetime, timedelta
from datetime import UTC, datetime
from pathlib import Path

import arrow
import discord
from async_rediscache import RedisCache
from discord import app_commands
Expand Down Expand Up @@ -128,7 +127,7 @@ async def completionist_task(self) -> None:
log.debug(f"Giving completionist role to {member.name} ({member.mention}).")
await members.handle_role_change(member, member.add_roles, completionist_role)

@commands.group(name="adventofcode", aliases=("aoc",))
@commands.hybrid_group(name="adventofcode", aliases=("aoc",))
@whitelist_override(channels=AOC_WHITELIST)
async def adventofcode_group(self, ctx: commands.Context) -> None:
"""All of the Advent of Code commands."""
Expand Down Expand Up @@ -175,14 +174,8 @@ async def aoc_countdown(self, ctx: commands.Context) -> None:
await ctx.send(f"Day {tomorrow.day} starts <t:{next_day_timestamp}:R>.")
return

datetime_now = arrow.now(_helpers.EST)
# Calculate the delta to this & next year's December 1st to see which one is closest and not in the past
this_year = arrow.get(datetime(datetime_now.year, 12, 1, tzinfo=UTC), _helpers.EST)
next_year = arrow.get(datetime(datetime_now.year + 1, 12, 1, tzinfo=UTC), _helpers.EST)
deltas = (dec_first - datetime_now for dec_first in (this_year, next_year))
delta = min(delta for delta in deltas if delta >= timedelta()) # timedelta() gives 0 duration delta

next_aoc_timestamp = int((datetime_now + delta).timestamp())
next_aoc, _ = _helpers.time_left_to_next_aoc()
next_aoc_timestamp = int(next_aoc.timestamp())

await ctx.send(
"The Advent of Code event is not currently running. "
Expand Down Expand Up @@ -265,7 +258,7 @@ async def aoc_link_account(self, ctx: commands.Context, *, aoc_name: str | None
if aoc_name:
# Let's check the current values in the cache to make sure it isn't already tied to a different account
if aoc_name == await self.account_links.get(ctx.author.id):
await ctx.reply(f"{aoc_name} is already tied to your account.")
await ctx.reply(f"{aoc_name} is already tied to your account.", ephemeral=True)
return
if aoc_name in cache_aoc_names:
log.info(
Expand All @@ -274,7 +267,8 @@ async def aoc_link_account(self, ctx: commands.Context, *, aoc_name: str | None
)
await ctx.reply(
f"{aoc_name} is already tied to another account."
" Please contact an admin if you believe this is an error."
" Please contact an admin if you believe this is an error.",
ephemeral=True,
)
return

Expand Down Expand Up @@ -314,10 +308,10 @@ async def aoc_unlink_account(self, ctx: commands.Context) -> None:
if aoc_cache_name := await self.account_links.get(ctx.author.id):
log.info(f"Unlinking {ctx.author} ({ctx.author.id}) from Advent of Code account {aoc_cache_name}")
await self.account_links.delete(ctx.author.id)
await ctx.reply(f"We have removed the link between your Discord ID and {aoc_cache_name}.")
await ctx.reply(f"We have removed the link between your Discord ID and {aoc_cache_name}.", ephemeral=True)
else:
log.info(f"Attempted to unlink {ctx.author} ({ctx.author.id}), but no link was found.")
await ctx.reply("You don't have an Advent of Code account linked.")
await ctx.reply("You don't have an Advent of Code account linked.", ephemeral=True)

@in_month(Month.DECEMBER, Month.JANUARY, Month.FEBRUARY)
@adventofcode_group.command(
Expand Down
14 changes: 14 additions & 0 deletions bot/exts/advent_of_code/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,20 @@ def is_in_advent() -> bool:
return arrow.now(EST).day in range(1, 25) and arrow.now(EST).month == 12


def time_left_to_next_aoc() -> tuple[datetime.datetime, datetime.timedelta]:
"""
Calculate the amount of time left until the next AoC.

This will be either this year or next year's December 1, whichever one is
closer and not in the past.
"""
datetime_now = arrow.now(EST)
this_year = arrow.get(datetime.datetime(datetime_now.year, 12, 1, tzinfo=datetime.UTC), EST)
next_year = arrow.get(datetime.datetime(datetime_now.year + 1, 12, 1, tzinfo=datetime.UTC), EST)
dec_first = this_year if this_year > datetime_now else next_year
return dec_first, dec_first - datetime_now


def time_left_to_est_midnight() -> tuple[datetime.datetime, datetime.timedelta]:
"""Calculate the amount of time left until midnight EST/UTC-5."""
# Change all time properties back to 00:00
Expand Down