feat: Add EvalCog for evaluating Python code snippets with error handling
This commit is contained in:
parent
64ce0f629f
commit
31fd6e5aa7
0
cogs/compv2test.py
Normal file
0
cogs/compv2test.py
Normal file
61
cogs/eval_cog.py
Normal file
61
cogs/eval_cog.py
Normal file
@ -0,0 +1,61 @@
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
import io
|
||||
import textwrap
|
||||
import traceback
|
||||
|
||||
class EvalCog(commands.Cog):
|
||||
def __init__(self, bot):
|
||||
self.bot = bot
|
||||
|
||||
def cleanup_code(self, content):
|
||||
"""Automatically removes code blocks from the code."""
|
||||
# remove ```py\n```
|
||||
if content.startswith('```') and content.endswith('```'):
|
||||
return '\n'.join(content.split('\n')[1:-1])
|
||||
return content.strip('` \n')
|
||||
|
||||
@commands.command(name='evalpy', hidden=True)
|
||||
@commands.is_owner()
|
||||
async def _eval(self, ctx, *, body: str):
|
||||
"""Evaluates a code snippet."""
|
||||
env = {
|
||||
'bot': self.bot,
|
||||
'ctx': ctx,
|
||||
'channel': ctx.channel,
|
||||
'author': ctx.author,
|
||||
'guild': ctx.guild,
|
||||
'message': ctx.message,
|
||||
'discord': discord,
|
||||
'commands': commands
|
||||
}
|
||||
|
||||
env.update(globals())
|
||||
|
||||
body = self.cleanup_code(body)
|
||||
stdout = io.StringIO()
|
||||
|
||||
to_compile = f'async def func():\n{textwrap.indent(body, " ")}'
|
||||
|
||||
try:
|
||||
exec(to_compile, env)
|
||||
except Exception as e:
|
||||
return await ctx.send(f'```py\n{e.__class__.__name__}: {e}\n```')
|
||||
|
||||
func = env['func']
|
||||
try:
|
||||
with io.redirect_stdout(stdout):
|
||||
ret = await func()
|
||||
except Exception as e:
|
||||
value = stdout.getvalue()
|
||||
await ctx.send(f'```py\n{value}{traceback.format_exc()}\n```')
|
||||
else:
|
||||
value = stdout.getvalue()
|
||||
if ret is None:
|
||||
if value:
|
||||
await ctx.send(f'```py\n{value}\n```')
|
||||
else:
|
||||
await ctx.send(f'```py\n{value}{ret}\n```')
|
||||
|
||||
async def setup(bot):
|
||||
await bot.add_cog(EvalCog(bot))
|
Loading…
x
Reference in New Issue
Block a user