def eval_(self, ctx, *, code: cleanup_code):
"""Alternative to `debug` that executes code inside a coroutine.
Allows multiple lines and `await`ing.
This is a modified version of RoboDanny's latest `eval` command.
"""
env = get_env(ctx)
to_compile = 'async def _func():\n%s' % textwrap.indent(code, ' ')
stdout = io.StringIO()
try:
exec(to_compile, env)
except SyntaxError as e:
await ctx.send(self.eval_output('\n'.join(get_syntax_error(e).splitlines()[1:-1])))
return
func = env['_func']
try:
with redirect_stdout(stdout):
ret = await func()
except Exception as e:
value = stdout.getvalue()
exc = traceback.format_exc().replace(UPPER_PATH, '...').splitlines()
exc = '\n'.join([exc[0], *exc[3:]])
await ctx.send(self.eval_output(f'{value}{exc}'))
else:
value = stdout.getvalue()
if isinstance(ret, discord.Embed):
await ctx.send(self.eval_output(value if value else None), embed=ret)
else:
await ctx.send(self.eval_output(value if ret is None else f'{value}{rep(ret)}'))
评论列表
文章目录