From 17b4703748d6e0b8f92e8679b7b6799c20097d11 Mon Sep 17 00:00:00 2001 From: Will Da Silva Date: Mon, 5 Oct 2020 14:14:01 -0400 Subject: Replace OMDB with TMDB Closes: #136 --- bot/constants.py | 1 - 1 file changed, 1 deletion(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index e113428e..d2e10ae1 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -184,7 +184,6 @@ class Roles(NamedTuple): class Tokens(NamedTuple): giphy = environ.get("GIPHY_TOKEN") aoc_session_cookie = environ.get("AOC_SESSION_COOKIE") - omdb = environ.get("OMDB_API_KEY") youtube = environ.get("YOUTUBE_API_KEY") tmdb = environ.get("TMDB_API_KEY") nasa = environ.get("NASA_API_KEY") -- cgit v1.2.3 From 415f550969555f6929cc2d0273818bf6db57cab0 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 24 Nov 2020 17:38:43 +0200 Subject: Update constants to match with new format of AoC that will run in 2020 --- bot/constants.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index 6999f321..841f2303 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -31,8 +31,10 @@ log = logging.getLogger(__name__) class AdventOfCode: leaderboard_cache_age_threshold_seconds = 3600 - leaderboard_id = 631135 - leaderboard_join_code = str(environ.get("AOC_JOIN_CODE", None)) + leaderboard_public_ids = [645282] + leaderboard_staff_id = 957532 + leaderboard_public_join_codes = environ.get("AOC_PUBLIC_JOIN_CODES", "").split(",") + leaderboard_staff_join_code = environ.get("AOC_STAFF_JOIN_CODE", "") leaderboard_max_displayed_members = 10 year = int(environ.get("AOC_YEAR", datetime.utcnow().year)) role_id = int(environ.get("AOC_ROLE_ID", 518565788744024082)) @@ -44,7 +46,8 @@ class Branding: class Channels(NamedTuple): admins = 365960823622991872 - advent_of_code = int(environ.get("AOC_CHANNEL_ID", 517745814039166986)) + advent_of_code = int(environ.get("AOC_CHANNEL_ID", 780818162836439041)) + advent_of_code_staff = int(environ.get("AOC_STAFF_CHANNEL_ID", 778646502641500181)) announcements = int(environ.get("CHANNEL_ANNOUNCEMENTS", 354619224620138496)) big_brother_logs = 468507907357409333 bot = 267659945086812160 -- cgit v1.2.3 From 2f6236671f89eb1088e985284f87292695e376cf Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Tue, 24 Nov 2020 17:44:08 +0200 Subject: Add comments about AoC env config order and change cookies way --- bot/constants.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index 841f2303..bbfe7c3f 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -33,6 +33,7 @@ class AdventOfCode: leaderboard_cache_age_threshold_seconds = 3600 leaderboard_public_ids = [645282] leaderboard_staff_id = 957532 + # Public join codes in environment must be in same order than in AdventOfCode.leaderboard_public_ids leaderboard_public_join_codes = environ.get("AOC_PUBLIC_JOIN_CODES", "").split(",") leaderboard_staff_join_code = environ.get("AOC_STAFF_JOIN_CODE", "") leaderboard_max_displayed_members = 10 @@ -203,7 +204,9 @@ class Roles(NamedTuple): class Tokens(NamedTuple): giphy = environ.get("GIPHY_TOKEN") - aoc_session_cookie = environ.get("AOC_SESSION_COOKIE") + # Public AoC cookies in environment must be in same order than in AdventOfCode.leaderboard_public_ids + aoc_public_session_cookies = environ.get("AOC_PUBLIC_SESSION_COOKIES", "").split(",") + aoc_staff_session_cookie = environ.get("AOC_STAFF_SESSION_COOKIE") omdb = environ.get("OMDB_API_KEY") youtube = environ.get("YOUTUBE_API_KEY") tmdb = environ.get("TMDB_API_KEY") -- cgit v1.2.3 From ffa3619a19515c147013669339f51d36f83a5f29 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Fri, 27 Nov 2020 19:14:27 +0200 Subject: Fix adventofcode extension (and constants) grammar Co-authored-by: Joe Banks --- bot/constants.py | 4 ++-- bot/exts/christmas/adventofcode.py | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index bbfe7c3f..d24551f6 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -33,7 +33,7 @@ class AdventOfCode: leaderboard_cache_age_threshold_seconds = 3600 leaderboard_public_ids = [645282] leaderboard_staff_id = 957532 - # Public join codes in environment must be in same order than in AdventOfCode.leaderboard_public_ids + # Public join codes in environment must be in the same order as AdventOfCode.leaderboard_public_ids leaderboard_public_join_codes = environ.get("AOC_PUBLIC_JOIN_CODES", "").split(",") leaderboard_staff_join_code = environ.get("AOC_STAFF_JOIN_CODE", "") leaderboard_max_displayed_members = 10 @@ -204,7 +204,7 @@ class Roles(NamedTuple): class Tokens(NamedTuple): giphy = environ.get("GIPHY_TOKEN") - # Public AoC cookies in environment must be in same order than in AdventOfCode.leaderboard_public_ids + # Public AoC cookies in environment must be in the same order as AdventOfCode.leaderboard_public_ids aoc_public_session_cookies = environ.get("AOC_PUBLIC_SESSION_COOKIES", "").split(",") aoc_staff_session_cookie = environ.get("AOC_STAFF_SESSION_COOKIE") omdb = environ.get("OMDB_API_KEY") diff --git a/bot/exts/christmas/adventofcode.py b/bot/exts/christmas/adventofcode.py index feff09aa..b188059e 100644 --- a/bot/exts/christmas/adventofcode.py +++ b/bot/exts/christmas/adventofcode.py @@ -185,11 +185,11 @@ class AdventOfCode(commands.Cog): @seasonal_task(Month.DECEMBER, sleep_time=60 * 30) async def leaderboard_members_updater(self) -> None: """Updates public leaderboards cached member amounts in every 30 minutes.""" - # Whole December isn't advent + # Check whether we are in the 25 days of advent if not is_in_advent(): return - # Update every leaderboard for what we have session cookie + # Update every leaderboard with our session cookies for aoc_id, cookie in self.leaderboard_cookies.items(): leaderboard = await AocPrivateLeaderboard.from_url(aoc_id, cookie) await self.public_leaderboard_members.set(aoc_id, len(leaderboard.members)) @@ -203,7 +203,7 @@ class AdventOfCode(commands.Cog): for aoc_id, cookie in self.leaderboard_cookies.items() ] - # Check does this have any failed requests + # Check if any requests failed if False in leaderboards: log.warning("Unable to get one or more of the public leaderboards. Not updating cache.") return @@ -243,7 +243,7 @@ class AdventOfCode(commands.Cog): "earned": datetime.fromtimestamp(user_data["days"][day]["star_two_earned"]), }) - # Sort these lists based on user star earning time + # Sort these lists based on the time a user earnt a star star_one_users = sorted(star_one_users, key=lambda k: k["earned"])[:100] star_two_users = sorted(star_two_users, key=lambda k: k["earned"])[:100] @@ -263,7 +263,7 @@ class AdventOfCode(commands.Cog): leaderboard_users[star_user_two["id"]]["score"] = points points -= 1 - # Put completions also in to make building easier later. + # Attach star completions for building the response later for user, user_data in leaderboard_users.items(): completions_star_one = sum([1 for day in user_data["days"].values() if day["star_one"]]) completions_star_two = sum([1 for day in user_data["days"].values() if day["star_two"]]) @@ -271,7 +271,7 @@ class AdventOfCode(commands.Cog): leaderboard_users[user]["star_one_completions"] = completions_star_one leaderboard_users[user]["star_two_completions"] = completions_star_two - # Finally clear old cache and persist everything to Redis + # Finally, clear old cache and persist everything to Redis await self.public_user_data.clear() [await self.public_user_data.set(user, json.dumps(user_data)) for user, user_data in leaderboard_users.items()] @@ -300,7 +300,7 @@ class AdventOfCode(commands.Cog): async def get_leaderboard(self, members_amount: int, context: commands.Context) -> typing.Union[str, bool]: """Generates leaderboard based on Redis data.""" - # When we don't have users in cache, log warning and return False. + # When we don't have users in cache, warn and return False. if await self.public_user_data.length() == 0: log.warning("Don't have cache for displaying AoC public leaderboard.") return False @@ -414,12 +414,12 @@ class AdventOfCode(commands.Cog): join_code = AocConfig.leaderboard_staff_join_code log.info(f"{author.name} ({author.id}) ran command in staff AoC channel. Returning staff code.") else: - # We want that user get only 1 code + # Ensure we use the same leaderboard code for the same user if await self.user_leaderboards.contains(ctx.author.id): join_code = self.leaderboard_join_codes[await self.user_leaderboards.get(ctx.author.id)] - log.info(f"{author.name} ({author.id}) have already cached AoC join code. Returning it.") + log.info(f"{author.name} ({author.id}) has a cached AoC join code, returning it.") else: - # Find leaderboard that have least members inside (based on cache) + # Find the leaderboard that has the least members inside from cache least_id, least = 0, 200 for aoc_id, amount in await self.public_leaderboard_members.items(): log.info(amount, least) -- cgit v1.2.3 From 73dff91f530d7125a7ff130e9a56b759b7fdbaab Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 00:35:57 +0100 Subject: Add constants parsing for multiple leaderboards I've added a unified approach to setting data for multiple leaderboards using environmental variables. Instead of setting separate variables for the three pieces of data we have, hoping that the position of each board matches up in the three fields, I now set one environmental variable, AOC_LEADERBOARDS, that holds delimited data. The data is in the format: board_id1,session1,join_code1::board_id2,session2,join_code2[::...] The staff leaderboards should be included as usual in this environmental variable. Another environment variable, AOC_STAFF_LEADERBOARD_ID, can be used to designate which leaderboard should be used as the staff board. I've also made some other constants configurable in this commit and added the role ID of the Events Lead role to allow the Events Lead to force a reload of the leaderboard cache. --- bot/constants.py | 47 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index d24551f6..fc9929ec 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -2,7 +2,7 @@ import enum import logging from datetime import datetime from os import environ -from typing import NamedTuple +from typing import Dict, NamedTuple __all__ = ( "AdventOfCode", @@ -29,14 +29,42 @@ __all__ = ( log = logging.getLogger(__name__) +class AdventOfCodeLeaderboard(NamedTuple): + id: str + session: str + join_code: str + + +def _parse_aoc_leaderboard_env() -> Dict[str, AdventOfCodeLeaderboard]: + """ + Parse the environment variable containing leaderboard information. + + A leaderboard should be specified in the format `id,session,join_code`, + without the backticks. If more than leaderboard needs to be added to the + constants, separate the individual leaderboards with `::`. + + Example ENV: `id1,session1,join_code1::id2,session2,join_code2` + """ + raw_leaderboards = environ.get("AOC_LEADERBOARDS", "") + if not raw_leaderboards: + return {} + + leaderboards = {} + for leaderboard in raw_leaderboards.split("::"): + leaderboard_id, session, join_code = leaderboard.split(",") + leaderboards[leaderboard_id] = AdventOfCodeLeaderboard(leaderboard_id, session, join_code) + + return leaderboards + + class AdventOfCode: - leaderboard_cache_age_threshold_seconds = 3600 - leaderboard_public_ids = [645282] - leaderboard_staff_id = 957532 - # Public join codes in environment must be in the same order as AdventOfCode.leaderboard_public_ids - leaderboard_public_join_codes = environ.get("AOC_PUBLIC_JOIN_CODES", "").split(",") - leaderboard_staff_join_code = environ.get("AOC_STAFF_JOIN_CODE", "") - leaderboard_max_displayed_members = 10 + # Information for the several leaderboards we have + leaderboards = _parse_aoc_leaderboard_env() + staff_leaderboard_id = environ.get("AOC_STAFF_LEADERBOARD_ID", "") + + # Other Advent of Code constants + leaderboard_displayed_members = 10 + leaderboard_cache_expiry_seconds = 1800 year = int(environ.get("AOC_YEAR", datetime.utcnow().year)) role_id = int(environ.get("AOC_ROLE_ID", 518565788744024082)) @@ -197,9 +225,10 @@ class Roles(NamedTuple): muted = 277914926603829249 owner = 267627879762755584 verified = 352427296948486144 - helpers = 267630620367257601 + helpers = int(environ.get("ROLE_HELPERS", 267630620367257601)) rockstars = 458226413825294336 core_developers = 587606783669829632 + events_lead = 778361735739998228 class Tokens(NamedTuple): -- cgit v1.2.3 From b13667cfc261ec43daad6fd592d5b952f73997fe Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 01:18:40 +0100 Subject: Remove now redundant advent_of_code_staff constant We're no longer going to use a two-channel setup for this event, as we don't want to split the event community into two, staff and non-staff. --- bot/constants.py | 1 - bot/exts/christmas/advent_of_code/_cog.py | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index fc9929ec..e459ed21 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -76,7 +76,6 @@ class Branding: class Channels(NamedTuple): admins = 365960823622991872 advent_of_code = int(environ.get("AOC_CHANNEL_ID", 780818162836439041)) - advent_of_code_staff = int(environ.get("AOC_STAFF_CHANNEL_ID", 778646502641500181)) announcements = int(environ.get("CHANNEL_ANNOUNCEMENTS", 354619224620138496)) big_brother_logs = 468507907357409333 bot = 267659945086812160 diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 1a6715bb..388d0592 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -24,7 +24,7 @@ AOC_REQUEST_HEADER = {"user-agent": "PythonDiscord AoC Event Bot"} EST = timezone("EST") COUNTDOWN_STEP = 60 * 5 -AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code, Channels.advent_of_code_staff) +AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code,) def is_in_advent() -> bool: -- cgit v1.2.3 From 5ce12af9d9f48042b0ebb7ef215e3253bcc418bd Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 01:20:19 +0100 Subject: Set correct channel ID as default for AoC channel --- bot/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index e459ed21..00d75a3f 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -75,7 +75,7 @@ class Branding: class Channels(NamedTuple): admins = 365960823622991872 - advent_of_code = int(environ.get("AOC_CHANNEL_ID", 780818162836439041)) + advent_of_code = int(environ.get("AOC_CHANNEL_ID", 782715290437943306)) announcements = int(environ.get("CHANNEL_ANNOUNCEMENTS", 354619224620138496)) big_brother_logs = 468507907357409333 bot = 267659945086812160 -- cgit v1.2.3 From c352c80cf97620e715ee55184b54d1609312c76b Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 01:38:11 +0100 Subject: Fix docstrings and add a few explanatory comments --- bot/constants.py | 4 ++-- bot/exts/christmas/advent_of_code/__init__.py | 2 +- bot/exts/christmas/advent_of_code/_cog.py | 4 ++-- bot/exts/christmas/advent_of_code/_helpers.py | 3 +++ 4 files changed, 8 insertions(+), 5 deletions(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index 00d75a3f..cb5a91cc 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -40,8 +40,8 @@ def _parse_aoc_leaderboard_env() -> Dict[str, AdventOfCodeLeaderboard]: Parse the environment variable containing leaderboard information. A leaderboard should be specified in the format `id,session,join_code`, - without the backticks. If more than leaderboard needs to be added to the - constants, separate the individual leaderboards with `::`. + without the backticks. If more than one leaderboard needs to be added to + the constant, separate the individual leaderboards with `::`. Example ENV: `id1,session1,join_code1::id2,session2,join_code2` """ diff --git a/bot/exts/christmas/advent_of_code/__init__.py b/bot/exts/christmas/advent_of_code/__init__.py index 20ac5ab9..3c521168 100644 --- a/bot/exts/christmas/advent_of_code/__init__.py +++ b/bot/exts/christmas/advent_of_code/__init__.py @@ -2,7 +2,7 @@ from bot.bot import Bot def setup(bot: Bot) -> None: - """Advent of Code Cog load.""" + """Set up the Advent of Code extension.""" # Import the Cog at runtime to prevent side effects like defining # RedisCache instances too early. from ._cog import AdventOfCode diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 388d0592..19baca93 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -230,7 +230,7 @@ class AdventOfCode(commands.Cog): @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.""" + """DM the user the information for joining the Python Discord leaderboard.""" author = ctx.message.author log.info(f"{author.name} ({author.id}) has requested a PyDis AoC leaderboard code") @@ -284,7 +284,7 @@ class AdventOfCode(commands.Cog): @adventofcode_group.command( name="stats", aliases=("dailystats", "ds"), - brief="Get daily statistics for the PyDis private leaderboard" + brief="Get daily statistics for the Python Discord leaderboard" ) @override_in_channel(AOC_WHITELIST) async def private_leaderboard_daily_stats(self, ctx: commands.Context) -> None: diff --git a/bot/exts/christmas/advent_of_code/_helpers.py b/bot/exts/christmas/advent_of_code/_helpers.py index 8b85bf5d..57aad54d 100644 --- a/bot/exts/christmas/advent_of_code/_helpers.py +++ b/bot/exts/christmas/advent_of_code/_helpers.py @@ -102,10 +102,13 @@ def _parse_raw_leaderboard_data(raw_leaderboard_data: dict) -> dict: sorted(leaderboard.items(), key=lambda t: t[1]["score"], reverse=True) ) + # Create summary stats for the stars completed for each day of the event. daily_stats = {} for day in range(1, 26): star_one = len(star_results.get((day, 1), [])) star_two = len(star_results.get((day, 1), [])) + # By using a dictionary instead of namedtuple here, we can serialize + # this data to JSON in order to cache it in Redis. daily_stats[day] = {"star_one": star_one, "star_two": star_two} return {"daily_stats": daily_stats, "leaderboard": sorted_leaderboard} -- cgit v1.2.3 From b0e30c21f0638a0b45096a01a00327acee51b46d Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Mon, 30 Nov 2020 13:50:34 +0100 Subject: Remove constants replaced by AOC_LEADERBOARDS There were still two constants left over that were no longer used after the rewrite. I've removed them. --- bot/constants.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index cb5a91cc..292a242a 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -232,9 +232,6 @@ class Roles(NamedTuple): class Tokens(NamedTuple): giphy = environ.get("GIPHY_TOKEN") - # Public AoC cookies in environment must be in the same order as AdventOfCode.leaderboard_public_ids - aoc_public_session_cookies = environ.get("AOC_PUBLIC_SESSION_COOKIES", "").split(",") - aoc_staff_session_cookie = environ.get("AOC_STAFF_SESSION_COOKIE") omdb = environ.get("OMDB_API_KEY") youtube = environ.get("YOUTUBE_API_KEY") tmdb = environ.get("TMDB_API_KEY") -- cgit v1.2.3 From 210c564e1b6823617cb4f64ff5e33031dc61d487 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Tue, 1 Dec 2020 18:30:12 +0100 Subject: Add support for ignoring scores from specific days I've added support for ignoring scores from specific days. A list of days to ignore can be provided using the environment variable `AOC_IGNORED_DAYS` as a comma-separated list. This example would ignore day 1 and day 23: AOC_IGNORED_DAYS=1,23 I've also added a helper function to sort the leaderboard not only on the achieved score, but also on the number of stars an individual has completed. --- bot/constants.py | 1 + bot/exts/christmas/advent_of_code/_helpers.py | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index 292a242a..e313e086 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -63,6 +63,7 @@ class AdventOfCode: staff_leaderboard_id = environ.get("AOC_STAFF_LEADERBOARD_ID", "") # Other Advent of Code constants + ignored_days = [day for day in environ.get("AOC_IGNORED_DAYS", "").split(",")] leaderboard_displayed_members = 10 leaderboard_cache_expiry_seconds = 1800 year = int(environ.get("AOC_YEAR", datetime.utcnow().year)) diff --git a/bot/exts/christmas/advent_of_code/_helpers.py b/bot/exts/christmas/advent_of_code/_helpers.py index 72c1ce20..e84348cb 100644 --- a/bot/exts/christmas/advent_of_code/_helpers.py +++ b/bot/exts/christmas/advent_of_code/_helpers.py @@ -53,6 +53,17 @@ EST = pytz.timezone("EST") _StarResult = collections.namedtuple("StarResult", "name completion_time") +def leaderboard_sorting_function(entry: typing.Tuple[str, dict]) -> typing.Tuple[int, int]: + """ + Provide a sorting value for our leaderboard. + + The leaderboard is sorted primarily on the score someone has received and + secondary on the number of stars someone has completed. + """ + result = entry[1] + return result["score"], result["star_2_count"] + result["star_1_count"] + + def _parse_raw_leaderboard_data(raw_leaderboard_data: dict) -> dict: """ Parse the leaderboard data received from the AoC website. @@ -98,13 +109,15 @@ def _parse_raw_leaderboard_data(raw_leaderboard_data: dict) -> dict: # participants per star, we can compute the rank-based scores each participant # should get for that star. max_score = len(leaderboard) - for star in star_results.values(): - for rank, star_result in enumerate(sorted(star, key=operator.itemgetter(1))): + for(day, _star), results in star_results.items(): + if day in AdventOfCode.ignored_days: + continue + for rank, star_result in enumerate(sorted(results, key=operator.itemgetter(1))): leaderboard[star_result.name]["score"] += max_score - rank # Since dictionaries now retain insertion order, let's use that sorted_leaderboard = dict( - sorted(leaderboard.items(), key=lambda t: t[1]["score"], reverse=True) + sorted(leaderboard.items(), key=leaderboard_sorting_function, reverse=True) ) # Create summary stats for the stars completed for each day of the event. -- cgit v1.2.3 From d4c8c0f7184e5d494136cc2b7fc670e8ab7a8f93 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Wed, 2 Dec 2020 00:22:55 +0100 Subject: Update docstrings and fix grammar in comments I've updated some docstrings to include more information about the inner workings of some of the functions. In addition, I've also slightly reformulated some block comments to improve their grammar. Kaizen change: There was a redundant list comprehension in the Advent of Code section of the constants. I've removed it. --- bot/constants.py | 2 +- bot/exts/christmas/advent_of_code/_cog.py | 4 ++-- bot/exts/christmas/advent_of_code/_helpers.py | 24 +++++++++++++++++------- 3 files changed, 20 insertions(+), 10 deletions(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index e313e086..c696b202 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -63,7 +63,7 @@ class AdventOfCode: staff_leaderboard_id = environ.get("AOC_STAFF_LEADERBOARD_ID", "") # Other Advent of Code constants - ignored_days = [day for day in environ.get("AOC_IGNORED_DAYS", "").split(",")] + ignored_days = environ.get("AOC_IGNORED_DAYS", "").split(",") leaderboard_displayed_members = 10 leaderboard_cache_expiry_seconds = 1800 year = int(environ.get("AOC_YEAR", datetime.utcnow().year)) diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 2f60e512..29dcc3cf 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -35,8 +35,8 @@ class AdventOfCode(commands.Cog): self.countdown_task = None self.status_task = None - countdown_coro = _helpers.day_countdown(self.bot) - self.countdown_task = self.bot.loop.create_task(countdown_coro) + announcement_coro = _helpers.new_puzzle_announcement(self.bot) + self.new_puzzle_announcement_task = self.bot.loop.create_task(announcement_coro) status_coro = _helpers.countdown_status(self.bot) self.status_task = self.bot.loop.create_task(status_coro) diff --git a/bot/exts/christmas/advent_of_code/_helpers.py b/bot/exts/christmas/advent_of_code/_helpers.py index c75f47fa..7a6d873e 100644 --- a/bot/exts/christmas/advent_of_code/_helpers.py +++ b/bot/exts/christmas/advent_of_code/_helpers.py @@ -374,9 +374,16 @@ async def wait_for_advent_of_code(*, hours_before: int = 1) -> None: """ Wait for the Advent of Code event to start. - This function returns `hours_before` (default: 1) before the Advent of Code + This function returns `hours_before` (default: 1) the Advent of Code actually starts. This allows functions to schedule and execute code that needs to run before the event starts. + + If the event has already started, this function returns immediately. + + Note: The "next Advent of Code" is determined based on the current value + of the `AOC_YEAR` environment variable. This allows callers to exit early + if we're already past the Advent of Code edition the bot is currently + configured for. """ start = datetime.datetime(AdventOfCode.year, 12, 1, 0, 0, 0, tzinfo=EST) target = start - datetime.timedelta(hours=hours_before) @@ -449,12 +456,13 @@ async def countdown_status(bot: Bot) -> None: await asyncio.sleep(delay) -async def day_countdown(bot: Bot) -> None: +async def new_puzzle_announcement(bot: Bot) -> None: """ - Calculate the number of seconds left until the next day of Advent. + Announce the release of a new Advent of Code puzzle. - Once we have calculated this we should then sleep that number and when the time is reached, ping - the Advent of Code role notifying them that the new challenge is ready. + This background task hibernates until just before the Advent of Code starts + and will then start announcing puzzles as they are published. After the + event has finished, this task will terminate. """ # We wake up one hour before the event starts to prepare the announcement # of the release of the first puzzle. @@ -483,8 +491,10 @@ async def day_countdown(bot: Bot) -> None: log.trace("Started puzzle notification loop.") tomorrow, time_left = time_left_to_est_midnight() - # Use fractional `total_seconds` to wake up very close to our target, with - # padding of 0.1 seconds to ensure that we actually pass midnight. + # Use `total_seconds` to get the time left in fractional seconds This + # should wake us up very close to the target. As a safe guard, the sleep + # duration is padded with 0.1 second to make sure we wake up after + # midnight. sleep_seconds = time_left.total_seconds() + 0.1 log.trace(f"The puzzle notification task will sleep for {sleep_seconds} seconds") await asyncio.sleep(sleep_seconds) -- cgit v1.2.3 From 1aad543a8d4b3c7317fe98937144591d85ad3330 Mon Sep 17 00:00:00 2001 From: Sebastiaan Zeeff Date: Sat, 5 Dec 2020 00:21:04 +0100 Subject: Support a fallback session cookie in constants To mitigate problems due to expiring session cookies, I'm currently in the process of adding support for a fallback cookie. Basically, my Advent of Code account is a member of *all* leaderboards, which means that my cookie can be used to fetch all leaderboards as well. As my session cookie should not expire until after the event, it should not give us any issues. However, to avoid issues with issuing too many requests from one session, we should still make sure to set individual session values regardless of the mitigation. --- bot/constants.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index e313e086..9e6db7a6 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -1,3 +1,4 @@ +import dataclasses import enum import logging from datetime import datetime @@ -29,11 +30,27 @@ __all__ = ( log = logging.getLogger(__name__) -class AdventOfCodeLeaderboard(NamedTuple): +@dataclasses.dataclass +class AdventOfCodeLeaderboard: id: str - session: str + _session: str join_code: str + # If we notice that the session for this board expired, we set + # this attribute to `True`. We will emit a Sentry error so we + # can handle it, but, in the meantime, we'll try using the + # fallback session to make sure the commands still work. + use_fallback_session: bool = False + + @property + def session(self) -> str: + """Return either the actual `session` cookie or the fallback cookie.""" + if self.use_fallback_session: + log.info(f"Returning fallback cookie for board `{self.id}`.") + return AdventOfCode.fallback_session + + return self._session + def _parse_aoc_leaderboard_env() -> Dict[str, AdventOfCodeLeaderboard]: """ @@ -61,6 +78,7 @@ class AdventOfCode: # Information for the several leaderboards we have leaderboards = _parse_aoc_leaderboard_env() staff_leaderboard_id = environ.get("AOC_STAFF_LEADERBOARD_ID", "") + fallback_session = environ.get("AOC_FALLBACK_SESSION", "") # Other Advent of Code constants ignored_days = [day for day in environ.get("AOC_IGNORED_DAYS", "").split(",")] -- cgit v1.2.3 From d884c21e8ae2bd82370b11621230d7172cb098a3 Mon Sep 17 00:00:00 2001 From: janine9vn Date: Wed, 2 Dec 2020 09:28:32 -0500 Subject: Disallow .aoc commands in primary aoc channel Commands like `.aoc leaderboard` and `.aoc stats` proved to be spammy in the main advent of code channel. An aoc_commands channel has been added for aoc commands and this update prohibits aoc commands from being used in the primary aoc channel and adds the comands channel to the whitelist. This also specifically allows the less spammier commands: join, subscribe, unsubscribe, and countdown in the primary channel to foster discussion though. --- bot/constants.py | 1 + bot/exts/christmas/advent_of_code/_cog.py | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index 9e6db7a6..5dc42462 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -95,6 +95,7 @@ class Branding: class Channels(NamedTuple): admins = 365960823622991872 advent_of_code = int(environ.get("AOC_CHANNEL_ID", 782715290437943306)) + advent_of_code_commands = int(environ.get("AOC_COMMANDS_CHANNEL_ID", 783503267849437205)) announcements = int(environ.get("CHANNEL_ANNOUNCEMENTS", 354619224620138496)) big_brother_logs = 468507907357409333 bot = 267659945086812160 diff --git a/bot/exts/christmas/advent_of_code/_cog.py b/bot/exts/christmas/advent_of_code/_cog.py index 0bcd9f42..fad13f23 100644 --- a/bot/exts/christmas/advent_of_code/_cog.py +++ b/bot/exts/christmas/advent_of_code/_cog.py @@ -21,7 +21,7 @@ AOC_REQUEST_HEADER = {"user-agent": "PythonDiscord AoC Event Bot"} COUNTDOWN_STEP = 60 * 5 -AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code,) +AOC_WHITELIST = WHITELISTED_CHANNELS + (Channels.advent_of_code_commands,) async def countdown_status(bot: commands.Bot) -> None: @@ -139,7 +139,7 @@ class AdventOfCode(commands.Cog): aliases=("sub", "notifications", "notify", "notifs"), brief="Notifications for new days" ) - @override_in_channel(AOC_WHITELIST) + @override_in_channel(AOC_WHITELIST + (Channels.advent_of_code)) async def aoc_subscribe(self, ctx: commands.Context) -> None: """Assign the role for notifications about new days being ready.""" current_year = datetime.now().year @@ -160,7 +160,7 @@ class AdventOfCode(commands.Cog): @in_month(Month.DECEMBER) @adventofcode_group.command(name="unsubscribe", aliases=("unsub",), brief="Notifications for new days") - @override_in_channel(AOC_WHITELIST) + @override_in_channel(AOC_WHITELIST + Channels.advent_of_code) 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) @@ -172,7 +172,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) + @override_in_channel(AOC_WHITELIST + (Channels.advent_of_code)) async def aoc_countdown(self, ctx: commands.Context) -> None: """Return time left until next day.""" if not _helpers.is_in_advent(): @@ -207,7 +207,7 @@ class AdventOfCode(commands.Cog): await ctx.send("", embed=self.cached_about_aoc) @adventofcode_group.command(name="join", aliases=("j",), brief="Learn how to join the leaderboard (via DM)") - @override_in_channel(AOC_WHITELIST) + @override_in_channel(AOC_WHITELIST + (Channels.advent_of_code)) async def join_leaderboard(self, ctx: commands.Context) -> None: """DM the user the information for joining the Python Discord leaderboard.""" current_year = datetime.now().year -- cgit v1.2.3 From 0ca1c0b55ea5d07694dc53d4f4856cbcddb35a8f Mon Sep 17 00:00:00 2001 From: janine9vn Date: Fri, 11 Dec 2020 19:30:23 -0500 Subject: Change Default AoC Commands Channel Changes the default value of the advent_of_code_commands constant to be the same channel ID as sir-lancebot-commands. If no AoC commands channel is set in the .env file, it'll re-direct people to sir-lancebot-commands instead. --- bot/constants.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index 5dc42462..a58801f7 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -95,7 +95,7 @@ class Branding: class Channels(NamedTuple): admins = 365960823622991872 advent_of_code = int(environ.get("AOC_CHANNEL_ID", 782715290437943306)) - advent_of_code_commands = int(environ.get("AOC_COMMANDS_CHANNEL_ID", 783503267849437205)) + advent_of_code_commands = int(environ.get("AOC_COMMANDS_CHANNEL_ID", 607247579608121354)) announcements = int(environ.get("CHANNEL_ANNOUNCEMENTS", 354619224620138496)) big_brother_logs = 468507907357409333 bot = 267659945086812160 -- cgit v1.2.3 From 7d3180c91d65f959a8374167edd22486c1175ca7 Mon Sep 17 00:00:00 2001 From: ks129 <45097959+ks129@users.noreply.github.com> Date: Sat, 19 Dec 2020 19:18:45 +0200 Subject: Add Git SHA as constant --- bot/constants.py | 2 ++ 1 file changed, 2 insertions(+) (limited to 'bot/constants.py') diff --git a/bot/constants.py b/bot/constants.py index 5e97fa2d..0325066a 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -313,6 +313,8 @@ WHITELISTED_CHANNELS = ( Channels.sprint_documentation, ) +GIT_SHA = environ.get("GIT_SHA", "foobar") + # Bot replies ERROR_REPLIES = [ "Please don't do that.", -- cgit v1.2.3