-
Notifications
You must be signed in to change notification settings - Fork 0
/
Misc.py
65 lines (51 loc) · 1.68 KB
/
Misc.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
import discord
import class_descriptions
import newfile
import asyncio
import ast
import sqlite3
import importlib
import typing
import os
import random
from discord.ext import commands
def insert_returns(body):
# insert return stmt if the last expression is a expression statement
if isinstance(body[-1], ast.Expr):
body[-1] = ast.Return(body[-1].value)
ast.fix_missing_locations(body[-1])
# for if statements, we insert returns into the body and the orelse
if isinstance(body[-1], ast.If):
insert_returns(body[-1].body)
insert_returns(body[-1].orelse)
# for with blocks, again we insert returns into the body
if isinstance(body[-1], ast.With):
insert_returns(body[-1].body)
class Misc(commands.Cog):
def __init__(self, bot):
self.bot = bot
@commands.command
async def ping(self, ctx):
await ctx.send(f"Pong! {self.bot.latency}")
@commands.command
async def eval(self, ctx, *, cmd):
fn_name = "_eval_expr"
cmd = cmd.strip("` ")
cmd = "\n".join(f" {i}" for i in cmd.splitlines())
body = f"async def {fn_name}():\n{cmd}"
parsed = ast.parse(body)
body = parsed.body[0].body
insert_returns(body)
env = {
'bot': ctx.bot,
'discord': discord,
'commands': commands,
'ctx': ctx,
'importlib': importlib
}
exec(compile(parsed, filename="<ast>", mode="exec"), # pylint: disable=exec-used
env)
result = (await eval(f"{fn_name}()", env)) # pylint: disable=eval-used
await ctx.send(f'```python\n{result}```')
def setup(bot):
bot.add_cog(Misc(bot))