Skip to content

Commit c96f9ca

Browse files
Remove requirement for 'difficult' config keys. (#45)
* remove keys that make this bot hard to run * pyright didnt flag this as accessed
1 parent 89d896e commit c96f9ca

File tree

5 files changed

+25
-14
lines changed

5 files changed

+25
-14
lines changed

config.template.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ owner_ids = [123, 456, 789] # user or role ids, optional
33

44
[TOKENS]
55
bot = ''
6-
idevision = ''
7-
mystbin = ''
8-
pythonista = ''
6+
idevision = '' # optional key
7+
mystbin = '' # optional key
8+
pythonista = '' # optional key
99

1010
[DATABASE]
1111
dsn = 'postgres://pythonistabot:pythonistabot@database:5432/pythonistabot' # assumed default

launcher.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ async def main() -> None:
4040
bot.pool = pool
4141
bot.log_handler = handler
4242

43-
bot.mb_client = mystbin.Client(token=core.CONFIG["TOKENS"]["mystbin"], session=session)
43+
_mystbin_token = core.CONFIG["TOKENS"].get("mystbin")
44+
bot.mb_client = mystbin.Client(token=_mystbin_token, session=session)
4445

4546
await bot.load_extension("jishaku")
4647
for extension in EXTENSIONS:

modules/api.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545

4646

4747
class API(core.Cog):
48-
def __init__(self, bot: core.Bot) -> None:
48+
def __init__(self, bot: core.Bot, *, pythonista_api_key: str) -> None:
4949
self.bot = bot
50+
self._auth: str = pythonista_api_key
5051

5152
self.session: aiohttp.ClientSession | None = None
5253
self.backoff: ExponentialBackoff[bool] = ExponentialBackoff()
@@ -57,7 +58,7 @@ def __init__(self, bot: core.Bot) -> None:
5758

5859
@property
5960
def headers(self) -> dict[str, Any]:
60-
return {"Authorization": core.CONFIG["TOKENS"]["pythonista"]}
61+
return {"Authorization": self._auth}
6162

6263
async def cog_load(self) -> None:
6364
self.session = aiohttp.ClientSession(headers=self.headers)
@@ -170,4 +171,9 @@ async def keep_alive(self) -> None:
170171

171172

172173
async def setup(bot: core.Bot) -> None:
173-
await bot.add_cog(API(bot))
174+
pythonista_api_key = core.CONFIG["TOKENS"].get("pythonista")
175+
if not pythonista_api_key:
176+
LOGGER.warning("Not enabling %r due to missing config key.", __file__)
177+
return
178+
179+
await bot.add_cog(API(bot, pythonista_api_key=pythonista_api_key))

modules/manuals.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ class Manuals(commands.Cog):
5959

6060
target = yarl.URL("https://idevision.net/api/public/")
6161

62-
def __init__(self, bot: core.Bot) -> None:
62+
def __init__(self, bot: core.Bot, *, idevision_auth: str | None) -> None:
6363
self.bot = bot
64+
self._idevision_auth: str | None = idevision_auth
6465

6566
@staticmethod
6667
def _cooldown_bucket(ctx: core.Context) -> commands.Cooldown | None:
@@ -176,8 +177,9 @@ async def rtfm(self, ctx: core.Context, *, query: str) -> None:
176177

177178
headers = {
178179
"User-Agent": f"PythonistaBot discord bot (via {ctx.author})",
179-
"Authorization": core.CONFIG["TOKENS"]["idevision"],
180180
}
181+
if self._idevision_auth:
182+
headers["Authorization"] = self._idevision_auth
181183

182184
async with self.bot.session.get(url, headers=headers) as resp:
183185
if resp.status != 200:
@@ -269,8 +271,9 @@ async def rtfs(self, ctx: core.Context, *, query: str) -> None:
269271

270272
headers = {
271273
"User-Agent": f"PythonistaBot discord bot (via {ctx.author})",
272-
"Authorization": core.CONFIG["TOKENS"]["idevision"],
273274
}
275+
if self._idevision_auth:
276+
headers["Authorization"] = self._idevision_auth
274277

275278
async with self.bot.session.get(url, headers=headers) as resp:
276279
if resp.status != 200:
@@ -306,4 +309,5 @@ async def rtfs(self, ctx: core.Context, *, query: str) -> None:
306309

307310

308311
async def setup(bot: core.Bot) -> None:
309-
await bot.add_cog(Manuals(bot))
312+
idevision_auth_key = core.CONFIG["TOKENS"].get("idevision")
313+
await bot.add_cog(Manuals(bot, idevision_auth=idevision_auth_key))

types_/config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
from typing import NotRequired, TypedDict
1+
from typing import NotRequired, Required, TypedDict
22

33

44
__all__ = ("Config",)
55

66

7-
class Tokens(TypedDict):
8-
bot: str
7+
class Tokens(TypedDict, total=False):
8+
bot: Required[str]
99
idevision: str
1010
mystbin: str
1111
github_bot: str

0 commit comments

Comments
 (0)