tuxbot-bot/tuxbot/core/config.py

56 lines
1.3 KiB
Python
Raw Normal View History

2020-06-04 19:16:51 +02:00
import json
import logging
2020-06-04 19:16:51 +02:00
__all__ = ["Config"]
2020-06-04 19:16:51 +02:00
from typing import List, Dict
2020-06-04 19:16:51 +02:00
import discord
2020-06-04 19:16:51 +02:00
from tuxbot.core.data_manager import data_path
2020-06-04 19:16:51 +02:00
log = logging.getLogger("tuxbot.config")
2020-06-04 19:16:51 +02:00
class Config:
def __init__(
self,
cog_instance: str = None
):
self._cog_instance = cog_instance
def __getitem__(self, item) -> Dict:
path = data_path(self._cog_instance)
if item != 'core':
path = path / 'cogs' / item
else:
path /= 'core'
settings_file = path / 'settings.json'
if not settings_file.exists():
raise FileNotFoundError(f"Unable to find settings file "
f"'{settings_file}'")
else:
with settings_file.open('r') as f:
return json.load(f)
def __call__(self, item):
return self.__getitem__(item)
def owner_ids(self) -> List[int]:
return self.__getitem__('core').get('owner_ids')
def token(self) -> str:
return self.__getitem__('core').get('token')
def get_prefixes(self, guild: discord.Guild) -> List[str]:
core = self.__getitem__('core')
prefixes = core\
.get('guild', {}) \
.get(guild.id, {}) \
.get('prefixes', [])
return prefixes