1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-12-25 06:37:29 +00:00

2 > 4, WIP console tweaking

This commit is contained in:
Myned 2017-10-20 16:24:43 -04:00
parent 9edaac7803
commit 12c577d9b1

View file

@ -5,6 +5,7 @@ import os
import re import re
import sys import sys
import traceback as tb import traceback as tb
from contextlib import suppress
import discord as d import discord as d
import pyrasite as pyr import pyrasite as pyr
@ -114,57 +115,89 @@ class Tools:
@checks.del_ctx() @checks.del_ctx()
async def console(self, ctx): async def console(self, ctx):
def execute(msg): def execute(msg):
if msg.content == 'exit' and msg.author is ctx.author: if msg.content.startswith('exe') and msg.author is ctx.author and msg.channel is ctx.channel:
raise exc.Abort results.cancel()
elif msg.author is ctx.author and msg.channel is ctx.channel:
return True return True
else: return False
def evaluate(msg):
if msg.content.startswith('eval') and msg.author is ctx.author and msg.channel is ctx.channel:
results.cancel()
return True
return False
def exit(reaction, user):
if reaction.emoji == '\N{LEFTWARDS ARROW WITH HOOK}' and user is ctx.author and reaction.message.id == ctx.message.id:
results.cancel()
raise exc.Abort
return False return False
try: try:
await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}')
console = await self.generate(ctx) console = await self.generate(ctx)
exception = await self.generate_err(ctx) exception = await self.generate_err(ctx)
await ctx.message.add_reaction('\N{LEFTWARDS ARROW WITH HOOK}')
while not self.bot.is_closed(): while not self.bot.is_closed():
try: try:
exe = await self.bot.wait_for('message', check=execute) results = await asyncio.gather([self.bot.wait_for('message', check=execute), self.bot.wait_for('message', check=evaluate), self.bot.wait_for('reaction_add', check=exit)], return_exceptions=True)
except exc.Abort: print(results)
raise exc.Abort except exc.Execute:
await exe.delete()
try: try:
sys.stdout = io.StringIO() sys.stdout = io.StringIO()
sys.stderr = io.StringIO() sys.stderr = io.StringIO()
exec(exe.content) exec(exe.content)
except Exception: except Exception:
await self.refresh_err(exception, tb.format_exc(limit=1)) await self.refresh_err(exception, tb.format_exc(limit=1))
finally: finally:
await self.refresh(console, exe.content, sys.stdout.getvalue()) await self.refresh(console, exe.content, sys.stdout.getvalue() if sys.stdout.getvalue() != console.content else None)
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__ sys.stderr = sys.__stderr__
except exc.Evaluate:
try:
sys.stdout = io.StringIO()
sys.stderr = io.StringIO()
eval(exe.content)
except Exception:
await self.refresh_err(exception, tb.format_exc(limit=1))
finally:
await self.refresh(console, exe.content, sys.stdout.getvalue() if sys.stdout.getvalue() != console.content else None)
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
finally:
with suppress(d.NotFound):
await exe.delete()
except exc.Abort: except exc.Abort:
await ctx.send('\N{LEFTWARDS ARROW WITH HOOK} **Exited console.**') pass
finally: finally:
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__ sys.stderr = sys.__stderr__
print('Reset sys output.') print('Reset sys output.')
await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}')
@commands.command(name='arbitrary', aliases=[',arbit', ',ar']) @commands.command(name='arbitrary', aliases=[',arbit', ',ar'])
@commands.is_owner() @commands.is_owner()
@checks.del_ctx() @checks.del_ctx()
async def arbitrary(self, ctx, *, exe): async def arbitrary(self, ctx, *, exe):
try: try:
await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}')
sys.stdout = io.StringIO() sys.stdout = io.StringIO()
exec(exe) exec(exe)
await self.generate(ctx, exe, sys.stdout.getvalue()) await self.generate(ctx, exe, sys.stdout.getvalue())
except Exception: except Exception:
await ctx.send('```\n{}```'.format(tb.format_exc(limit=1))) await ctx.send('```\n{}```'.format(tb.format_exc(limit=1)))
tb.print_exc(limit=1)
finally: finally:
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
print('Reset stdout.') print('Reset stdout.')
await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}')
@commands.group(aliases=[',db'], hidden=True) @commands.group(aliases=[',db'], hidden=True)
@commands.is_owner() @commands.is_owner()