You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.7 KiB
Python
96 lines
3.7 KiB
Python
import logging
|
|
import os.path
|
|
from typing import List, Dict
|
|
|
|
import discord
|
|
import yaml
|
|
|
|
class ConfigLoader:
|
|
"""
|
|
Loads the configuration data
|
|
"""
|
|
DEFAULT_CONFIG_LOCATION = "mc-auth.config"
|
|
_CONF_INST = None
|
|
|
|
def __init__(self, path=None):
|
|
ConfigLoader._CONF_INST = self
|
|
self.log = logging.getLogger("Conf")
|
|
try:
|
|
path = os.environ.get('MC_AUTH_CONFIG_LOCATION') if os.environ.get('MC_AUTH_CONFIG_LOCATION') is not None else path
|
|
except KeyError:
|
|
pass
|
|
self.conf_file_path = os.path.join(os.getcwd(), ConfigLoader.DEFAULT_CONFIG_LOCATION) if path is None else path
|
|
assert(os.path.isfile(self.conf_file_path))
|
|
|
|
self.config: dict = dict()
|
|
self.read_config()
|
|
|
|
self.__auth_guilds_per_domain: Dict[str, List[discord.Guild]] = None
|
|
self.__auth_guilds: List[discord.Guild] = None
|
|
self.__whitelist_locations_per_domain: Dict[str, str] = None
|
|
|
|
async def load_guilds(self, bot):
|
|
if self.__auth_guilds_per_domain is not None:
|
|
return
|
|
self.log.debug("Loading all auth_guild objects from discord API")
|
|
self.__auth_guilds_per_domain = dict()
|
|
guilds_bot_is_in: List[discord.Guild] = bot.fetch_guilds()
|
|
for domain in self.config["minecraft"]["domains"]:
|
|
gs = []
|
|
for gid in self.config["minecraft"]["domains"][domain]["auth_guilds"]:
|
|
async for guild in guilds_bot_is_in:
|
|
if guild.id == int(gid):
|
|
gs.append(guild)
|
|
self.__auth_guilds_per_domain[domain] = gs
|
|
|
|
@staticmethod
|
|
def get_config_loader(path=None):
|
|
if ConfigLoader._CONF_INST is None:
|
|
ConfigLoader(path)
|
|
return ConfigLoader._CONF_INST
|
|
|
|
def read_config(self):
|
|
self.log.debug(f"Reading config \"{self.conf_file_path}\"")
|
|
self.config = yaml.safe_load(open(self.conf_file_path, 'r'))
|
|
|
|
@property
|
|
def auth_guilds_per_domain(self) -> Dict[str, List[discord.Guild]]:
|
|
if self.__auth_guilds_per_domain is None:
|
|
self.log.critical("Looked up ConfigLoader.auth_guilds before it could be loaded by the DiscordBot!")
|
|
raise ValueError("Looked up ConfigLoader.auth_guilds before it could be loaded by the DiscordBot!")
|
|
return self.__auth_guilds_per_domain
|
|
|
|
@property
|
|
def auth_guilds(self) -> List[discord.Guild]:
|
|
if self.__auth_guilds is None:
|
|
self.__auth_guilds = list()
|
|
for domain in self.auth_guilds_per_domain:
|
|
self.__auth_guilds += self.auth_guilds_per_domain[domain]
|
|
self.__auth_guilds = list(set(self.__auth_guilds))
|
|
return self.__auth_guilds
|
|
|
|
@property
|
|
def whitelist_location_per_domain(self) -> Dict[str, str]:
|
|
if self.__whitelist_locations_per_domain is None:
|
|
self.__whitelist_locations_per_domain = dict()
|
|
for domain in self.config["minecraft"]["domains"]:
|
|
self.__whitelist_locations_per_domain[domain] = self.config["minecraft"]["domains"][domain]["whitelist_location"]
|
|
return self.__whitelist_locations_per_domain
|
|
|
|
@property
|
|
def default_minecraft_domain(self) -> str:
|
|
return self.config["minecraft"]["default_domain"]
|
|
|
|
@property
|
|
def database_host(self) -> str:
|
|
return self.config["database"]["host"]
|
|
|
|
@property
|
|
def database_port(self) -> str:
|
|
return self.config["database"]["port"]
|
|
|
|
def post_application_text(self, domain: str) -> str:
|
|
return self.config["minecraft"]["domains"][domain]["post_application_text"]
|
|
|
|
def roles_with_server_access(self, domain: str) -> List[str]:
|
|
return self.config["minecraft"]["domains"][domain]["roles_with_server_access"] |