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
|
import re
from typing import List, Dict, Pattern
import discord
REPLACEMENTS: Dict[Pattern, str] = {
re.compile(r'<@!?(?P<id>[0-9]+)>'): '(guild.get_member({id}) if guild is not None else client.get_user({id}))',
re.compile(r'<#(?P<id>[0-9]+)>'): '(discord.utils.get(all_channels, id={id}))',
re.compile(r'<@&(?P<id>[0-9]+)>'): '(discord.utils.get(all_roles, id={id}))',
# Maybe later emoji support
}
async def handle_eval(message: discord.Message, client: discord.Client):
content: str = message.content
command_start: int = content.find(' ')
channel: discord.TextChannel = message.channel
author: discord.Member = message.author
all_channels: List[discord.Guild] = []
all_roles: List[discord.Role] = []
for guild in client.guilds:
guild: discord.Guild = guild # for type hints
all_channels += guild.channels
all_roles += guild.roles
variables = {
'message': message,
'author': author,
'channel': channel,
'all_channels': all_channels,
'all_roles': all_roles,
'client': client,
'discord': discord,
}
if channel.guild is not None:
variables['guild'] = channel.guild
lines: List[str] = content[command_start:].strip().split('\n')
lines[-1] = 'return ' + lines[-1]
block: str = '\n'.join(' ' + line for line in lines)
code = f"async def code({', '.join(variables.keys())}):\n" \
f"{block}"
for regex, replacement in REPLACEMENTS.items():
code = re.sub(regex, lambda match: replacement.format(**match.groupdict()), code)
_globals, _locals = {}, {}
exec(code, _globals, _locals)
result = {**_globals, **_locals}
result = await result["code"](**variables)
return await channel.send("Evaluation success: ```tr\n%r\n```" % result)
|