Необычное задание, вобщем-то нужно расставить табы в коде по правилам PEP-8(если кому интересно это discord.py) код:
import discord
from discord.ext import commands
import ast
class Eval(commands.Cog):
def __init__(self, client):
self.client = client
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)
@commands.command()
async def e(ctx, *, cmd):
if ctx.author.id != 580681990240469013:
emb = discord.Embed(title="\n❌ Произошла ошибка", description=str(error), color=0xff0000)
await ctx.send(embed=emb)
else:
try:
fn_name = "_eval_expr"
cmd = cmd.strip("` ")
cmd = "\n".join(f" {i}" for i in cmd.splitlines())
body = f"async def {fn_name}():\n{cmd}"
parsed = ast.parse(body)
body = parsed.body[0].body
insert_returns(body)
env = { "bot":client, 'discord': discord, 'commands': commands, 'ctx': ctx, '__import__': __import__ }
exec(compile(parsed, filename=" ", mode="exec"), env)
result = (await eval(f"{fn_name}()", env))
except Exception as error:
emb = discord.Embed(title="\n❌ Произошла ошибка", description=str(error), color=0xff0000)
await ctx.send(embed=emb)
def setup(client):
client.add_cog(Eval(client))