tuxbot-bot/tuxbot/core/config.py

142 lines
3.3 KiB
Python
Raw Normal View History

2020-06-06 18:51:47 +02:00
import asyncio
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-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-06 02:00:16 +02:00
__all__ = ["Config"]
log = logging.getLogger("tuxbot.core.config")
2020-06-04 19:16:51 +02:00
class Config:
2020-06-06 18:51:47 +02:00
def __init__(self, cog_instance: str = None):
2020-06-04 19:16:51 +02:00
self._cog_instance = cog_instance
2020-06-06 18:51:47 +02:00
self.lock = asyncio.Lock()
self.loop = asyncio.get_event_loop()
self._settings_file = None
self._datas = {}
2020-06-04 19:16:51 +02:00
def __getitem__(self, item) -> Dict:
path = data_path(self._cog_instance)
2020-06-06 18:51:47 +02:00
if item != "core":
path = path / "cogs" / item
2020-06-04 19:16:51 +02:00
else:
2020-06-06 18:51:47 +02:00
path /= "core"
2020-06-04 19:16:51 +02:00
2020-06-06 18:51:47 +02:00
settings_file = path / "settings.json"
2020-06-04 19:16:51 +02:00
if not settings_file.exists():
2020-06-06 18:51:47 +02:00
raise FileNotFoundError(
f"Unable to find settings file " f"'{settings_file}'"
)
2020-06-04 19:16:51 +02:00
else:
2020-06-06 18:51:47 +02:00
with settings_file.open("r") as f:
2020-06-04 19:16:51 +02:00
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-06 18:51:47 +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-06 18:51:47 +02:00
return self.__getitem__("core").get("token")
2020-06-04 19:16:51 +02:00
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-06 18:51:47 +02:00
core = self.__getitem__("core")
prefixes = core.get("guild", {}).get(guild.id, {}).get("prefixes", [])
2020-06-04 19:16:51 +02:00
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-06 18:51:47 +02:00
core = self.__getitem__("core")
blacklist = core.get("blacklist", {}).get(key, [])
2020-06-05 00:29:14 +02:00
return blacklist
2020-06-06 18:51:47 +02:00
def _dump(self):
with self._settings_file.open("w") as f:
json.dump(self._datas, f, indent=4)
async def update(self, cog_name: str, item: str, value: Any) -> dict:
2020-06-06 02:00:16 +02:00
"""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
2020-06-06 18:51:47 +02:00
self._datas = datas
if cog_name != "core":
path = path / "cogs" / cog_name
2020-06-05 00:29:14 +02:00
else:
2020-06-06 18:51:47 +02:00
path /= "core"
2020-06-05 00:29:14 +02:00
2020-06-06 18:51:47 +02:00
self._settings_file = path / "settings.json"
2020-06-05 00:29:14 +02:00
2020-06-06 18:51:47 +02:00
async with self.lock:
await self.loop.run_in_executor(None, self._dump)
2020-06-05 00:29:14 +02:00
return datas