From 4e30efea756dc9b4067a662047411dd7621ac1aa Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sat, 12 Sep 2020 18:45:15 +0100 Subject: [PATCH] Copy pasted eval command :pepelaugh: --- cogs/help.py | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/cogs/help.py b/cogs/help.py index 4e9e3b51..3c421ead 100644 --- a/cogs/help.py +++ b/cogs/help.py @@ -18,9 +18,12 @@ # Help paginator by Rapptz # Edited by F4stZ4p # Edited by Hamothy - import asyncio import datetime +import io +import textwrap +import traceback +from contextlib import redirect_stdout from typing import Optional import discord @@ -619,6 +622,36 @@ def error_handling(self, author): return ErrorHandlingEmbed +def cleanup_code(content): + """Automatically removes code blocks from the code.""" + # remove ```py\n``` + if content.startswith('```') and content.endswith('```'): + return '\n'.join(content.split('\n')[1:-1]) + + # remove `foo` + return content.strip('` \n') + + +def paginate(text: str): + """Simple generator that paginates text.""" + last = 0 + pages = [] + for curr in range(0, len(text)): + if curr % 1980 == 0: + pages.append(text[last:curr]) + last = curr + appd_index = curr + if appd_index != len(text) - 1: + pages.append(text[last:curr]) + return list(filter(lambda a: a != '', pages)) + + +def get_syntax_error(e): + if e.text is None: + return f'```py\n{e.__class__.__name__}: {e}\n```' + return f'```py\n{e.text}{"^":>{e.offset}}\n{e.__class__.__name__}: {e}```' + + # Set up the Cog class Help(Cog): """Help Commands!""" @@ -690,6 +723,75 @@ class Help(Cog): prefix = self.bot.get_prefix_for_guild(ctx.guild.id) await self.bot.generate_embed(ctx, desc=f"**The current guild prefix is `{prefix}`**") + @command(name="eval", hidden=True) + @is_owner() + async def _eval(self, ctx, *, body): + """ + Evaluates python code + Gracefully yoinked from (https://github.com/fourjr/eval-bot)""" + + env = { + 'ctx': ctx, + 'bot': self.bot, + 'channel': ctx.channel, + 'author': ctx.author, + 'guild': ctx.guild, + 'message': ctx.message, + 'source': inspect.getsource + } + + env.update(globals()) + + body = cleanup_code(body) + stdout = io.StringIO() + err = out = None + + to_compile = f'async def func():\n{textwrap.indent(body, " ")}' + + try: + exec(to_compile, env) + except Exception as e: + err = await ctx.send(f'```py\n{e.__class__.__name__}: {e}\n```') + return await ctx.message.add_reaction('\u2049') + + func = env['func'] + try: + with redirect_stdout(stdout): + ret = await func() + except Exception as e: + value = stdout.getvalue() + err = await ctx.send(f'```py\n{value}{traceback.format_exc()}\n```') + else: + value = stdout.getvalue() + if ret is None: + if value: + try: + out = await ctx.send(f'```py\n{value}\n```') + except: + paginated_text = paginate(value) + for page in paginated_text: + if page == paginated_text[-1]: + out = await ctx.send(f'```py\n{page}\n```') + break + await ctx.send(f'```py\n{page}\n```') + else: + try: + out = await ctx.send(f'```py\n{value}{ret}\n```') + except: + paginated_text = paginate(f"{value}{ret}") + for page in paginated_text: + if page == paginated_text[-1]: + out = await ctx.send(f'```py\n{page}\n```') + break + await ctx.send(f'```py\n{page}\n```') + + if out: + await ctx.message.add_reaction('\u2705') # tick + elif err: + await ctx.message.add_reaction('\u2049') # x + else: + await ctx.message.add_reaction('\u2705') + @command(name="support") async def support(self, ctx): """Joining Support Server And Sending Feedback"""