From 49b5f17357eef58f16354cfc04cd7c3da0919fc7 Mon Sep 17 00:00:00 2001 From: D0rs4n <41237606+D0rs4n@users.noreply.github.com> Date: Sun, 10 Apr 2022 22:24:55 +0200 Subject: [PATCH] Add support for storing AoC blocks on site - This commits adds an APIClient to the AoC cog, and implements site as the backend storage for AoC role blocks --- bot/__main__.py | 2 +- bot/bot.py | 1 - bot/constants.py | 6 ++++++ bot/exts/advent_of_code/_cog.py | 7 +++++-- docker-compose.yml | 1 + 5 files changed, 13 insertions(+), 4 deletions(-) diff --git a/bot/__main__.py b/bot/__main__.py index cf6bae9..0e37594 100644 --- a/bot/__main__.py +++ b/bot/__main__.py @@ -8,6 +8,7 @@ if not Client.in_ci: async def main() -> None: + """Entry Async method for starting the bot.""" bot._resolver = aiohttp.AsyncResolver() # Use AF_INET as its socket family to prevent HTTPS related problems both locally @@ -22,7 +23,6 @@ async def main() -> None: bot.http.connector = bot._connector bot.http_session = aiohttp.ClientSession(connector=bot._connector) - """Entry Async method for starting the bot.""" async with bot: bot._guild_available = asyncio.Event() await bot.start(Client.token) diff --git a/bot/bot.py b/bot/bot.py index 99717a5..6dd5c52 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -3,7 +3,6 @@ import aiohttp import discord -import aiohttp from botcore.utils.extensions import walk_extensions from botcore.utils.logging import get_logger from botcore.utils.scheduling import create_task diff --git a/bot/constants.py b/bot/constants.py index 6ce2205..642da0c 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -227,6 +227,12 @@ def _parse_aoc_leaderboard_env() -> dict[str, AdventOfCodeLeaderboard]: leaderboards[leaderboard_id] = AdventOfCodeLeaderboard(leaderboard_id, session, join_code) +class URLs(NamedTuple): + site_api_schema = environ.get("SITE_API_SCHEMA", "http://") + site_api = environ.get("SITE_API", "web:8000") + site_api_token = environ.get("SITE_API_TOKEN", "suitable-for-development-only") + + class AdventOfCode(NamedTuple): # Information for the several leaderboards we have leaderboards = _parse_aoc_leaderboard_env() diff --git a/bot/exts/advent_of_code/_cog.py b/bot/exts/advent_of_code/_cog.py index e214a07..048c0f7 100644 --- a/bot/exts/advent_of_code/_cog.py +++ b/bot/exts/advent_of_code/_cog.py @@ -6,6 +6,7 @@ import arrow import discord from async_rediscache import RedisCache +from botcore.site_api import APIClient from botcore.utils import members from botcore.utils.logging import get_logger from discord.ext import commands, tasks @@ -13,7 +14,7 @@ from bot.bot import SirRobin from bot.constants import WHITELISTED_CHANNELS from bot.constants import AdventOfCode as AocConfig -from bot.constants import Channels, Client, Colours, Emojis, Month, Roles +from bot.constants import Channels, Client, Colours, Emojis, Month, Roles, URLs from bot.exts.advent_of_code import _helpers from bot.exts.advent_of_code.views.dayandstarview import AoCDropdownView from bot.utils.decorators import (InChannelCheckFailure, in_interval, @@ -44,7 +45,9 @@ class AdventOfCode(commands.Cog): completionist_block_list = RedisCache() def __init__(self, bot: SirRobin): + self.bot = bot + self.api = APIClient(site_api_url=(URLs.site_api_schema + URLs.site_api), site_api_token=URLs.site_api_token) self._base_url = f"https://adventofcode.com/{AocConfig.year}" self.global_leaderboard_url = f"https://adventofcode.com/{AocConfig.year}/leaderboard" @@ -136,7 +139,7 @@ async def block_from_role(self, ctx: commands.Context, member: discord.Member) - if completionist_role in member.roles: await member.remove_roles(completionist_role) - await self.completionist_block_list.set(member.id, "sentinel") + await self.api.post("api/bot/aoc-completionist-blocks", json={"user": member.id}) await ctx.send(f":+1: Blocked {member.mention} from getting the AoC completionist role.") @commands.guild_only() diff --git a/docker-compose.yml b/docker-compose.yml index 1cd8fcb..b7b4bfd 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -21,6 +21,7 @@ services: tty: true depends_on: + - web - redis environment: