From 9d6ea0f020a448cb1498f0c4eeb560858c510a69 Mon Sep 17 00:00:00 2001 From: Numerlor Date: Thu, 3 Oct 2019 20:27:09 +0200 Subject: limit Advent Of Code commands to global whitelist and the AoC channel --- bot/seasons/christmas/adventofcode.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'bot/seasons/christmas/adventofcode.py') diff --git a/bot/seasons/christmas/adventofcode.py b/bot/seasons/christmas/adventofcode.py index 513c1020..a88e5ac3 100644 --- a/bot/seasons/christmas/adventofcode.py +++ b/bot/seasons/christmas/adventofcode.py @@ -13,7 +13,7 @@ from bs4 import BeautifulSoup from discord.ext import commands from pytz import timezone -from bot.constants import AdventOfCode as AocConfig, Channels, Colours, Emojis, Tokens +from bot.constants import AdventOfCode as AocConfig, Channels, Colours, Emojis, Tokens, WHITELISTED_CHANNELS from bot.decorators import override_in_channel log = logging.getLogger(__name__) @@ -24,6 +24,8 @@ AOC_SESSION_COOKIE = {"session": Tokens.aoc_session_cookie} EST = timezone("EST") COUNTDOWN_STEP = 60 * 5 +AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code,) + def is_in_advent() -> bool: """Utility function to check if we are between December 1st and December 25th.""" @@ -126,7 +128,7 @@ class AdventOfCode(commands.Cog): self.status_task = asyncio.ensure_future(self.bot.loop.create_task(status_coro)) @commands.group(name="adventofcode", aliases=("aoc",), invoke_without_command=True) - @override_in_channel() + @override_in_channel(AOC_WHITELIST) async def adventofcode_group(self, ctx: commands.Context) -> None: """All of the Advent of Code commands.""" await ctx.send_help(ctx.command) -- cgit v1.2.3 From 3afb3ce056d225464333b8f633ec3dac40024da8 Mon Sep 17 00:00:00 2001 From: Numerlor Date: Sat, 12 Oct 2019 22:24:00 +0200 Subject: allow all AoC commands in whitelisted channels --- bot/seasons/christmas/adventofcode.py | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'bot/seasons/christmas/adventofcode.py') diff --git a/bot/seasons/christmas/adventofcode.py b/bot/seasons/christmas/adventofcode.py index a88e5ac3..007e4783 100644 --- a/bot/seasons/christmas/adventofcode.py +++ b/bot/seasons/christmas/adventofcode.py @@ -138,6 +138,7 @@ class AdventOfCode(commands.Cog): aliases=("sub", "notifications", "notify", "notifs"), brief="Notifications for new days" ) + @override_in_channel(AOC_WHITELIST) async def aoc_subscribe(self, ctx: commands.Context) -> None: """Assign the role for notifications about new days being ready.""" role = ctx.guild.get_role(AocConfig.role_id) @@ -152,6 +153,7 @@ class AdventOfCode(commands.Cog): f"If you don't want them any more, run `{unsubscribe_command}` instead.") @adventofcode_group.command(name="unsubscribe", aliases=("unsub",), brief="Notifications for new days") + @override_in_channel(AOC_WHITELIST) async def aoc_unsubscribe(self, ctx: commands.Context) -> None: """Remove the role for notifications about new days being ready.""" role = ctx.guild.get_role(AocConfig.role_id) @@ -163,6 +165,7 @@ class AdventOfCode(commands.Cog): await ctx.send("Hey, you don't even get any notifications about new Advent of Code tasks currently anyway.") @adventofcode_group.command(name="countdown", aliases=("count", "c"), brief="Return time left until next day") + @override_in_channel(AOC_WHITELIST) async def aoc_countdown(self, ctx: commands.Context) -> None: """Return time left until next day.""" if not is_in_advent(): @@ -180,11 +183,13 @@ class AdventOfCode(commands.Cog): await ctx.send(f"There are {hours} hours and {minutes} minutes left until day {tomorrow.day}.") @adventofcode_group.command(name="about", aliases=("ab", "info"), brief="Learn about Advent of Code") + @override_in_channel(AOC_WHITELIST) async def about_aoc(self, ctx: commands.Context) -> None: """Respond with an explanation of all things Advent of Code.""" await ctx.send("", embed=self.cached_about_aoc) @adventofcode_group.command(name="join", aliases=("j",), brief="Learn how to join PyDis' private AoC leaderboard") + @override_in_channel(AOC_WHITELIST) async def join_leaderboard(self, ctx: commands.Context) -> None: """DM the user the information for joining the PyDis AoC private leaderboard.""" author = ctx.message.author @@ -205,6 +210,7 @@ class AdventOfCode(commands.Cog): aliases=("board", "lb"), brief="Get a snapshot of the PyDis private AoC leaderboard", ) + @override_in_channel(AOC_WHITELIST) async def aoc_leaderboard(self, ctx: commands.Context, number_of_people_to_display: int = 10) -> None: """ Pull the top number_of_people_to_display members from the PyDis leaderboard and post an embed. @@ -246,6 +252,7 @@ class AdventOfCode(commands.Cog): aliases=("dailystats", "ds"), brief="Get daily statistics for the PyDis private leaderboard" ) + @override_in_channel(AOC_WHITELIST) async def private_leaderboard_daily_stats(self, ctx: commands.Context) -> None: """ Respond with a table of the daily completion statistics for the PyDis private leaderboard. @@ -289,6 +296,7 @@ class AdventOfCode(commands.Cog): aliases=("globalboard", "gb"), brief="Get a snapshot of the global AoC leaderboard", ) + @override_in_channel(AOC_WHITELIST) async def global_leaderboard(self, ctx: commands.Context, number_of_people_to_display: int = 10) -> None: """ Pull the top number_of_people_to_display members from the global AoC leaderboard and post an embed. -- cgit v1.2.3 From cff62e0baf1eb3c6105431c695ef3b0e679576db Mon Sep 17 00:00:00 2001 From: "S. Co1" Date: Sat, 30 Nov 2019 15:42:38 -0500 Subject: Fix AOC countdown logic The current time delta until the next AOC event assumes that the next event is next year's. While this is was a safe assumption when written, since the command would not be available until the season is loaded on December 1st, it provides an incorrect answer if the season is loaded prior. The logic has been adjusted to return the closest December 1st that is not in the past. The feedback string has also been adjusted to give hours remaining if we're less than a day away from the event starting. --- bot/seasons/christmas/adventofcode.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) (limited to 'bot/seasons/christmas/adventofcode.py') diff --git a/bot/seasons/christmas/adventofcode.py b/bot/seasons/christmas/adventofcode.py index 007e4783..bbfe0a69 100644 --- a/bot/seasons/christmas/adventofcode.py +++ b/bot/seasons/christmas/adventofcode.py @@ -170,10 +170,21 @@ class AdventOfCode(commands.Cog): """Return time left until next day.""" if not is_in_advent(): datetime_now = datetime.now(EST) - december_first = datetime(datetime_now.year + 1, 12, 1, tzinfo=EST) - delta = december_first - datetime_now + + # Calculate the delta to this & next year's December 1st to see which one is closest and not in the past + this_year = datetime(datetime_now.year, 12, 1, tzinfo=EST) + next_year = datetime(datetime_now.year + 1, 12, 1, tzinfo=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 + + # Add a finer timedelta if there's less than a day left + if delta.days == 0: + delta_str = f"approximately {delta.seconds // 3600} hours" + else: + delta_str = f"{delta.days} days" + await ctx.send(f"The Advent of Code event is not currently running. " - f"The next event will start in {delta.days} days.") + f"The next event will start in {delta_str}.") return tomorrow, time_left = time_left_to_aoc_midnight() -- cgit v1.2.3 From 7dfcfa082a8af1f3ad9723cc28c6b655efd5a4f4 Mon Sep 17 00:00:00 2001 From: decorator-factory <42166884+decorator-factory@users.noreply.github.com> Date: Sun, 1 Dec 2019 16:22:57 +0300 Subject: Fix cmd annotation Fix the `brief` argument for `.aoc join` to make its behaviour more obvious. --- bot/seasons/christmas/adventofcode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/seasons/christmas/adventofcode.py') diff --git a/bot/seasons/christmas/adventofcode.py b/bot/seasons/christmas/adventofcode.py index 007e4783..8898c1eb 100644 --- a/bot/seasons/christmas/adventofcode.py +++ b/bot/seasons/christmas/adventofcode.py @@ -188,7 +188,7 @@ class AdventOfCode(commands.Cog): """Respond with an explanation of all things Advent of Code.""" await ctx.send("", embed=self.cached_about_aoc) - @adventofcode_group.command(name="join", aliases=("j",), brief="Learn how to join PyDis' private AoC leaderboard") + @adventofcode_group.command(name="join", aliases=("j",), brief="Send a DM with information on how to join the leaderboard") @override_in_channel(AOC_WHITELIST) async def join_leaderboard(self, ctx: commands.Context) -> None: """DM the user the information for joining the PyDis AoC private leaderboard.""" -- cgit v1.2.3 From b26dc348cce92feb627d4e4466155c46b2e899c9 Mon Sep 17 00:00:00 2001 From: decorator-factory <42166884+decorator-factory@users.noreply.github.com> Date: Sun, 1 Dec 2019 18:37:37 +0300 Subject: fix line length in command `.aoc join` --- bot/seasons/christmas/adventofcode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/seasons/christmas/adventofcode.py') diff --git a/bot/seasons/christmas/adventofcode.py b/bot/seasons/christmas/adventofcode.py index 8898c1eb..3750964a 100644 --- a/bot/seasons/christmas/adventofcode.py +++ b/bot/seasons/christmas/adventofcode.py @@ -188,7 +188,7 @@ class AdventOfCode(commands.Cog): """Respond with an explanation of all things Advent of Code.""" await ctx.send("", embed=self.cached_about_aoc) - @adventofcode_group.command(name="join", aliases=("j",), brief="Send a DM with information on how to join the leaderboard") + @adventofcode_group.command(name="join", aliases=("j",), brief="Learn how to join the leaderboard (via DM)") @override_in_channel(AOC_WHITELIST) async def join_leaderboard(self, ctx: commands.Context) -> None: """DM the user the information for joining the PyDis AoC private leaderboard.""" -- cgit v1.2.3 From 8c01aea64030ae49c1a14f1a633633915c1a28a6 Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Mon, 2 Dec 2019 10:03:43 +0100 Subject: Cancel season related tasks on cog unload --- bot/seasons/christmas/adventofcode.py | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'bot/seasons/christmas/adventofcode.py') diff --git a/bot/seasons/christmas/adventofcode.py b/bot/seasons/christmas/adventofcode.py index 3750964a..818e5748 100644 --- a/bot/seasons/christmas/adventofcode.py +++ b/bot/seasons/christmas/adventofcode.py @@ -406,6 +406,11 @@ class AdventOfCode(commands.Cog): self.cached_global_leaderboard = await AocGlobalLeaderboard.from_url() else: self.cached_private_leaderboard = await AocPrivateLeaderboard.from_url() + + def cog_unload(self) -> None: + """Cancel season-related tasks on cog unload.""" + self.countdown_task.cancel() + self.status_task.cancel() class AocMember: -- cgit v1.2.3 From 8691b54468ed9f0454b9ba6503131414ea041deb Mon Sep 17 00:00:00 2001 From: Matteo Bertucci Date: Mon, 2 Dec 2019 10:08:31 +0100 Subject: Delete whitespaces --- bot/seasons/christmas/adventofcode.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/seasons/christmas/adventofcode.py') diff --git a/bot/seasons/christmas/adventofcode.py b/bot/seasons/christmas/adventofcode.py index 818e5748..fe2fa956 100644 --- a/bot/seasons/christmas/adventofcode.py +++ b/bot/seasons/christmas/adventofcode.py @@ -406,7 +406,7 @@ class AdventOfCode(commands.Cog): self.cached_global_leaderboard = await AocGlobalLeaderboard.from_url() else: self.cached_private_leaderboard = await AocPrivateLeaderboard.from_url() - + def cog_unload(self) -> None: """Cancel season-related tasks on cog unload.""" self.countdown_task.cancel() -- cgit v1.2.3 From 4af0413204d24ff4931118363149920601a137fd Mon Sep 17 00:00:00 2001 From: kwzrd Date: Mon, 2 Dec 2019 23:22:59 +0100 Subject: Add envelope reaction if AoC join DM sends successfully --- bot/seasons/christmas/adventofcode.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'bot/seasons/christmas/adventofcode.py') diff --git a/bot/seasons/christmas/adventofcode.py b/bot/seasons/christmas/adventofcode.py index fe2fa956..b59c4eba 100644 --- a/bot/seasons/christmas/adventofcode.py +++ b/bot/seasons/christmas/adventofcode.py @@ -204,6 +204,8 @@ class AdventOfCode(commands.Cog): except discord.errors.Forbidden: log.debug(f"{author.name} ({author.id}) has disabled DMs from server members") await ctx.send(f":x: {author.mention}, please (temporarily) enable DMs to receive the join code") + else: + await ctx.message.add_reaction(Emojis.envelope) @adventofcode_group.command( name="leaderboard", -- cgit v1.2.3