change things, somehow

This commit is contained in:
2021-01-14 09:38:32 +00:00
parent 2ff9497f61
commit 6d18a5c56e
6 changed files with 97 additions and 24 deletions
+27 -8
View File
@@ -1,20 +1,24 @@
import util
import asyncio
import traceback
import re
from discord.ext import commands
import util
def setup(bot):
async def admin_check(ctx):
return await bot.is_owner(ctx.author)
@bot.group()
@commands.check(admin_check)
@commands.check(util.admin_check)
async def magic(ctx):
if ctx.invoked_subcommand == None:
return await ctx.send("Invalid magic command.")
@magic.command(rest_is_raw=True)
async def py(ctx, *, code):
timeout = 5.0
timeout_match = re.search("#timeout:([0-9]+)", code, re.IGNORECASE)
if timeout_match:
timeout = int(timeout_match.group(1))
if timeout == 0: timeout = None
code = util.extract_codeblock(code)
try:
loc = {
@@ -23,13 +27,24 @@ def setup(bot):
"ctx": ctx,
"db": bot.database
}
result = await asyncio.wait_for(util.async_exec(code, loc, globals()), timeout=5.0)
def check(re, u): return str(re.emoji) == "" and u == ctx.author
result = None
async def run():
nonlocal result
result = await util.async_exec(code, loc, globals())
halt_task = asyncio.create_task(bot.wait_for("reaction_add", check=check))
exec_task = asyncio.create_task(run())
done, pending = await asyncio.wait((exec_task, halt_task), timeout=timeout, return_when=asyncio.FIRST_COMPLETED)
for task in done: task.result() # get exceptions
for task in pending: task.cancel()
if result != None:
if isinstance(result, str):
await ctx.send(result[:1999])
await ctx.send(result[:2000])
else:
await ctx.send(util.gen_codeblock(repr(result)))
except TimeoutError:
except (TimeoutError, asyncio.CancelledError):
await ctx.send(embed=util.error_embed("Timed out."))
except BaseException as e:
await ctx.send(embed=util.error_embed(util.gen_codeblock(traceback.format_exc())))
@@ -51,4 +66,8 @@ def setup(bot):
@magic.command()
async def reload_config(ctx):
util.load_config()
ctx.send("Done!")
ctx.send("Done!")
@magic.command()
async def reload_ext(ctx):
for ext in util.extensions: bot.reload_extension(ext)