refactor(all): start from new
feat(doc): add readme file
This commit is contained in:
parent
28d1d71c5a
commit
078dc075f2
22 changed files with 120 additions and 311 deletions
89
tuxbot/core/utils/functions/cli.py
Normal file
89
tuxbot/core/utils/functions/cli.py
Normal file
|
@ -0,0 +1,89 @@
|
|||
import codecs
|
||||
import itertools
|
||||
import sys
|
||||
|
||||
|
||||
def bordered(*columns: dict) -> str:
|
||||
"""
|
||||
credits to https://github.com/Cog-Creators/Red-DiscordBot/blob/V3/develop/redbot/core/utils/chat_formatting.py
|
||||
|
||||
Get two blocks of text in a borders.
|
||||
|
||||
Note
|
||||
----
|
||||
This will only work with a monospaced font.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
*columns : `sequence` of `str`
|
||||
The columns of text, each being a list of lines in that column.
|
||||
|
||||
Returns
|
||||
-------
|
||||
str
|
||||
The bordered text.
|
||||
|
||||
"""
|
||||
encoder = codecs.getencoder(sys.stdout.encoding)
|
||||
try:
|
||||
encoder("┌┐└┘─│") # border symbols
|
||||
except UnicodeEncodeError:
|
||||
ascii_border = True
|
||||
else:
|
||||
ascii_border = False
|
||||
|
||||
borders = {
|
||||
"TL": "+" if ascii_border else "┌", # Top-left
|
||||
"TR": "+" if ascii_border else "┐", # Top-right
|
||||
"BL": "+" if ascii_border else "└", # Bottom-left
|
||||
"BR": "+" if ascii_border else "┘", # Bottom-right
|
||||
"HZ": "-" if ascii_border else "─", # Horizontal
|
||||
"VT": "|" if ascii_border else "│", # Vertical
|
||||
}
|
||||
|
||||
sep = " " * 4 # Separator between boxes
|
||||
widths = tuple(
|
||||
max(
|
||||
len(row) for row in column.get('rows')
|
||||
) + 9
|
||||
for column in columns
|
||||
) # width of each col
|
||||
cols_done = [False] * len(columns) # whether or not each column is done
|
||||
lines = [""]
|
||||
|
||||
for i, column in enumerate(columns):
|
||||
lines[0] += "{TL}" + "{HZ}" + column.get('title') \
|
||||
+ "{HZ}" * (widths[i] - len(column.get('title')) - 1) \
|
||||
+ "{TR}" + sep
|
||||
|
||||
for line in itertools.zip_longest(
|
||||
*[column.get('rows') for column in columns]
|
||||
):
|
||||
row = []
|
||||
for colidx, column in enumerate(line):
|
||||
width = widths[colidx]
|
||||
done = cols_done[colidx]
|
||||
if column is None:
|
||||
if not done:
|
||||
# bottom border of column
|
||||
column = "{HZ}" * width
|
||||
row.append("{BL}" + column + "{BR}")
|
||||
cols_done[colidx] = True # mark column as done
|
||||
else:
|
||||
# leave empty
|
||||
row.append(" " * (width + 2))
|
||||
else:
|
||||
column += " " * (width - len(column)) # append padded spaces
|
||||
row.append("{VT}" + column + "{VT}")
|
||||
|
||||
lines.append(sep.join(row))
|
||||
|
||||
final_row = []
|
||||
for width, done in zip(widths, cols_done):
|
||||
if not done:
|
||||
final_row.append("{BL}" + "{HZ}" * width + "{BR}")
|
||||
else:
|
||||
final_row.append(" " * (width + 2))
|
||||
lines.append(sep.join(final_row))
|
||||
|
||||
return "\n".join(lines).format(**borders)
|
114
tuxbot/core/utils/functions/extra.py
Normal file
114
tuxbot/core/utils/functions/extra.py
Normal file
|
@ -0,0 +1,114 @@
|
|||
import ast
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
|
||||
import discord
|
||||
from discord.ext import commands, flags
|
||||
|
||||
from configs.bot.protected import protected
|
||||
from configs.bot.settings import prefixes
|
||||
|
||||
|
||||
class ContextPlus(commands.Context):
|
||||
async def send(self, content=None, *args, **kwargs):
|
||||
if content is not None:
|
||||
for value in protected:
|
||||
content = content.replace(
|
||||
str(value),
|
||||
'[Deleted]'
|
||||
)
|
||||
|
||||
if kwargs.get('content') is not None:
|
||||
for value in protected:
|
||||
kwargs['content'] = kwargs['content'].replace(
|
||||
str(value),
|
||||
'[Deleted]'
|
||||
)
|
||||
|
||||
if kwargs.get('embeds') is not None and len(kwargs.get('embeds')) > 0:
|
||||
for i, embed in enumerate(kwargs.get('embeds')):
|
||||
embed = str(kwargs.get('embed').to_dict())
|
||||
for value in protected:
|
||||
embed = embed.replace(str(value), '[Deleted]')
|
||||
kwargs['embeds'][i] = discord.Embed.from_dict(
|
||||
ast.literal_eval(embed)
|
||||
)
|
||||
|
||||
if kwargs.get('embed') is not None:
|
||||
embed = str(kwargs.get('embed').to_dict())
|
||||
for value in protected:
|
||||
embed = embed.replace(str(value), '[Deleted]')
|
||||
kwargs['embed'] = discord.Embed.from_dict(
|
||||
ast.literal_eval(embed)
|
||||
)
|
||||
|
||||
if (hasattr(self.command, 'deletable') and self.command.deletable) \
|
||||
and kwargs.pop('deletable', True):
|
||||
message = await super().send(content, *args, **kwargs)
|
||||
await message.add_reaction('🗑')
|
||||
|
||||
def check(reaction: discord.Reaction, user: discord.User):
|
||||
return user == self.author \
|
||||
and str(reaction.emoji) == '🗑' \
|
||||
and reaction.message.id == message.id
|
||||
|
||||
try:
|
||||
await self.bot.wait_for(
|
||||
'reaction_add',
|
||||
timeout=60.0,
|
||||
check=check
|
||||
)
|
||||
except asyncio.TimeoutError:
|
||||
await message.remove_reaction('🗑', self.bot.user)
|
||||
else:
|
||||
await message.delete()
|
||||
return message
|
||||
else:
|
||||
return await super().send(content, *args, **kwargs)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
async def get_prefix(bot, message):
|
||||
custom_prefix = prefixes
|
||||
if message.guild:
|
||||
path = f"configs/guilds/{str(message.guild.id)}.json"
|
||||
|
||||
if os.path.exists(path):
|
||||
with open(path) as f:
|
||||
datas = json.load(f)
|
||||
|
||||
custom_prefix = datas["Prefix"]
|
||||
|
||||
return commands.when_mentioned_or(*custom_prefix)(bot, message)
|
||||
|
||||
|
||||
def get_owners() -> list:
|
||||
with open("configs/bot/whitelist.json") as f:
|
||||
datas = json.load(f)
|
||||
|
||||
return datas['owners']
|
||||
|
||||
|
||||
def get_blacklist() -> dict:
|
||||
with open("configs/bot/blacklist.json") as f:
|
||||
return json.load(f)
|
Loading…
Add table
Add a link
Reference in a new issue