2020-06-04 19:16:51 +02:00
|
|
|
import json
|
|
|
|
import logging
|
2020-06-06 02:00:16 +02:00
|
|
|
from typing import List, Dict, Union, Any
|
2020-06-03 01:07:43 +02:00
|
|
|
|
2020-06-04 19:16:51 +02:00
|
|
|
import discord
|
2020-06-03 01:07:43 +02:00
|
|
|
|
2020-06-04 19:16:51 +02:00
|
|
|
from tuxbot.core.data_manager import data_path
|
2020-06-03 01:07:43 +02:00
|
|
|
|
2020-06-06 02:00:16 +02:00
|
|
|
__all__ = ["Config"]
|
|
|
|
|
|
|
|
log = logging.getLogger("tuxbot.core.config")
|
2020-06-03 01:07:43 +02:00
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2020-06-05 00:29:14 +02:00
|
|
|
def owners_id(self) -> List[int]:
|
2020-06-06 02:00:16 +02:00
|
|
|
"""Simply return the owners id saved in config file.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
str
|
|
|
|
Owners id.
|
|
|
|
"""
|
2020-06-05 00:29:14 +02:00
|
|
|
return self.__getitem__('core').get('owners_id')
|
2020-06-04 19:16:51 +02:00
|
|
|
|
|
|
|
def token(self) -> str:
|
2020-06-06 02:00:16 +02:00
|
|
|
"""Simply return the bot token saved in config file.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
str
|
|
|
|
Bot token.
|
|
|
|
"""
|
2020-06-04 19:16:51 +02:00
|
|
|
return self.__getitem__('core').get('token')
|
|
|
|
|
|
|
|
def get_prefixes(self, guild: discord.Guild) -> List[str]:
|
2020-06-06 02:00:16 +02:00
|
|
|
"""Get custom prefixes for one guild.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
guild:discord.Guild
|
|
|
|
The required guild prefixes.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
List[str]
|
|
|
|
List of all prefixes.
|
|
|
|
"""
|
2020-06-04 19:16:51 +02:00
|
|
|
core = self.__getitem__('core')
|
2020-06-06 02:00:16 +02:00
|
|
|
prefixes = core \
|
2020-06-04 19:16:51 +02:00
|
|
|
.get('guild', {}) \
|
|
|
|
.get(guild.id, {}) \
|
|
|
|
.get('prefixes', [])
|
|
|
|
|
|
|
|
return prefixes
|
2020-06-05 00:29:14 +02:00
|
|
|
|
|
|
|
def get_blacklist(self, key: str) -> List[Union[str, int]]:
|
2020-06-06 02:00:16 +02:00
|
|
|
"""Return list off all blacklisted values
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
key:str
|
|
|
|
Which type of blacklist to choice (guilds ? channels ?,...).
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
List[Union[str, int]]
|
|
|
|
List containing blacklisted values.
|
|
|
|
"""
|
2020-06-05 00:29:14 +02:00
|
|
|
core = self.__getitem__('core')
|
|
|
|
blacklist = core \
|
|
|
|
.get('blacklist', {}) \
|
|
|
|
.get(key, [])
|
|
|
|
|
|
|
|
return blacklist
|
|
|
|
|
2020-06-06 02:00:16 +02:00
|
|
|
def update(self, cog_name: str, item: str, value: Any) -> dict:
|
|
|
|
"""Update values in config file.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
cog_name:str
|
|
|
|
Name of cog who's corresponding to the config file.
|
|
|
|
item:str
|
|
|
|
Key to update.
|
|
|
|
value:Any
|
|
|
|
New values to apply.
|
|
|
|
|
|
|
|
Returns
|
|
|
|
-------
|
|
|
|
dict:
|
|
|
|
Updated values.
|
|
|
|
|
|
|
|
"""
|
2020-06-05 00:29:14 +02:00
|
|
|
datas = self.__getitem__(cog_name)
|
|
|
|
path = data_path(self._cog_instance)
|
|
|
|
|
|
|
|
datas[item] = value
|
|
|
|
|
|
|
|
if cog_name != 'core':
|
|
|
|
path = path / 'cogs' / cog_name
|
|
|
|
else:
|
|
|
|
path /= 'core'
|
|
|
|
|
|
|
|
settings_file = path / 'settings.json'
|
|
|
|
|
|
|
|
with settings_file.open('w') as f:
|
|
|
|
json.dump(datas, f, indent=4)
|
|
|
|
|
|
|
|
return datas
|