-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
303 lines (239 loc) · 9.76 KB
/
bot.py
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import ast
import contextlib
import contextvars
import functools
import pathlib
import sys
import textwrap
import traceback
from io import StringIO
import discord
from discord.ext import commands
bot = commands.Bot('py ', description='Created by A\u200bva#4982', help_command=commands.MinimalHelpCommand())
truncate = functools.partial(textwrap.shorten, width=1000)
@contextlib.asynccontextmanager
async def aclosing(gen):
"""
Async version of `contextlib.closing`
"""
try:
yield gen
finally:
await gen.aclose()
class StdoutProxy:
def __init__(self):
self.var = contextvars.ContextVar('stdout')
self.original = sys.stdout
def write(self, *args, **kwargs):
out = self.var.get(self.original)
out.write(*args, **kwargs)
def __getattr__(self, item):
return getattr(self.var.get(self.original), item)
def set(self, val):
self.var.set(val)
def get(self):
return self.var.get().getvalue()
_stdout = StdoutProxy()
sys.stdout = _stdout
def cleaned_code(arg):
"""
Strip discord code blocks
"""
arg = arg.strip('`')
if arg.startswith('py'):
arg = arg[2:]
return arg.strip()
class ReturnException(Exception):
"""
Used to mock a return
"""
pass
class Eval(commands.Cog):
# I use ... as a sentinel value in the `eval` command. Having a yield here unconditionally makes the resulting
# function an async generator which makes it nicer to handle than using `if inspect.isasyncgenfunction`.
base_code = (
'async def func_():\n'
' yield ...'
)
# mimics the above behaviour, ... is a sentinel value
last_val = ...
def transform(self, statement: ast.stmt):
"""
Adds an implicit yield to the last expression in the function body if it's yield-able
"""
body = getattr(statement, 'body', statement)
# we only want to look at the last statement in the code
statement = body.pop()
if isinstance(statement, ast.Expr):
# if it's an expression turn the expression into a yield expression instead
statement.value = ast.Yield(value=statement.value)
elif isinstance(statement, ast.If):
# we have to transform the body of the if statement, so let's just use recursion
self.transform(statement)
# if the statement has elif or else branches we need to visit those too
if statement.orelse:
self.transform(statement.orelse)
elif isinstance(statement, ast.Try):
# transform try block
self.transform(statement)
# transform each except block
for handler in statement.handlers:
self.transform(handler)
# transform finally if it exists
if statement.finalbody:
self.transform(statement.finalbody)
# transform else if it exists
if statement.orelse:
self.transform(statement.orelse)
# add the (potentially) transformed statement back to the function body
body.append(statement)
def replace_returns(self, tree):
"""
Async generators cannot have `return`s inside them,
so this function replaces each `return` in the function body with a `raise` that raises a custom exception.
This effectively has the same effect without causing a SyntaxError
"""
for index, val in enumerate(tree): # need to keep track of index to be able to replace returns
# if the node in the tree has a body (e.g. if and try block) we want to recursively adjust that too.
if hasattr(val, 'body'):
self.replace_returns(val.body)
# if we see a return, we want to replace it
elif isinstance(val, ast.Return):
tree[index] = ast.Raise(
exc=ast.Call(
# `return abc` -> `raise ReturnException(abc)`
args=[val.value],
# basically just loads the name `ReturnException`
func=ast.Name(
id='ReturnException', ctx=ast.Load(), lineno=val.lineno, col_offset=val.col_offset
),
# these need to be provided though the values don't really matter
keywords=[],
lineno=val.lineno,
col_offset=val.col_offset,
),
# these also need to be provided though the values don't really matter
cause=None,
lineno=val.lineno,
col_offset=val.col_offset
)
def create_namespaces(self, ctx):
"""
Create the `globals` and `locals` namespaces for the `exec` function
"""
# utility stuff
globals_ = {
'_': self.last_val,
'ctx': ctx,
'bot': ctx.bot,
'guild': ctx.guild,
'server': ctx.guild,
'author': ctx.author,
'channel': ctx.channel,
'message': ctx.message
}
# imports and other global variables
globals_.update(globals())
# we fetch the compiled function from this later on, but don't need to put anything in it
locals_ = {}
return globals_, locals_
@commands.is_owner()
@commands.command(name='eval')
async def _eval(self, ctx, *, code: cleaned_code):
"""
Evaluate Python code
TODO write a better docstring here
"""
# create the AST tree to transform
base_tree = ast.parse(self.base_code)
# do .body[0] to get the actual `async def`, since everything is wrapped in an `ast.Module`
async_func = base_tree.body[0]
try:
# create the AST tree for the code we want to execute
code = ast.parse(code)
except SyntaxError:
# oops the user did something silly, we should let them know.
await ctx.message.add_reaction(':warning:572102707331203075')
embed = discord.Embed(colour=discord.Colour.red())
embed.add_field(
name='**Traceback**',
value=f'```{truncate(traceback.format_exc(limit=2))}```'
)
return await ctx.send(embed=embed)
# put all the executable code into the body of the base function after the `yield ...` line
async_func.body.extend(code.body)
# replace all `return`s with a custom raise, see docstring of function
self.replace_returns(async_func.body)
self.transform(async_func)
# we need to call this or it's all fucked, don't ask me why
ast.fix_missing_locations(base_tree)
# turn the AST into a code object
code = compile(base_tree, '<eval>', 'exec')
globals_, locals_ = self.create_namespaces(ctx)
exec(code, globals_, locals_)
# get the
func = locals_['func_']
# we capture the `print`s in the compiled code into a `StringIO` object so we can observe the `stdout` output
_stdout.set(StringIO())
# if a `None` was yielded/returned we want to acknowledge it without showing it in the result message
add_reaction = False
# whether an exception occurred
exception = None
results = []
agen = func()
try:
# we need to close the async generator in case it raises or it's all bad for some reason I don't understand
# see https://www.python.org/dev/peps/pep-0533/
async with aclosing(agen):
# explicit `yield`s in the body or the last return value
async for result in agen:
# this is the sentinel we want to ignore
if result is ...:
continue
# we don't wanna show None in the output but still wanna know it's there
if result is None:
add_reaction = True
continue
# otherwise show the result
results.append(repr(result))
# update the _ magic var like in the REPL
self.last_val = result
except ReturnException as e:
# see the thing about no `return`s in async generators
results.extend(repr(arg) for arg in e.args)
except Exception:
# if we got a legitimate exception we wanna show that
exception = traceback.format_exc(limit=2)
if add_reaction:
await ctx.message.add_reaction(':check:572005045407842314')
embed = discord.Embed()
if results:
plural = len(results) > 1
embed.add_field(
name=f'**Result{"s" * plural}:**',
value='```py\n' + truncate(textwrap.indent('\n'.join(results), ' \N{BULLET OPERATOR} ')) + '```'
)
stdout = _stdout.get()
if stdout:
embed.add_field(
name='**stdout**',
value=f'```\n{truncate(stdout)}```'
)
if exception:
await ctx.message.add_reaction(':warning:572102707331203075')
embed.colour = discord.Colour.red()
embed.add_field(
name='**Traceback**',
value=f'```{truncate(exception)}```'
)
else:
embed.colour = discord.Colour.green()
# if we didn't have anything then don't just send an empty embed
if embed.fields:
await ctx.send(embed=embed)
bot.add_cog(Eval())
@bot.command()
async def info(ctx):
await ctx.send(ctx.bot.description)
if __name__ == '__main__':
bot.run(pathlib.Path('token.txt').read_text())