From 7b9c639393bd27d51b7e85988b462b1d321b06a0 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Thu, 24 Dec 2020 21:43:26 -0800 Subject: [PATCH] support config from files as well as env vars --- bot.py | 9 ++++----- lib.py | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/bot.py b/bot.py index 4e9bf48..8ccb43c 100644 --- a/bot.py +++ b/bot.py @@ -12,12 +12,11 @@ import lib import heavynode -DISCORD_TOKEN = os.environ['discord_token'] +DISCORD_TOKEN = lib.getconfig('DISCORD_TOKEN') DISCORD_SERVER_ID = 530446700058509323 -HEAVYNODE_TOKEN = os.environ['heavynode_token'] -# SESSION_COOKIE = os.environ['pterodactyl_session_cookie'] -COOKIE_NAME = 'remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d' -COOKIE_VALUE = os.environ[COOKIE_NAME] +HEAVYNODE_TOKEN = lib.getconfig('HEAVYNODE_TOKEN') +COOKIE_NAME = lib.getconfig('HEAVYNODE_COOKIE_NAME') +COOKIE_VALUE = lib.getconfig('HEAVYNODE_COOKIE_VALUE') logging.basicConfig() diff --git a/lib.py b/lib.py index d68a4ed..fe95689 100644 --- a/lib.py +++ b/lib.py @@ -1,8 +1,14 @@ import inspect +import os +import pathlib from discord.ext import commands +class ConfigError(Exception): + pass + + class MineBot(commands.Bot): def __init__(self, *args, **kwargs): self.cleanup = [] @@ -17,4 +23,14 @@ class MineBot(commands.Bot): # coroutines etc. return an awaitable object if inspect.isawaitable(r): await r - await super().close() \ No newline at end of file + await super().close() + + +def getconfig(key): + if key in os.environ: + return os.environ[key] + elif f'{key}_FILE' in os.environ: + p = os.environ[f'{key}_FILE'] + return pathlib.Path(p).read_text() + else: + raise ConfigError('Missing config:', key) \ No newline at end of file