from sys import platform
from discord.ext import commands
import traceback
import requests
import discord
import asyncio
import json
import main

import sys
import io
import os


def insert_returns(body):

    if isinstance(body[-1], ast.Expr):
        body[-1] = ast.Return(body[-1].value)
        ast.fix_missing_locations(body[-1])

    if isinstance(body[-1], ast.If):
        insert_returns(body[-1].body)
        insert_returns(body[-1].orelse)

    if isinstance(body[-1], ast.With):
        insert_returns(body[-1].body)


class admin(commands.Cog):
    def __init__(self, bot: commands.AutoShardedBot):
        self.bot = bot
     
     
      
    
    
 
    @commands.command(pass_context=True)
    @commands.is_owner()
    async def eval(self, ctx, *, cmd):
        fn_name = "_eval_expr"

        cmd = "\n".join(f"    {i}" for i in cmd.splitlines())

        # wrap in async def body
        body = f"async def {fn_name}():\n{cmd}"

        parsed = ast.parse(body)
        body = parsed.body[0].body

        insert_returns(body)

        env = {
            'client': ctx.bot,
            'discord': discord,
            'commands': commands,
            'ctx': ctx,
            'requests': requests,
            'os': os,
            'main': main,
            '__import__': __import__,
            'asyncio': asyncio,
            'ast': ast
            
        }
        exec(compile(parsed, filename="<ast>", mode="exec"), env)

        try:
            result = (await eval(f"{fn_name}()", env))
            if result == None:
                return
            await ctx.send(f'```py\n{result}```')
        except Exception as error:
            await ctx.send(f'```\n{error}```')


def setup(bot):
    bot.add_cog(admin(bot)) 
