From 888a7924bea5d94fb3ca0849a929de3481c8b8b3 Mon Sep 17 00:00:00 2001
From: Romain J <romain.ordi@gmail.com>
Date: Mon, 19 Oct 2020 01:37:12 +0200
Subject: [PATCH] update(extra): remove yaml for token replacement
---
setup.cfg | 1 -
tuxbot/__run__.py | 2 +-
tuxbot/cogs/admin/admin.py | 4 +---
tuxbot/core/config.py | 2 +-
tuxbot/core/i18n.py | 18 +++++++++++++-----
tuxbot/core/utils/functions/extra.py | 18 +++++++++++-------
6 files changed, 27 insertions(+), 18 deletions(-)
diff --git a/setup.cfg b/setup.cfg
index 06b414b..9def2ff 100644
--- a/setup.cfg
+++ b/setup.cfg
@@ -23,7 +23,6 @@ install_requires =
humanize==2.6.0
jishaku>=1.19.1.200
psutil>=5.7.2
- pyyaml>=5.3.1
rich>=6.0.0
structured_config>=4.12
diff --git a/tuxbot/__run__.py b/tuxbot/__run__.py
index dcd08df..0741abd 100644
--- a/tuxbot/__run__.py
+++ b/tuxbot/__run__.py
@@ -116,7 +116,7 @@ def debug_info() -> NoReturn:
table.add_row(f"[u]System arch:[/u] {os.uname().machine}")
table.add_row(f"[u]Kernel:[/u] {os.uname().release}")
table.add_row(f"[u]User:[/u] {os.getlogin()}")
- table.add_row(f"[u]Uptime:[/u] {uptime[2]}")
+ table.add_row(f"[u]Uptime:[/u] {uptime[2][:-1]}")
table.add_row(
f"[u]Load Average:[/u] {' '.join(map(str, os.getloadavg()))}"
)
diff --git a/tuxbot/cogs/admin/admin.py b/tuxbot/cogs/admin/admin.py
index 6a4b221..3ab95db 100644
--- a/tuxbot/cogs/admin/admin.py
+++ b/tuxbot/cogs/admin/admin.py
@@ -26,9 +26,7 @@ class Admin(commands.Cog, name="Admin"):
self.bot = bot
async def _save_lang(self, ctx: ContextPlus, lang: str):
- await self.bot.config.update(
- "core", f"guild.{ctx.guild.id}.locale", lang
- )
+ self.bot.config.Servers.all[ctx.guild.id].locale = lang
@group_extra(name="lang", aliases=["locale", "langue"], deletable=True)
@commands.guild_only()
diff --git a/tuxbot/core/config.py b/tuxbot/core/config.py
index a9df315..5b14d5a 100644
--- a/tuxbot/core/config.py
+++ b/tuxbot/core/config.py
@@ -70,7 +70,7 @@ class AppConfig(Structure):
# =============================================================================
-# Useful functions to browse configs
+# Useful functions to interact with configs
# =============================================================================
diff --git a/tuxbot/core/i18n.py b/tuxbot/core/i18n.py
index f6bac4a..69add4a 100644
--- a/tuxbot/core/i18n.py
+++ b/tuxbot/core/i18n.py
@@ -6,6 +6,7 @@ from typing import Callable, Union, Dict, List
from babel.messages.pofile import read_po
from tuxbot.core import Config
+from tuxbot.core.config import search_for
from tuxbot.core.utils.functions.extra import ContextPlus
log = logging.getLogger("tuxbot.core.i18n")
@@ -59,12 +60,19 @@ class Translator(Callable[[str], str]):
self, untranslated: str, ctx: ContextPlus, config: Config
) -> str:
try:
- locale = config.get_value(
- "core",
- f"guild.{ctx.guild.id}.locale",
- config.get_value("core", "locale"),
+ user_locale = search_for(
+ config.Users, ctx.author.id, "locale", None
)
- return self.translations[locale][untranslated]
+ if user_locale:
+ return self.translations[user_locale][untranslated]
+
+ guild_locale = search_for(
+ config.Servers, ctx.guild.id, "locale", None
+ )
+ if guild_locale:
+ return self.translations[guild_locale][untranslated]
+
+ return self.translations[config.Core.locale][untranslated]
except KeyError:
return untranslated
diff --git a/tuxbot/core/utils/functions/extra.py b/tuxbot/core/utils/functions/extra.py
index 6607726..6d5479f 100644
--- a/tuxbot/core/utils/functions/extra.py
+++ b/tuxbot/core/utils/functions/extra.py
@@ -1,5 +1,4 @@
import asyncio
-import yaml
import discord
from discord import Embed
@@ -9,18 +8,23 @@ from rich.console import Console
console = Console()
-console.clear()
+TOKEN_REPLACEMENT = "whoops, leaked token"
class ContextPlus(commands.Context):
async def send(self, content=None, *args, **kwargs):
if content is not None:
- content = content.replace(self.bot.config.Core.token, "<token>")
+ content = content.replace(
+ self.bot.config.Core.token, TOKEN_REPLACEMENT
+ )
if kwargs.get("embed"):
- e = str(kwargs.get("embed").to_dict())
- e = e.replace(self.bot.config("core").get("token"), "<token>")
- e = yaml.load(e, Loader=yaml.FullLoader)
- kwargs["embed"] = Embed.from_dict(e)
+ embed = kwargs.get("embed").to_dict()
+ for key, value in embed.items():
+ if isinstance(value, (str, bytes)):
+ embed[key] = value.replace(
+ self.bot.config.Core.token, TOKEN_REPLACEMENT
+ )
+ kwargs["embed"] = Embed.from_dict(embed)
if (
hasattr(self.command, "deletable") and self.command.deletable