2020-05-27 00:58:53 +02:00
|
|
|
import asyncio
|
2020-11-12 00:03:01 +01:00
|
|
|
import random
|
2020-05-24 01:16:08 +02:00
|
|
|
|
2021-01-26 15:24:10 +01:00
|
|
|
import aiohttp
|
2020-05-24 01:16:08 +02:00
|
|
|
import discord
|
2020-08-29 01:01:34 +02:00
|
|
|
from discord import Embed
|
2020-11-12 00:03:01 +01:00
|
|
|
from discord.ext import commands
|
2020-05-27 00:58:53 +02:00
|
|
|
|
2021-01-26 17:10:31 +01:00
|
|
|
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
|
|
|
|
2019-12-16 19:36:55 +01:00
|
|
|
|
2020-05-24 01:16:08 +02:00
|
|
|
class ContextPlus(commands.Context):
|
2020-10-22 00:00:48 +02:00
|
|
|
async def send(
|
|
|
|
self,
|
|
|
|
content=None,
|
|
|
|
*,
|
|
|
|
tts=False,
|
|
|
|
embed=None,
|
|
|
|
file=None,
|
|
|
|
files=None,
|
|
|
|
delete_after=None,
|
|
|
|
nonce=None,
|
|
|
|
allowed_mentions=None,
|
2021-01-25 17:28:59 +01:00
|
|
|
deletable=True
|
2020-10-22 00:00:48 +02:00
|
|
|
): # i know *args and **kwargs but, i prefer work with same values
|
2021-01-26 17:10:31 +01:00
|
|
|
from tuxbot.core.utils.functions.utils import (
|
|
|
|
replace_in_dict,
|
|
|
|
replace_in_list,
|
|
|
|
)
|
2021-01-25 17:28:59 +01:00
|
|
|
|
2020-10-22 00:00:48 +02:00
|
|
|
if content:
|
2021-01-26 11:59:36 +01:00
|
|
|
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)
|
2020-10-19 01:37:12 +02:00
|
|
|
)
|
2020-10-22 00:00:48 +02:00
|
|
|
if embed:
|
|
|
|
e = embed.to_dict()
|
|
|
|
for key, value in e.items():
|
2020-10-19 01:37:12 +02:00
|
|
|
if isinstance(value, (str, bytes)):
|
2021-01-26 11:59:36 +01:00
|
|
|
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)
|
2020-10-19 01:37:12 +02:00
|
|
|
)
|
2021-01-26 17:10:31 +01:00
|
|
|
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
|
|
|
|
)
|
2020-10-22 00:00:48 +02:00
|
|
|
embed = Embed.from_dict(e)
|
2020-08-29 01:01:34 +02:00
|
|
|
|
2020-06-06 18:51:47 +02:00
|
|
|
if (
|
|
|
|
hasattr(self.command, "deletable") and self.command.deletable
|
2021-01-25 17:28:59 +01:00
|
|
|
) and deletable:
|
2020-10-22 00:00:48 +02:00
|
|
|
message = await super().send(
|
|
|
|
content=content,
|
|
|
|
tts=tts,
|
|
|
|
embed=embed,
|
|
|
|
file=file,
|
|
|
|
files=files,
|
|
|
|
delete_after=delete_after,
|
|
|
|
nonce=nonce,
|
|
|
|
allowed_mentions=allowed_mentions,
|
|
|
|
)
|
2020-06-06 18:51:47 +02:00
|
|
|
await message.add_reaction("🗑")
|
2020-05-27 00:58:53 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
)
|
2020-05-27 00:58:53 +02:00
|
|
|
|
|
|
|
try:
|
2020-09-02 00:08:06 +02:00
|
|
|
await self.bot.wait_for(
|
2020-10-19 00:20:58 +02:00
|
|
|
"reaction_add", timeout=42.0, check=check
|
2020-09-02 00:08:06 +02:00
|
|
|
)
|
2020-05-27 00:58:53 +02:00
|
|
|
except asyncio.TimeoutError:
|
2020-06-06 18:51:47 +02:00
|
|
|
await message.remove_reaction("🗑", self.bot.user)
|
2020-05-27 00:58:53 +02:00
|
|
|
else:
|
|
|
|
await message.delete()
|
2020-05-31 22:49:04 +02:00
|
|
|
return message
|
2020-10-20 23:56:02 +02:00
|
|
|
|
2020-10-22 00:00:48 +02:00
|
|
|
return await super().send(
|
|
|
|
content=content,
|
|
|
|
tts=tts,
|
|
|
|
embed=embed,
|
|
|
|
file=file,
|
|
|
|
files=files,
|
|
|
|
delete_after=delete_after,
|
|
|
|
nonce=nonce,
|
|
|
|
allowed_mentions=allowed_mentions,
|
|
|
|
)
|
2020-05-27 00:58:53 +02:00
|
|
|
|
2021-01-26 15:24:10 +01:00
|
|
|
@property
|
|
|
|
def session(self) -> aiohttp.ClientSession:
|
|
|
|
return self.bot.session
|
|
|
|
|
2021-01-27 15:14:05 +01:00
|
|
|
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-27 15:14:05 +01:00
|
|
|
|
2021-01-28 10:39:54 +01:00
|
|
|
return "<%s %s>" % (self.__class__.__name__, ", ".join(items))
|
2021-01-27 15:14:05 +01:00
|
|
|
|
2020-05-27 00:58:53 +02:00
|
|
|
|
2020-11-12 00:03:01 +01:00
|
|
|
class CommandPLus(commands.Command):
|
2020-05-27 00:58:53 +02:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2020-11-12 00:03:01 +01:00
|
|
|
class GroupPlus(commands.Group):
|
2020-05-27 00:58:53 +02:00
|
|
|
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)
|