This commit is contained in:
Romain J 2021-04-20 17:30:05 +02:00
parent 22c5ee57d4
commit 751c82909d
6 changed files with 117 additions and 113 deletions

View file

@ -34,7 +34,7 @@ update-all:
$(VIRTUAL_ENV)/bin/pip install --upgrade --force-reinstall . $(VIRTUAL_ENV)/bin/pip install --upgrade --force-reinstall .
.PHONY: dev .PHONY: dev
dev: black type update dev: style update
tuxbot tuxbot
# Docker # Docker

View file

@ -1,15 +1,18 @@
import functools # import functools
import logging import logging
import discord
# import discord
from discord.ext import commands from discord.ext import commands
from tuxbot.cogs.Crypto.functions.extractor import extract #
from tuxbot.cogs.Crypto.functions.sync import encode # from tuxbot.cogs.Crypto.functions.extractor import extract
# from tuxbot.cogs.Crypto.functions.sync import encode
from tuxbot.core.bot import Tux from tuxbot.core.bot import Tux
from tuxbot.core.i18n import ( from tuxbot.core.i18n import (
Translator, Translator,
) )
from tuxbot.core.utils.functions.extra import group_extra, ContextPlus
# from tuxbot.core.utils.functions.extra import group_extra, ContextPlus
log = logging.getLogger("tuxbot.cogs.Crypto") log = logging.getLogger("tuxbot.cogs.Crypto")
@ -20,41 +23,41 @@ class Crypto(commands.Cog):
def __init__(self, bot: Tux): def __init__(self, bot: Tux):
self.bot = bot self.bot = bot
@group_extra(name="ralgo") # @group_extra(name="ralgo")
async def _ralgo(self, ctx: commands.Context): # async def _ralgo(self, ctx: commands.Context):
if ctx.invoked_subcommand is None: # if ctx.invoked_subcommand is None:
await ctx.send_help("ralgo") # await ctx.send_help("ralgo")
#
@_ralgo.command(name="encode") # @_ralgo.command(name="encode")
async def _ralgo_encode(self, ctx: ContextPlus, *, data: str = None): # async def _ralgo_encode(self, ctx: ContextPlus, *, data: str = None):
try: # try:
params = await extract(ctx.message.attachments, data, 10000) # params = await extract(ctx.message.attachments, data, 10000)
except ValueError: # except ValueError:
return await ctx.send("Invalid data provided") # return await ctx.send("Invalid data provided")
#
async with ctx.typing(): # async with ctx.typing():
output = await self.bot.loop.run_in_executor( # output = await self.bot.loop.run_in_executor(
None, functools.partial(encode, params) # None, functools.partial(encode, params)
) # )
#
if params["graphical"]: # if params["graphical"]:
return await ctx.send(file=output) # return await ctx.send(file=output)
#
await ctx.send(output) # await ctx.send(output)
#
@_ralgo.command(name="decode") # @_ralgo.command(name="decode")
async def _ralgo_decode(self, ctx: ContextPlus, *, data: str = None): # async def _ralgo_decode(self, ctx: ContextPlus, *, data: str = None):
try: # try:
params = await extract(ctx.message.attachments, data, 100000) # params = await extract(ctx.message.attachments, data, 100000)
except ValueError: # except ValueError:
return await ctx.send("Invalid data provided") # return await ctx.send("Invalid data provided")
#
async with ctx.typing(): # async with ctx.typing():
output = await self.bot.loop.run_in_executor( # output = await self.bot.loop.run_in_executor(
None, functools.partial(encode, params) # None, functools.partial(encode, params)
) # )
#
if isinstance(output, discord.File): # if isinstance(output, discord.File):
return await ctx.send(file=output) # return await ctx.send(file=output)
#
await ctx.send(output) # await ctx.send(output)

View file

@ -1,28 +1,28 @@
from typing import Optional, NoReturn, Union # from typing import Optional, NoReturn, Union
#
from discord import Attachment # from discord import Attachment
from tuxbot.cogs.Crypto.functions.parser import data_parser # from tuxbot.cogs.Crypto.functions.parser import data_parser
#
#
async def extract( # async def extract(
attachments: list[Optional[Attachment]], data: Optional[str], max_size: int # attachments: list[Optional[Attachment]], data: Optional[str], max_size: int
) -> dict: # ) -> dict:
if not data and len(attachments) == 0: # if not data and len(attachments) == 0:
raise ValueError # raise ValueError
#
kwargs = data_parser(data) # kwargs = data_parser(data)
#
if attachments and attachments[0]: # if attachments and attachments[0]:
file: Attachment = attachments[0] # file: Attachment = attachments[0]
if file.size > max_size: # if file.size > max_size:
raise ValueError # raise ValueError
#
kwargs["message"] = await file.read() # kwargs["message"] = await file.read()
#
params = { # params = {
"compressed": "compressed" in kwargs.keys(), # "compressed": "compressed" in kwargs.keys(),
"graphical": "graphical" in kwargs.keys(), # "graphical": "graphical" in kwargs.keys(),
"chars": kwargs["chars"] if "chars" in kwargs.keys() else (".", ","), # "chars": kwargs["chars"] if "chars" in kwargs.keys() else (".", ","),
} # }
#
return {"message": kwargs["message"], "params": params} # return {"message": kwargs["message"], "params": params}

View file

@ -1,40 +1,40 @@
from io import BytesIO # from io import BytesIO
from typing import Union # from typing import Union
#
import discord # import discord
from ralgo.ralgo import Ralgo # from ralgo.ralgo import Ralgo
from tuxbot.cogs.Crypto.functions.file import find_ext # from tuxbot.cogs.Crypto.functions.file import find_ext
#
#
def encode(params: dict) -> Union[str, discord.File]: # def encode(params: dict) -> Union[str, discord.File]:
statement = Ralgo(params["message"]) # statement = Ralgo(params["message"])
params = params["params"] # params = params["params"]
encoded = statement.encode(chars=params["chars"]) # encoded = statement.encode(chars=params["chars"])
#
if params["compressed"]: # if params["compressed"]:
return str(encoded.compress()) # return str(encoded.compress())
if params["graphical"]: # if params["graphical"]:
output = encoded.graphical().encode() # output = encoded.graphical().encode()
return discord.File(BytesIO(output.to_bytes()), "output.png") # return discord.File(BytesIO(output.to_bytes()), "output.png")
#
return str(encoded) # return str(encoded)
#
#
def decode(params: dict) -> Union[str, discord.File]: # def decode(params: dict) -> Union[str, discord.File]:
statement = Ralgo(params["message"]) # statement = Ralgo(params["message"])
params = params["params"] # params = params["params"]
#
if params["graphical"]: # if params["graphical"]:
output = Ralgo(statement.graphical().decode()).decode() # output = Ralgo(statement.graphical().decode()).decode()
elif params["compressed"]: # elif params["compressed"]:
output = Ralgo(statement.decompress()).decode() # output = Ralgo(statement.decompress()).decode()
else: # else:
output = statement.decode(chars=params["chars"]) # output = statement.decode(chars=params["chars"])
#
if isinstance(output, bytes): # if isinstance(output, bytes):
return discord.File(BytesIO(output), f"output.{find_ext(output)}") # return discord.File(BytesIO(output), f"output.{find_ext(output)}")
#
output = discord.utils.escape_markdown(str(output)) # output = discord.utils.escape_markdown(str(output))
output = discord.utils.escape_mentions(output) # output = discord.utils.escape_mentions(output)
#
return output if len(output) > 0 else "no content..." # return output if len(output) > 0 else "no content..."

View file

@ -69,6 +69,7 @@ class Logs(commands.Cog):
bot.on_error = self.on_error bot.on_error = self.on_error
if self.bot.instance_name != "dev": if self.bot.instance_name != "dev":
# pylint: disable=abstract-class-instantiated
sentry_sdk.init( sentry_sdk.init(
dsn=self.__config.sentryKey, dsn=self.__config.sentryKey,
traces_sample_rate=1.0, traces_sample_rate=1.0,

View file

@ -1,6 +1,6 @@
import json import json
import logging import logging
from typing import Union, Dict, Iterable, Any from typing import Union, Dict
import discord import discord
from discord.ext import commands from discord.ext import commands