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