improve(core|replacement): improve not to show replacements

This commit is contained in:
Romain J 2021-01-26 17:10:31 +01:00
parent f88adec45b
commit 1d37dc1961
2 changed files with 63 additions and 3 deletions

View file

@ -10,9 +10,9 @@ from rich.console import Console
console = Console()
TOKEN_REPLACEMENT = "\\*" * random.randint(3, 15)
PASSWORD_REPLACEMENT = "\\*" * random.randint(3, 15)
IP_REPLACEMENT = "\\*" * random.randint(3, 15)
TOKEN_REPLACEMENT = "" * random.randint(3, 15)
PASSWORD_REPLACEMENT = "" * random.randint(3, 15)
IP_REPLACEMENT = "" * random.randint(3, 15)
class ContextPlus(commands.Context):
@ -29,6 +29,10 @@ class ContextPlus(commands.Context):
allowed_mentions=None,
deletable=True
): # i know *args and **kwargs but, i prefer work with same values
from tuxbot.core.utils.functions.utils import (
replace_in_dict,
replace_in_list,
)
if content:
content = (
@ -53,6 +57,30 @@ class ContextPlus(commands.Context):
)
.replace(self.bot.config.Core.ip, IP_REPLACEMENT)
)
elif isinstance(value, list):
e[key] = replace_in_list(
value, self.bot.config.Core.token, TOKEN_REPLACEMENT
)
e[key] = replace_in_list(
e[key],
self.bot.config.Core.Database.password,
PASSWORD_REPLACEMENT,
)
e[key] = replace_in_list(
e[key], self.bot.config.Core.ip, IP_REPLACEMENT
)
elif isinstance(value, dict):
e[key] = replace_in_dict(
value, self.bot.config.Core.token, TOKEN_REPLACEMENT
)
e[key] = replace_in_dict(
e[key],
self.bot.config.Core.Database.password,
PASSWORD_REPLACEMENT,
)
e[key] = replace_in_dict(
e[key], self.bot.config.Core.ip, IP_REPLACEMENT
)
embed = Embed.from_dict(e)
if (

View file

@ -44,3 +44,35 @@ async def shorten(ctx: ContextPlus, text: str, length: int) -> dict:
pass
return output
def replace_in_dict(value: dict, search: str, replace: str) -> dict:
clean = {}
for k, v in value.items():
if isinstance(v, (str, bytes)):
v = v.replace(search, replace)
elif isinstance(v, list):
v = replace_in_list(v, search, replace)
elif isinstance(v, dict):
v = replace_in_dict(v, search, replace)
clean[k] = v
return clean
def replace_in_list(value: list, search: str, replace: str) -> list:
clean = []
for v in value:
if isinstance(v, (str, bytes)):
v = v.replace(search, replace)
elif isinstance(v, list):
v = replace_in_list(v, search, replace)
elif isinstance(v, dict):
v = replace_in_dict(v, search, replace)
clean.append(v)
return clean