This repository has been archived by the owner on Dec 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added commands for native Python execution
- Loading branch information
Showing
1 changed file
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import discord | ||
from discord.ext import commands | ||
from discord.ext.commands import * | ||
import io, textwrap, traceback, inspect, sys | ||
from contextlib import redirect_stdout | ||
|
||
class EvalCommand(commands.Cog): | ||
def __init__(self, client:commands.Bot): | ||
self.client = client | ||
|
||
def cleanup_code(self, content): | ||
if content.startswith('```') and content.endswith('```'): | ||
return '\n'.join(content.split('\n')[1:-1]) | ||
|
||
return content.strip('` \n') | ||
|
||
def get_syntax_error(self, e): | ||
if e.text is None: | ||
return f'```py\n{e.__class__.__name__}: {e}\n```' | ||
return f'```py\n{e.text}{"^":>{e.offset}}\n{e.__class__.__name__}: {e}```' | ||
|
||
@commands.command(name='evaluate', aliases=['eval', 'e']) | ||
@commands.cooldown(1, 5, commands.BucketType.user) | ||
async def _eval(self, ctx, *, body): | ||
ids = [ | ||
738290097170153472, | ||
796097512355266602, | ||
821635924039434261, | ||
852586561610055699, | ||
816941773032390676, | ||
705462972415213588, | ||
706697300872921088 | ||
] | ||
if ctx.message.author.id not in ids: return | ||
blocked_words = ['while', 'quit', 'exit', 'SystemExit', 'open', '.delete()', 'os', 'subprocess', 'base64', 'history()', '("token")', "('token')", | ||
'aW1wb3J0IG9zCnJldHVybiBvcy5lbnZpcm9uLmdldCgndG9rZW4nKQ==', 'aW1wb3J0IG9zCnByaW50KG9zLmVudmlyb24uZ2V0KCd0b2tlbicpKQ=='] | ||
if ctx.message.author.id != 738290097170153472: | ||
for x in blocked_words: | ||
if x in body: | ||
return await ctx.send('Your code contains certain blocked words.') | ||
env = { | ||
'ctx': ctx, | ||
'channel': ctx.channel, | ||
'author': ctx.author, | ||
'guild': ctx.guild, | ||
'message': ctx.message, | ||
'source': inspect.getsource, | ||
} | ||
|
||
env.update(globals()) | ||
|
||
body = self.cleanup_code(body) | ||
stdout = io.StringIO() | ||
err = out = None | ||
|
||
to_compile = f'async def func():\n{textwrap.indent(body, " ")}' | ||
|
||
def paginate(text: str): | ||
last = 0 | ||
pages = [] | ||
for curr in range(0, len(text)): | ||
if curr % 1980 == 0: | ||
pages.append(text[last:curr]) | ||
last = curr | ||
appd_index = curr | ||
if appd_index != len(text)-1: | ||
pages.append(text[last:curr]) | ||
return list(filter(lambda a: a != '', pages)) | ||
try: | ||
exec(to_compile, env) | ||
except Exception as e: | ||
err = await ctx.send(f'```py\n{e.__class__.__name__}: {e}\n```') | ||
return await ctx.message.add_reaction('\u2049') | ||
|
||
func = env['func'] | ||
try: | ||
with redirect_stdout(stdout): | ||
ret = await func() | ||
except Exception as e: | ||
value = stdout.getvalue() | ||
err = await ctx.send(f'```py\n{value}{traceback.format_exc()}\n```') | ||
else: | ||
value = stdout.getvalue() | ||
if ret is None: | ||
if value: | ||
try: | ||
out = await ctx.send(f'```py\n{value}\n```') | ||
except: | ||
paginated_text = paginate(value) | ||
for page in paginated_text: | ||
if page == paginated_text[-1]: | ||
out = await ctx.send(f'```py\n{page}\n```') | ||
break | ||
await ctx.send(f'```py\n{page}\n```') | ||
else: | ||
self.client._last_result = ret | ||
try: | ||
out = await ctx.send(f'```py\n{value}{ret}\n```') | ||
except: | ||
paginated_text = paginate(f"{value}{ret}") | ||
for page in paginated_text: | ||
if page == paginated_text[-1]: | ||
out = await ctx.send(f'```py\n{page}\n```') | ||
break | ||
await ctx.send(f'```py\n{page}\n```') | ||
|
||
if out: | ||
await ctx.message.add_reaction('\u2705') | ||
elif err: | ||
await ctx.message.add_reaction('\u2049') | ||
else: | ||
await ctx.message.add_reaction('\u2705') | ||
|
||
def setup(client): | ||
client.add_cog(EvalCommand(client)) |