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 .
.PHONY: dev
dev: black type update
dev: style update
tuxbot
# Docker

View file

@ -1,15 +1,18 @@
import functools
# import functools
import logging
import discord
# import discord
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.i18n import (
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")
@ -20,41 +23,41 @@ class Crypto(commands.Cog):
def __init__(self, bot: Tux):
self.bot = bot
@group_extra(name="ralgo")
async def _ralgo(self, ctx: commands.Context):
if ctx.invoked_subcommand is None:
await ctx.send_help("ralgo")
@_ralgo.command(name="encode")
async def _ralgo_encode(self, ctx: ContextPlus, *, data: str = None):
try:
params = await extract(ctx.message.attachments, data, 10000)
except ValueError:
return await ctx.send("Invalid data provided")
async with ctx.typing():
output = await self.bot.loop.run_in_executor(
None, functools.partial(encode, params)
)
if params["graphical"]:
return await ctx.send(file=output)
await ctx.send(output)
@_ralgo.command(name="decode")
async def _ralgo_decode(self, ctx: ContextPlus, *, data: str = None):
try:
params = await extract(ctx.message.attachments, data, 100000)
except ValueError:
return await ctx.send("Invalid data provided")
async with ctx.typing():
output = await self.bot.loop.run_in_executor(
None, functools.partial(encode, params)
)
if isinstance(output, discord.File):
return await ctx.send(file=output)
await ctx.send(output)
# @group_extra(name="ralgo")
# async def _ralgo(self, ctx: commands.Context):
# if ctx.invoked_subcommand is None:
# await ctx.send_help("ralgo")
#
# @_ralgo.command(name="encode")
# async def _ralgo_encode(self, ctx: ContextPlus, *, data: str = None):
# try:
# params = await extract(ctx.message.attachments, data, 10000)
# except ValueError:
# return await ctx.send("Invalid data provided")
#
# async with ctx.typing():
# output = await self.bot.loop.run_in_executor(
# None, functools.partial(encode, params)
# )
#
# if params["graphical"]:
# return await ctx.send(file=output)
#
# await ctx.send(output)
#
# @_ralgo.command(name="decode")
# async def _ralgo_decode(self, ctx: ContextPlus, *, data: str = None):
# try:
# params = await extract(ctx.message.attachments, data, 100000)
# except ValueError:
# return await ctx.send("Invalid data provided")
#
# async with ctx.typing():
# output = await self.bot.loop.run_in_executor(
# None, functools.partial(encode, params)
# )
#
# if isinstance(output, discord.File):
# return await ctx.send(file=output)
#
# await ctx.send(output)

View file

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

View file

@ -1,40 +1,40 @@
from io import BytesIO
from typing import Union
import discord
from ralgo.ralgo import Ralgo
from tuxbot.cogs.Crypto.functions.file import find_ext
def encode(params: dict) -> Union[str, discord.File]:
statement = Ralgo(params["message"])
params = params["params"]
encoded = statement.encode(chars=params["chars"])
if params["compressed"]:
return str(encoded.compress())
if params["graphical"]:
output = encoded.graphical().encode()
return discord.File(BytesIO(output.to_bytes()), "output.png")
return str(encoded)
def decode(params: dict) -> Union[str, discord.File]:
statement = Ralgo(params["message"])
params = params["params"]
if params["graphical"]:
output = Ralgo(statement.graphical().decode()).decode()
elif params["compressed"]:
output = Ralgo(statement.decompress()).decode()
else:
output = statement.decode(chars=params["chars"])
if isinstance(output, bytes):
return discord.File(BytesIO(output), f"output.{find_ext(output)}")
output = discord.utils.escape_markdown(str(output))
output = discord.utils.escape_mentions(output)
return output if len(output) > 0 else "no content..."
# from io import BytesIO
# from typing import Union
#
# import discord
# from ralgo.ralgo import Ralgo
# from tuxbot.cogs.Crypto.functions.file import find_ext
#
#
# def encode(params: dict) -> Union[str, discord.File]:
# statement = Ralgo(params["message"])
# params = params["params"]
# encoded = statement.encode(chars=params["chars"])
#
# if params["compressed"]:
# return str(encoded.compress())
# if params["graphical"]:
# output = encoded.graphical().encode()
# return discord.File(BytesIO(output.to_bytes()), "output.png")
#
# return str(encoded)
#
#
# def decode(params: dict) -> Union[str, discord.File]:
# statement = Ralgo(params["message"])
# params = params["params"]
#
# if params["graphical"]:
# output = Ralgo(statement.graphical().decode()).decode()
# elif params["compressed"]:
# output = Ralgo(statement.decompress()).decode()
# else:
# output = statement.decode(chars=params["chars"])
#
# if isinstance(output, bytes):
# return discord.File(BytesIO(output), f"output.{find_ext(output)}")
#
# output = discord.utils.escape_markdown(str(output))
# output = discord.utils.escape_mentions(output)
#
# 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
if self.bot.instance_name != "dev":
# pylint: disable=abstract-class-instantiated
sentry_sdk.init(
dsn=self.__config.sentryKey,
traces_sample_rate=1.0,

View file

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