mirror of
https://github.com/myned/modufur.git
synced 2024-12-24 14:27:27 +00:00
2 > 4, WIP console tweaking
This commit is contained in:
parent
9edaac7803
commit
12c577d9b1
1 changed files with 163 additions and 130 deletions
|
@ -5,6 +5,7 @@ import os
|
|||
import re
|
||||
import sys
|
||||
import traceback as tb
|
||||
from contextlib import suppress
|
||||
|
||||
import discord as d
|
||||
import pyrasite as pyr
|
||||
|
@ -114,57 +115,89 @@ class Tools:
|
|||
@checks.del_ctx()
|
||||
async def console(self, ctx):
|
||||
def execute(msg):
|
||||
if msg.content == 'exit' and msg.author is ctx.author:
|
||||
raise exc.Abort
|
||||
elif msg.author is ctx.author and msg.channel is ctx.channel:
|
||||
if msg.content.startswith('exe') and msg.author is ctx.author and msg.channel is ctx.channel:
|
||||
results.cancel()
|
||||
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
|
||||
|
||||
try:
|
||||
await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}')
|
||||
|
||||
console = await self.generate(ctx)
|
||||
exception = await self.generate_err(ctx)
|
||||
|
||||
await ctx.message.add_reaction('\N{LEFTWARDS ARROW WITH HOOK}')
|
||||
|
||||
while not self.bot.is_closed():
|
||||
try:
|
||||
exe = await self.bot.wait_for('message', check=execute)
|
||||
except exc.Abort:
|
||||
raise exc.Abort
|
||||
await exe.delete()
|
||||
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)
|
||||
print(results)
|
||||
except exc.Execute:
|
||||
try:
|
||||
sys.stdout = io.StringIO()
|
||||
sys.stderr = io.StringIO()
|
||||
exec(exe.content)
|
||||
|
||||
except Exception:
|
||||
await self.refresh_err(exception, tb.format_exc(limit=1))
|
||||
|
||||
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.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:
|
||||
await ctx.send('\N{LEFTWARDS ARROW WITH HOOK} **Exited console.**')
|
||||
pass
|
||||
|
||||
finally:
|
||||
sys.stdout = sys.__stdout__
|
||||
sys.stderr = sys.__stderr__
|
||||
print('Reset sys output.')
|
||||
|
||||
await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}')
|
||||
|
||||
@commands.command(name='arbitrary', aliases=[',arbit', ',ar'])
|
||||
@commands.is_owner()
|
||||
@checks.del_ctx()
|
||||
async def arbitrary(self, ctx, *, exe):
|
||||
try:
|
||||
await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}')
|
||||
|
||||
sys.stdout = io.StringIO()
|
||||
exec(exe)
|
||||
await self.generate(ctx, exe, sys.stdout.getvalue())
|
||||
except Exception:
|
||||
await ctx.send('```\n{}```'.format(tb.format_exc(limit=1)))
|
||||
tb.print_exc(limit=1)
|
||||
finally:
|
||||
sys.stdout = sys.__stdout__
|
||||
print('Reset stdout.')
|
||||
await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}')
|
||||
|
||||
@commands.group(aliases=[',db'], hidden=True)
|
||||
@commands.is_owner()
|
||||
|
|
Loading…
Reference in a new issue