- Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpython.py
More file actions
Latest commit
100 lines (84 loc) · 4.6 KB
/
Copy pathpython.py
File metadata and controls
100 lines (84 loc) · 4.6 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Friendly Telegram (telegram userbot)
# Copyright (C) 2018-2019 The Authors
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
importlogging
importtraceback
importsys
importitertools
importtypes
frommevalimportmeval
importtelethon
from .. importloader, utils
logger=logging.getLogger(__name__)
@loader.tds
classPythonMod(loader.Module):
"""Python stuff"""
strings= {"name": "Python",
"evaluated": "<b>Выполненное выражение:</b>\n<code>{}</code>\n<b>Возвращено:</b>\n<code>{}</code>",
"evaluate_fail": ("<b>(eval)Не удалось выполнить выражение:</b>\n<code>{}</code>"
"\n\n<b>Ошибка:</b>\n<code>{}</code>"),
"execute_fail": ("<b>(exec)Не удалось выполнить выражение:</b>\n<code>{}</code>"
"\n\n<b>Ошибка:</b>\n<code>{}</code>")}
asyncdefclient_ready(self, client, db):
self.client=client
self.db=db
@loader.owner
asyncdefevalcmd(self, message):
""".eval <expression>
Evaluates python code"""
phone=message.client.phone
ret=self.strings("evaluated", message)
try:
it=awaitmeval(utils.get_args_raw(message), globals(), **awaitself.getattrs(message))
exceptException:
exc=sys.exc_info()
exc="".join(traceback.format_exception(exc[0], exc[1], exc[2].tb_next.tb_next.tb_next))
exc=exc.replace(phone, "❚"*len(phone))
awaitutils.answer(message, self.strings("evaluate_fail", message)
.format(utils.escape_html(utils.get_args_raw(message)), utils.escape_html(exc)))
return
ret=ret.format(utils.escape_html(utils.get_args_raw(message)), utils.escape_html(it))
ret=ret.replace(str(phone), "❚"*len(str(phone)))
awaitutils.answer(message, ret)
@loader.owner
asyncdefexeccmd(self, message):
""".exec <expression>
Executes python code"""
phone=message.client.phone
try:
awaitmeval(utils.get_args_raw(message), globals(), **awaitself.getattrs(message))
exceptException:
exc=sys.exc_info()
exc="".join(traceback.format_exception(exc[0], exc[1], exc[2].tb_next.tb_next.tb_next))
exc=exc.replace(str(phone), "❚"*len(str(phone)))
awaitutils.answer(message, self.strings("execute_fail", message)
.format(utils.escape_html(utils.get_args_raw(message)), utils.escape_html(exc)))
return
asyncdefgetattrs(self, message):
return {"message": message, "client": self.client, "self": self, "db": self.db,
"reply": awaitmessage.get_reply_message(), **self.get_types(), **self.get_functions(),
"event": message, "chat": message.to_id}
defget_types(self):
returnself.get_sub(telethon.tl.types)
defget_functions(self):
returnself.get_sub(telethon.tl.functions)
defget_sub(self, it, _depth=1):
"""Get all callable capitalised objects in an object recursively, ignoring _*"""
return {**dict(filter(lambdax: x[0][0] !="_"andx[0][0].upper() ==x[0][0] andcallable(x[1]),
it.__dict__.items())),
**dict(itertools.chain.from_iterable([self.get_sub(y[1], _depth+1).items() foryin
filter(lambdax: x[0][0] !="_"
andisinstance(x[1], types.ModuleType)
andx[1] !=it
andx[1].__package__.rsplit(".", _depth)[0]
=="telethon.tl",
it.__dict__.items())]))}