support config from files as well as env vars

This commit is contained in:
Joseph Montanaro 2020-12-24 21:43:26 -08:00
parent 8b3740ce0c
commit 7b9c639393
2 changed files with 21 additions and 6 deletions

9
bot.py
View File

@ -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()

16
lib.py
View File

@ -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 = []
@ -18,3 +24,13 @@ class MineBot(commands.Bot):
if inspect.isawaitable(r):
await r
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)