2021-01-26 15:24:10 +01:00
|
|
|
import asyncio
|
2021-01-25 17:28:59 +01:00
|
|
|
import functools
|
2021-03-31 18:08:41 +02:00
|
|
|
from typing import Dict
|
2021-01-25 17:28:59 +01:00
|
|
|
|
2021-01-26 15:24:10 +01:00
|
|
|
import aiohttp
|
2021-01-25 17:28:59 +01:00
|
|
|
from discord.ext import commands
|
|
|
|
|
|
|
|
from tuxbot.core.utils.functions.extra import ContextPlus
|
|
|
|
|
|
|
|
|
2021-01-22 10:22:39 +01:00
|
|
|
def upper_first(string: str) -> str:
|
|
|
|
return "".join(string[0].upper() + string[1:])
|
2021-01-25 17:28:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
def typing(func):
|
|
|
|
@functools.wraps(func)
|
|
|
|
async def wrapped(*args, **kwargs):
|
|
|
|
context = (
|
|
|
|
args[0]
|
|
|
|
if isinstance(args[0], (commands.Context, ContextPlus))
|
|
|
|
else args[1]
|
|
|
|
)
|
|
|
|
|
|
|
|
async with context.typing():
|
|
|
|
await func(*args, **kwargs)
|
|
|
|
|
|
|
|
return wrapped
|
2021-01-26 15:24:10 +01:00
|
|
|
|
|
|
|
|
2021-03-01 14:11:18 +01:00
|
|
|
async def shorten(session, text: str, length: int) -> dict:
|
2021-03-31 18:08:41 +02:00
|
|
|
output: Dict[str, str] = {"text": text[:length], "link": ""}
|
2021-01-26 15:24:10 +01:00
|
|
|
|
|
|
|
if len(text) > length:
|
|
|
|
output["text"] += "[...]"
|
|
|
|
try:
|
2021-03-01 14:11:18 +01:00
|
|
|
async with session.post(
|
2021-01-26 15:24:10 +01:00
|
|
|
"https://paste.ramle.be/documents",
|
|
|
|
data=text.encode(),
|
|
|
|
timeout=aiohttp.ClientTimeout(total=2),
|
|
|
|
) as r:
|
|
|
|
output[
|
|
|
|
"link"
|
|
|
|
] = f"https://paste.ramle.be/{(await r.json())['key']}"
|
|
|
|
except (aiohttp.ClientError, asyncio.exceptions.TimeoutError):
|
|
|
|
pass
|
|
|
|
|
|
|
|
return output
|
2021-01-26 17:10:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
def replace_in_dict(value: dict, search: str, replace: str) -> dict:
|
|
|
|
clean = {}
|
|
|
|
|
|
|
|
for k, v in value.items():
|
|
|
|
if isinstance(v, (str, bytes)):
|
2021-03-31 18:08:41 +02:00
|
|
|
v = v.replace(search, replace) # type: ignore
|
2021-01-26 17:10:31 +01:00
|
|
|
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)):
|
2021-03-31 18:08:41 +02:00
|
|
|
v = v.replace(search, replace) # type: ignore
|
2021-01-26 17:10:31 +01:00
|
|
|
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
|