2020-05-12 18:23:22 -07:00
|
|
|
import inspect
|
2020-12-24 21:43:26 -08:00
|
|
|
import os
|
|
|
|
import pathlib
|
2020-05-12 18:23:22 -07:00
|
|
|
|
|
|
|
from discord.ext import commands
|
|
|
|
|
|
|
|
|
2020-12-24 21:43:26 -08:00
|
|
|
class ConfigError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2020-05-12 18:23:22 -07:00
|
|
|
class MineBot(commands.Bot):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.cleanup = []
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def add_cleanup(self, callback):
|
|
|
|
self.cleanup.append(callback)
|
|
|
|
|
|
|
|
async def close(self):
|
|
|
|
for callback in self.cleanup:
|
|
|
|
r = callback()
|
|
|
|
# coroutines etc. return an awaitable object
|
|
|
|
if inspect.isawaitable(r):
|
|
|
|
await r
|
2020-12-24 21:43:26 -08:00
|
|
|
await super().close()
|
|
|
|
|
|
|
|
|
2020-12-27 20:27:02 -08:00
|
|
|
def getconfig(key, default=None):
|
2020-12-24 21:43:26 -08:00
|
|
|
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()
|
2020-12-27 20:27:02 -08:00
|
|
|
elif default != None:
|
|
|
|
return default
|
2020-12-24 21:43:26 -08:00
|
|
|
else:
|
|
|
|
raise ConfigError('Missing config:', key)
|