-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotexec.py
121 lines (104 loc) · 3.57 KB
/
notexec.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
# -*- coding: utf-8 -*-
# Module author: @DneZyeK
import itertools
import logging
import sys
import traceback
import types
import telethon
from meval import meval
from .. import loader, utils
logger = logging.getLogger(__name__)
@loader.tds
class ExecutorMod(loader.Module):
"""Stores global notes (aka snips)"""
strings = {
"name": "Notexec",
"what_note": "<b>What notexec should be executed?</b>",
"no_note": "<b>Notexec not found</b>",
"execute_fail": ("<b>Failed to execute expression:</b>\n<code>{}</code>"),
}
async def notexeccmd(self, message):
"""Gets the note specified"""
args = utils.get_args(message)
if not args:
await utils.answer(message, self.strings("what_note", message))
return
asset_id = self._db.get("friendly-telegram.modules.notes", "notes", {}).get(
args[0], None
)
logger.debug(asset_id)
if asset_id is not None:
asset = await self._db.fetch_asset(asset_id)
else:
asset_id = self._db.get("friendly-telegram.modules.notes", "notes", {}).get(
args[0].lower(), None
)
asset = (
await self._db.fetch_asset(asset_id) if asset_id is not None else None
)
if asset is None:
await utils.answer(message, self.strings("no_note", message))
return
cmd = await self._db.fetch_asset(asset_id)
try:
await meval(cmd.raw_text, globals(), **await self.getattrs(message))
except Exception:
exc = sys.exc_info()
exc = "".join(
traceback.format_exception(
exc[0], exc[1], exc[2].tb_next.tb_next.tb_next
)
)
await utils.answer(
message,
self.strings("execute_fail", message).format(utils.escape_html(exc)),
)
return
async def client_ready(self, client, db):
self.client = client
self.db = db
self._db = db
async def getattrs(self, message):
return {
"message": message,
"client": self.client,
"self": self,
"db": self.db,
"reply": await message.get_reply_message(),
"event": message,
"chat": message.to_id,
**self.get_types(),
**self.get_functions(),
}
def get_types(self):
return self.get_sub(telethon.tl.types)
def get_functions(self):
return self.get_sub(telethon.tl.functions)
def get_sub(self, it, _depth=1):
"""Get all callable capitalised objects in an object recursively, ignoring _*"""
return {
**dict(
filter(
lambda x: x[0][0] != "_"
and x[0][0].upper() == x[0][0]
and callable(x[1]),
it.__dict__.items(),
)
),
**dict(
itertools.chain.from_iterable(
[
self.get_sub(y[1], _depth + 1).items()
for y in filter(
lambda x: x[0][0] != "_"
and isinstance(x[1], types.ModuleType)
and x[1] != it
and x[1].__package__.rsplit(".", _depth)[0]
== "telethon.tl",
it.__dict__.items(),
)
]
)
),
}