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

103 lines
2.9 KiB
Python
Raw Normal View History

import asyncio
2020-05-24 01:16:08 +02:00
import discord
from discord import Embed
from discord.ext import commands, flags
2020-09-02 00:08:06 +02:00
from rich.console import Console
2020-09-02 00:08:06 +02:00
console = Console()
TOKEN_REPLACEMENT = "whoops, leaked token"
2020-09-02 00:08:06 +02: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,
deletable=False
): # i know *args and **kwargs but, i prefer work with same values
if content:
content = content.replace(
self.bot.config.Core.token, TOKEN_REPLACEMENT
)
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)):
2020-10-22 00:00:48 +02:00
e[key] = value.replace(
self.bot.config.Core.token, TOKEN_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
2020-10-22 00:00:48 +02:00
) or deletable:
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("🗑")
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:
2020-06-06 18:51:47 +02:00
await message.remove_reaction("🗑", self.bot.user)
else:
await message.delete()
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,
)
class CommandPLus(flags.FlagCommand):
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(flags.FlagGroup):
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)