Created
June 2, 2018 08:36
-
-
Save AEnterprise/c74e2afc0dc701d3af4fd5169a05f858 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import contextlib | |
import io | |
from disco.bot import Bot, Plugin | |
class botplugin(Plugin): | |
@Plugin.command('ping') | |
def on_ping_command(self, event): | |
event.msg.reply('Pong!') | |
@Plugin.command('eval', '[code:str...]') | |
def eval(self, event, code): | |
env = { | |
'bot': Bot, | |
'event': event, | |
'msg': event.msg | |
} | |
env.update(globals()) | |
if code.startswith('```'): | |
code = "\n".join(code.split("\n")[1:-1]) | |
out = io.StringIO() | |
to_compile = f'async def func():\n{textwrap.indent(code, " ")}' | |
try: | |
exec(to_compile, env) | |
except Exception as e: | |
return event.msg.reply(f'```py\n{e.__class__.__name__}: {e}\n```') | |
func = env['func'] | |
try: | |
with contextlib.redirect_stdout(out): | |
ret = func() | |
except Exception as e: | |
value = out.getvalue() | |
event.msg.reply(f'```py\n{value}{traceback.format_exc()}\n```') | |
else: | |
value = out.getvalue() | |
if ret is None: | |
if value: | |
event.msg.reply(f'```py\n{value}\n```') | |
else: | |
event.msg.reply(f'```py\n{value}{ret}\n```') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment