From 1be4af84050a9158d18b2a3ab279308e39602f40 Mon Sep 17 00:00:00 2001 From: Romain J Date: Mon, 19 Oct 2020 00:53:26 +0200 Subject: [PATCH] fix(config): update old config code --- tuxbot/core/bot.py | 15 ++++++++------- tuxbot/core/config.py | 13 ++++++++++++- tuxbot/core/utils/functions/extra.py | 4 +--- tuxbot/core/utils/functions/prefix.py | 6 +++--- 4 files changed, 24 insertions(+), 14 deletions(-) diff --git a/tuxbot/core/bot.py b/tuxbot/core/bot.py index 8b8e2af..596e42f 100644 --- a/tuxbot/core/bot.py +++ b/tuxbot/core/bot.py @@ -15,7 +15,7 @@ from rich.traceback import install from tuxbot import version_info -from . import Config, ConfigFile +from .config import Config, ConfigFile, search_for from .data_manager import logs_data_path, data_path from . import __version__, ExitCodes @@ -189,9 +189,11 @@ class Tux(commands.AutoShardedBot): return if ( - self.config.Servers.all[message.guild.id].blacklisted - or self.config.Channels.all[message.channel.id].blacklisted - or self.config.Users.all[message.author.id].blacklisted + search_for(self.config.Servers, message.guild.id, "blacklisted") + or search_for( + self.config.Channels, message.channel.id, "blacklisted" + ) + or search_for(self.config.Users, message.author.id, "blacklisted") ): return @@ -200,9 +202,8 @@ class Tux(commands.AutoShardedBot): if ctx is None or ctx.valid is False: self.dispatch("message_without_command", message) else: - if ( - ctx.command - in self.config.Servers.all[message.guild.id].disabled_command + if ctx.command in search_for( + self.config.Servers, message.guild.id, "disabled_command", [] ): raise exceptions.DisabledCommandByServerOwner diff --git a/tuxbot/core/config.py b/tuxbot/core/config.py index bac61af..a9df315 100644 --- a/tuxbot/core/config.py +++ b/tuxbot/core/config.py @@ -9,7 +9,7 @@ from structured_config import ( ) -__all__ = ["Config", "ConfigFile"] +__all__ = ["Config", "ConfigFile", "search_for"] log = logging.getLogger("tuxbot.core.config") @@ -67,3 +67,14 @@ class AppConfig(Structure): last_run: int = IntField(0) instances: Dict[str, Instance] = {} + + +# ============================================================================= +# Useful functions to browse configs +# ============================================================================= + + +def search_for(config, key, value, default=False): + if key in config.all: + return getattr(config.all[key], value) + return default diff --git a/tuxbot/core/utils/functions/extra.py b/tuxbot/core/utils/functions/extra.py index 0d777e6..6607726 100644 --- a/tuxbot/core/utils/functions/extra.py +++ b/tuxbot/core/utils/functions/extra.py @@ -15,9 +15,7 @@ console.clear() class ContextPlus(commands.Context): async def send(self, content=None, *args, **kwargs): if content is not None: - content = content.replace( - self.bot.config("core").get("token"), "" - ) + content = content.replace(self.bot.config.Core.token, "") if kwargs.get("embed"): e = str(kwargs.get("embed").to_dict()) e = e.replace(self.bot.config("core").get("token"), "") diff --git a/tuxbot/core/utils/functions/prefix.py b/tuxbot/core/utils/functions/prefix.py index 37888b8..58f4e92 100644 --- a/tuxbot/core/utils/functions/prefix.py +++ b/tuxbot/core/utils/functions/prefix.py @@ -2,10 +2,10 @@ from typing import List import discord -from tuxbot.core.bot import Tux +from tuxbot.core.config import search_for -def get_prefixes(tux: Tux, guild: discord.Guild) -> List[str]: +def get_prefixes(tux, guild: discord.Guild) -> List[str]: """Get custom prefixes for one guild. Parameters ---------- @@ -19,4 +19,4 @@ def get_prefixes(tux: Tux, guild: discord.Guild) -> List[str]: List[str] List of all prefixes. """ - return tux.config.Servers.all[guild.id].prefixes or [] + return search_for(tux.config.Servers, guild.id, "prefixes", [])