forked from ChillBot-discord/Discord-Clusters
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.py
More file actions
82 lines (68 loc) · 2.29 KB
/
Copy patheval.py
File metadata and controls
82 lines (68 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import asyncio
import io
import json
import textwrap
import traceback
from contextlib import redirect_stdout
from discord.ext import commands
class Eval(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command()
@commands.is_owner()
async def evall(self, ctx, *, body: str):
self.bot.eval_wait = True
try:
await self.bot.websocket.send(json.dumps({'command': 'eval', 'content': body}).encode('utf-8'))
msgs = []
while True:
try:
msg = await asyncio.wait_for(self.bot.responses.get(), timeout=3)
except asyncio.TimeoutError:
break
msgs.append(f'{msg["author"]}: {msg["response"]}')
await ctx.send(' '.join(f'```py\n{m}\n```' for m in msgs))
finally:
self.bot.eval_wait = False
@commands.command(hidden=True, name='eval')
@commands.is_owner()
async def _eval(self, ctx, *, body: str):
"""Evaluates a code"""
env = {
'bot': self.bot,
'ctx': ctx,
'channel': ctx.channel,
'author': ctx.author,
'guild': ctx.guild,
'message': ctx.message,
'_': self.bot._last_result
}
env.update(globals())
body = self.bot.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 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()
try:
await ctx.message.add_reaction('\u2705')
except:
pass
if ret is None:
if value:
await ctx.send(f'```py\n{value}\n```')
else:
self.bot._last_result = ret
await ctx.send(f'```py\n{value}{ret}\n```')
def setup(bot):
bot.add_cog(Eval(bot))