async def _eval(ctx, *, body): """Evaluates python code""" env = { 'ctx': ctx, 'channel': ctx.channel, 'author': ctx.author, 'guild': ctx.guild, 'message': ctx.message, '_': bot._last_result, 'source': inspect.getsource, 'session': bot.session } env.update(globals()) body = utils.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 = utils.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: bot._last_result = ret try: out = await ctx.send(f'```py\n{value}{ret}\n```') except: paginated_text = utils.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')
async def hastebin(self, ctx, *, code): '''Hastebin-ify your code!''' code = utils.cleanup_code(code) async with self.bot.session.post("https://hastebin.com/documents", data=code) as resp: data = await resp.json() key = data['key'] await ctx.send(f"Hastebin-ified! <https://hastebin.com/{key}.py>")