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 import heavynode
DISCORD_TOKEN = os.environ['discord_token'] DISCORD_TOKEN = lib.getconfig('DISCORD_TOKEN')
DISCORD_SERVER_ID = 530446700058509323 DISCORD_SERVER_ID = 530446700058509323
HEAVYNODE_TOKEN = os.environ['heavynode_token'] HEAVYNODE_TOKEN = lib.getconfig('HEAVYNODE_TOKEN')
# SESSION_COOKIE = os.environ['pterodactyl_session_cookie'] COOKIE_NAME = lib.getconfig('HEAVYNODE_COOKIE_NAME')
COOKIE_NAME = 'remember_web_59ba36addc2b2f9401580f014c7f58ea4e30989d' COOKIE_VALUE = lib.getconfig('HEAVYNODE_COOKIE_VALUE')
COOKIE_VALUE = os.environ[COOKIE_NAME]
logging.basicConfig() logging.basicConfig()

18
lib.py
View File

@ -1,8 +1,14 @@
import inspect import inspect
import os
import pathlib
from discord.ext import commands from discord.ext import commands
class ConfigError(Exception):
pass
class MineBot(commands.Bot): class MineBot(commands.Bot):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.cleanup = [] self.cleanup = []
@ -17,4 +23,14 @@ class MineBot(commands.Bot):
# coroutines etc. return an awaitable object # coroutines etc. return an awaitable object
if inspect.isawaitable(r): if inspect.isawaitable(r):
await r await r
await super().close() 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)