tuxbot-bot/tuxbot/core/utils/functions/extra.py

161 lines
5.5 KiB
Python
Raw Permalink Normal View History

import asyncio
import random
from io import BytesIO
2020-05-24 01:16:08 +02:00
import aiohttp
2020-05-24 01:16:08 +02:00
import discord
from discord import Embed
from discord.ext import commands
TOKEN_REPLACEMENT = "" * random.randint(3, 15)
PASSWORD_REPLACEMENT = "" * random.randint(3, 15)
IP_REPLACEMENT = "" * random.randint(3, 15)
2020-09-02 00:08:06 +02:00
2020-05-24 01:16:08 +02:00
class ContextPlus(commands.Context):
# noinspection PyTypedDict
2020-10-22 00:00:48 +02:00
async def send(
self, content=None, *, embed=None, deletable=True, **kwargs
2020-10-22 00:00:48 +02:00
): # 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,
)
# todo: rewrite replacements
2020-10-22 00:00:48 +02:00
if content:
content = (
content.replace(self.bot.config.Core.token, TOKEN_REPLACEMENT)
.replace(
self.bot.config.Core.Database.password,
PASSWORD_REPLACEMENT,
)
.replace(self.bot.config.Core.ip, IP_REPLACEMENT)
.replace(self.bot.config.Core.ip6, IP_REPLACEMENT)
)
if len(content) > 1800:
content = "output too long..."
2020-10-22 00:00:48 +02:00
if embed:
e = embed.to_dict()
for key, value in e.items():
if isinstance(value, (str, bytes)):
2021-04-20 15:51:03 +02:00
# skipcq
e[key] = (
value.replace(
self.bot.config.Core.token, TOKEN_REPLACEMENT
)
.replace(
self.bot.config.Core.Database.password,
PASSWORD_REPLACEMENT,
)
.replace(self.bot.config.Core.ip, IP_REPLACEMENT)
.replace(self.bot.config.Core.ip6, IP_REPLACEMENT)
)
elif isinstance(value, list):
2021-04-20 15:51:03 +02:00
# skipcq
e[key] = replace_in_list(
value, self.bot.config.Core.token, TOKEN_REPLACEMENT
)
2021-04-20 15:51:03 +02:00
# skipcq
e[key] = replace_in_list(
e[key],
self.bot.config.Core.Database.password,
PASSWORD_REPLACEMENT,
)
2021-04-20 15:51:03 +02:00
# skipcq
e[key] = replace_in_list(
e[key], self.bot.config.Core.ip, IP_REPLACEMENT
)
2021-04-20 15:51:03 +02:00
# skipcq
e[key] = replace_in_list(
e[key], self.bot.config.Core.ip6, IP_REPLACEMENT
)
elif isinstance(value, dict):
2021-04-20 15:51:03 +02:00
# skipcq
e[key] = replace_in_dict(
value, self.bot.config.Core.token, TOKEN_REPLACEMENT
)
2021-04-20 15:51:03 +02:00
# skipcq
e[key] = replace_in_dict(
e[key],
self.bot.config.Core.Database.password,
PASSWORD_REPLACEMENT,
)
2021-04-20 15:51:03 +02:00
# skipcq
e[key] = replace_in_dict(
e[key], self.bot.config.Core.ip, IP_REPLACEMENT
)
2021-04-20 15:51:03 +02:00
# skipcq
e[key] = replace_in_dict(
e[key], self.bot.config.Core.ip6, IP_REPLACEMENT
)
2020-10-22 00:00:48 +02:00
embed = Embed.from_dict(e)
2020-06-06 18:51:47 +02:00
if (
hasattr(self.command, "deletable") and self.command.deletable
) and deletable:
2020-10-22 00:00:48 +02:00
message = await super().send(
content=content, embed=embed, **kwargs
2020-10-22 00:00:48 +02:00
)
2020-06-06 18:51:47 +02:00
await message.add_reaction("🗑")
def check(reaction: discord.Reaction, user: discord.User):
2020-06-06 18:51:47 +02:00
return (
user == self.author
and str(reaction.emoji) == "🗑"
and reaction.message.id == message.id
)
try:
2020-09-02 00:08:06 +02:00
await self.bot.wait_for(
"reaction_add", timeout=42.0, check=check
2020-09-02 00:08:06 +02:00
)
except asyncio.TimeoutError:
try:
await message.remove_reaction("🗑", self.bot.user)
except discord.HTTPException:
return None
else:
await message.delete()
return message
2020-10-20 23:56:02 +02:00
return await super().send(content=content, embed=embed, **kwargs)
@property
def session(self) -> aiohttp.ClientSession:
return self.bot.session
def __repr__(self):
2021-01-28 10:39:54 +01:00
items = (
"message=%s" % self.message,
"channel=%s" % self.channel,
"guild=%s" % self.guild,
"author=%s" % self.author,
"prefix=%s" % self.prefix,
"args=%s" % self.args,
"kwargs=%s" % self.kwargs,
)
2021-01-28 10:39:54 +01:00
return "<%s %s>" % (self.__class__.__name__, ", ".join(items))
class CommandPLus(commands.Command):
def __init__(self, function, **kwargs):
super().__init__(function, **kwargs)
self.deletable = kwargs.pop("deletable", True)
def command_extra(*args, **kwargs):
return commands.command(*args, **kwargs, cls=CommandPLus)
class GroupPlus(commands.Group):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.deletable = kwargs.pop("deletable", True)
def group_extra(*args, **kwargs):
return commands.group(*args, **kwargs, cls=GroupPlus)