Sfoglia il codice sorgente

Moved eval code back, fixed small bug in embed/help

master
Skyweb 1 anno fa
parent
commit
cadea1ec81
3 ha cambiato i file con 57 aggiunte e 61 eliminazioni
  1. +32
    -35
      cogs/admin.py
  2. +14
    -17
      utils/embedinator.py
  3. +11
    -9
      utils/sorahelp.py

+ 32
- 35
cogs/admin.py Vedi File

@@ -60,52 +60,49 @@ class Admin(commands.Cog, command_attrs=dict(hidden=True)):
"""Evaluates and runs arbitrary python code, use with care"""

async with context.channel.typing():
await self._eval(context, body)
env = {"bot": self.bot, "context": context, "ctx": context}

async def _eval(self, context, body):
env = {"bot": self.bot, "context": context, "ctx": context}
env.update(globals())

env.update(globals())
body = self.cleanup_code(body)
stdout = io.StringIO()

body = self.cleanup_code(body)
stdout = io.StringIO()
to_compile = f'async def func():\n{textwrap.indent(body, " ")}'

to_compile = f'async def func():\n{textwrap.indent(body, " ")}'

try:
exec(to_compile, env)
except Exception as e:
await context.send(f"```py\n{e.__class__.__name__}: {e}\n```")

return
try:
exec(to_compile, env)
except Exception as e:
await context.send(f"```py\n{e.__class__.__name__}: {e}\n```")

func = env["func"]
try:
with redirect_stdout(stdout):
result = await func()
except Exception:
value = stdout.getvalue()
await context.send(f"```py\n{value}{traceback.format_exc()}\n```")
return

func = env["func"]
try:
await context.message.add_reaction("❌")
with redirect_stdout(stdout):
result = await func()
except Exception:
pass
value = stdout.getvalue()
await context.send(f"```py\n{value}{traceback.format_exc()}\n```")

else:
value = stdout.getvalue()
try:
await context.message.add_reaction("✅")
except Exception:
pass
try:
await context.message.add_reaction("❌")
except Exception:
pass

if result is None:
if value:
await context.send(f"```py\n{value}\n```")
else:
await context.send(f"```No output```")
else:
await context.send(f"```py\n{value}{result}\n```")
value = stdout.getvalue()
try:
await context.message.add_reaction("✅")
except Exception:
pass

if result is None:
if value:
await context.send(f"```py\n{value}\n```")
else:
await context.send(f"```No output```")
else:
await context.send(f"```py\n{value}{result}\n```")

@commands.command(aliases=["quit"])
async def restart(self, context):

+ 14
- 17
utils/embedinator.py Vedi File

@@ -3,20 +3,17 @@ import discord


class Embedinator:
def __init__(self, bot, destination, member=None, **kwargs):
def __init__(self, bot, member=None, **kwargs):
self.bot = bot
self.destination = destination
self.member = member
self.max_fields = kwargs.get("max_fields", 25)
self.base_embed = discord.Embed(**kwargs)
self.embed_list = []
self.embed_list = [self.base_embed.copy()]
self.current = 0
self.buttons = ["◀", "▶", "⏹"]
self.add_page()

@property
def last_page(self):
if len(self.embed_list) == 0:
self.add_page()
return self.embed_list[-1]

def add_page(self):
@@ -25,13 +22,14 @@ class Embedinator:
self.set_footer()

def add_field(self, *, name, value, inline=True):
if len(self.last_page().fields) >= self.max_fields:
if len(self.last_page.fields) >= self.max_fields:
self.add_page()
self.last_page().add_field(name=name, value=value, inline=inline)
self.last_page.add_field(name=name, value=value, inline=inline)

async def send(self):
self.message = await self.destination.send(embed=self.embed_list[self.current])
return self.message
async def send(self, destination):
self.message = await destination.send(embed=self.embed_list[0])
if len(self.embed_list) > 1:
await self.handle()

async def edit(self):
await self.message.edit(embed=self.embed_list[self.current])
@@ -72,12 +70,11 @@ class Embedinator:
return

def check_reaction(self, reaction, user):
checks = [
reaction.message.id == self.message.id,
str(reaction.emoji) in self.buttons,
(self.member is None or user.id == self.member.id),
]
return all(checks)
return (
reaction.message.id == self.message.id
and str(reaction.emoji) in self.buttons
and (self.member is None or user.id == self.member.id)
)

def check_delete(self, message):
return message.id == self.message.id

+ 11
- 9
utils/sorahelp.py Vedi File

@@ -42,13 +42,15 @@ class SoraHelpCommand(commands.HelpCommand):
await self.send_embedinator(embedinator)

async def send_bot_help(self, mapping):
embedinator = self.create_embedinator(description=self.get_opening_note())
embedinator = self.create_embedinator(
description=self.get_opening_note())

for cog, cog_commands in mapping.items():
filtered = await self.filter_commands(cog_commands)
if filtered:
if len(embedinator.last_page().fields) != 0:
if len(embedinator.last_page.fields) != 0:
embedinator.add_page()

embedinator.add_field(
name=f"Category: {cog.qualified_name}", value=cog.description or "No description", inline=False
)
@@ -81,15 +83,17 @@ class SoraHelpCommand(commands.HelpCommand):

def create_embed(self):
embed = discord.Embed(colour=self.colour)
embed.set_author(name=self.context.bot.user.name, icon_url=self.context.bot.user.avatar_url)
embed.set_author(name=self.context.bot.user.name,
icon_url=self.context.bot.user.avatar_url)

return embed

def create_embedinator(self, **kwargs):
destination = self.get_destination()
embedinator = Embedinator(self.context.bot, destination, self.context.author, **kwargs, colour=self.colour)
embedinator = Embedinator(
self.context.bot, self.context.author, **kwargs, colour=self.colour)

embedinator.set_author(name=self.context.bot.user.name, icon_url=self.context.bot.user.avatar_url)
embedinator.set_author(
name=self.context.bot.user.name, icon_url=self.context.bot.user.avatar_url)

return embedinator

@@ -98,6 +102,4 @@ class SoraHelpCommand(commands.HelpCommand):
embedinator.add_field(name=name, value=command.short_doc, inline=False)

async def send_embedinator(self, embedinator):
await embedinator.send()
if len(embedinator.embed_list) != 1:
await embedinator.handle()
await embedinator.send(self.get_destination())

Loading…
Annulla
Salva