From 5ca70a22fb7833c31f294a7ee3a628a6589cd7ae Mon Sep 17 00:00:00 2001 From: David Stephens Date: Sat, 23 Sep 2023 16:04:14 +0100 Subject: [PATCH] Use authorisation header, if provided, with St Jude scoreboard --- botto/clients/stjude_scoreboard.py | 14 +++++++++++--- botto/config.py | 3 +++ botto/slash_commands.py | 4 +++- 3 files changed, 17 insertions(+), 4 deletions(-) diff --git a/botto/clients/stjude_scoreboard.py b/botto/clients/stjude_scoreboard.py index 27c42c8..593aa7a 100644 --- a/botto/clients/stjude_scoreboard.py +++ b/botto/clients/stjude_scoreboard.py @@ -1,5 +1,5 @@ import logging -from typing import Literal +from typing import Literal, Optional import aiohttp @@ -7,11 +7,19 @@ class StJudeScoreboardClient: - def __init__(self): + def __init__(self, token: Optional[str]): super().__init__() self._url = "https://stjude-scoreboard.snailedit.org/api/co-founders" + self._token = token async def update_score(self, co_founder: Literal["myke", "stephen"], score: int): async with aiohttp.ClientSession() as session: url = f"{self._url}/{co_founder}" - await session.put(url, json={"score": score}, raise_for_status=True) + headers = ( + {"Authorization": f"Bearer {self._token}"} + if self._token is not None + else None + ) + await session.put( + url, json={"score": score}, headers=headers, raise_for_status=True + ) diff --git a/botto/config.py b/botto/config.py index 53432f4..4f7243d 100644 --- a/botto/config.py +++ b/botto/config.py @@ -350,6 +350,9 @@ def parse(config): if clickup_enabled_guilds := decode_base64_env("TLDBOTTO_CLICKUP_ENABLED_GUILDS"): defaults["clickup_enabled_guilds"] = clickup_enabled_guilds + if st_jude_scoreboard_key := decode_base64_env("ST_JUDE_SCOREBOARD_KEY"): + defaults["st_jude_scoreboard_key"] = st_jude_scoreboard_key + defaults["clickup_enabled_guilds"] = set(defaults["clickup_enabled_guilds"]) if id := os.getenv("TLDBOTTO_ID"): diff --git a/botto/slash_commands.py b/botto/slash_commands.py index e5bb47d..e141f5c 100644 --- a/botto/slash_commands.py +++ b/botto/slash_commands.py @@ -38,7 +38,9 @@ def setup_slash( ): client.tree.clear_commands(guild=None) client.tree.clear_commands(guild=client.snailed_it_beta_guild) - st_jude_scoreboard_client = StJudeScoreboardClient() + st_jude_scoreboard_client = StJudeScoreboardClient( + token=config.get("st_jude_scoreboard_key") + ) @client.tree.command( name="ping",