Skip to content

Commit

Permalink
Add support for storing AoC blocks on site
Browse files Browse the repository at this point in the history
- This commits adds an APIClient to the AoC cog, and implements site as the backend storage for AoC role blocks
  • Loading branch information
D0rs4n committed Apr 10, 2022
1 parent 9663f18 commit 49b5f17
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion bot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
1 change: 0 additions & 1 deletion bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions bot/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions bot/exts/advent_of_code/_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
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

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,
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ services:
tty: true

depends_on:
- web
- redis

environment:
Expand Down

0 comments on commit 49b5f17

Please sign in to comment.