forked from micodev/botShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
298 lines (269 loc) · 11.3 KB
/
main.py
File metadata and controls
298 lines (269 loc) · 11.3 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
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
import os
from telethon import TelegramClient, events, Button, extensions, functions, types
from os.path import dirname, realpath, join
import re
import asyncio
import datetime
from utilities import utilities
loop = asyncio.get_event_loop()
utilities.client = None
def sort_key(p):
return p["name"]
def run_client():
utilities.config = utilities.get_config()
config = utilities.config
utilities.client = TelegramClient(
"sessions_bot", config["api_id"], config["api_hash"], loop=loop
)
utilities.client.start()
utilities.load_plugins()
utilities.plugins.sort(key=sort_key)
utilities.public_plugins.sort(key=sort_key)
run_client()
from Db.mute_sql import getMutedUser, remMuteUser
from Db.dev_sql import getDevsUsers
for dev in getDevsUsers():
utilities.devs.append(int("%.0f" % dev.user_id))
async def saveBotId():
me = await utilities.client.get_me()
utilities.prLightGray("name : " + me.first_name)
if me.username:
utilities.prYellow("username : https://t.me/" + me.username)
if me.bot:
utilities.prGreen("botType : API")
else:
utilities.prGreen("botType : CLI")
utilities.prBlack("---------------------------")
utilities.config["bot_id"] = (me).id
utilities.config["isbot"] = (me).bot
utilities.save_config()
def check_sudo(chat_id):
if chat_id in utilities.config["sudo_members"]:
return True
if chat_id in utilities.devs:
return True
return False
@utilities.client.on(events.ChatAction)
async def my_event_handler(event):
try:
if event.user_joined or event.user_added:
from_user = event.added_by
target_user = event.user
plugins = utilities.plugins
for plugin in plugins:
if "added" not in plugin:
continue
if "bot" in plugin and utilities.config["isbot"] != plugin["bot"]:
if plugin["bot"]:
await event.reply("for bot-api only")
else:
await event.reply("for bot-cli only")
return
# if plugin["sudo"]:
# if check_sudo(event.sender_id):
# return_values = await plugin["added"](
# event,
# event.chat_id,
# 0
# if (target_user.id in utilities.user_steps)
# else utilities.user_steps[target_user.id]["step"],
# crons=utilities.crons,
# )
# for return_value in return_values:
# if return_value:
# await (return_value)
# else:
# await event.reply("for sudores")
# else:
return_values = await plugin["added"](
event,
event.chat_id,
0
if (target_user.id not in utilities.user_steps)
else utilities.user_steps[target_user.id]["step"],
)
if return_values:
for return_value in return_values:
await (return_value)
except Exception as e:
print("chat_handler : %s" % (e))
@utilities.client.on(events.NewMessage)
async def command_interface(event):
try:
message = event.message
prefix = "send"
if message.is_reply:
prefix = "reply"
if message.out:
return
from_id = message.from_id
to_id = message.to_id
if event.is_private:
pr = utilities.prGreen
else:
pr = utilities.prPurple
if message.text and not message.via_bot_id:
pr(
str(from_id)
+ ": "
+ prefix
+ " text message : "
+ message.text
+ " to "
+ str(to_id)
)
elif message.media and not message.via_bot_id:
pr(str(from_id) + ": " + prefix + " media message to " + str(to_id))
elif message.via_bot_id:
pr(str(from_id) + ": " + prefix + " inline message to " + str(to_id))
else:
utilities.prRed(
str(from_id) + ": " + prefix + " unknown message to " + str(to_id)
)
except Exception as e:
print(str(e))
@utilities.client.on(events.MessageEdited)
@utilities.client.on(events.NewMessage)
async def my_event_handler(event):
plugins = utilities.plugins
try:
message = event.message
chat_id = event.chat_id
from_id = event.sender_id
mutedUsers = getMutedUser(chat_id, from_id)
if mutedUsers:
remMuteUser(chat_id, from_id)
if message.text:
matches = re.findall("^[#/!](cancel)$", event.raw_text, re.IGNORECASE)
if len(matches) > 0 and matches[0] == "cancel":
if from_id in utilities.user_steps:
del utilities.user_steps[from_id]
return await message.reply("Canceling successfully !")
if from_id in utilities.user_steps:
for plugin in plugins:
if plugin["name"] == utilities.user_steps[from_id]["name"]:
for pattern in plugin["patterns"]:
if re.search(
pattern, event.raw_text, re.IGNORECASE | re.MULTILINE
):
matches = re.findall(
pattern, event.raw_text, re.IGNORECASE | re.DOTALL
)
break
else:
matches = ["xxxxxxxxxx"]
if plugin["sudo"]:
if check_sudo(from_id):
return_values = await plugin["run"](
message,
matches[0],
chat_id,
utilities.user_steps[from_id]["step"],
)
for return_value in return_values:
if return_value:
await (return_value)
else:
return
else:
return_values = await plugin["run"](
message,
matches[0],
chat_id,
utilities.user_steps[from_id]["step"],
)
if return_values:
for return_value in return_values:
await (return_value)
break
return
elif message.text is not None:
for plugin in plugins:
for pattern in plugin["patterns"]:
if re.search(pattern, event.raw_text, re.IGNORECASE | re.MULTILINE):
if (
"bot" in plugin
and utilities.config["isbot"] != plugin["bot"]
):
if plugin["bot"]:
await event.reply("for bot-api only")
else:
await event.reply("for bot-cli only")
return
matches = re.findall(
pattern,
event.raw_text,
re.IGNORECASE | re.MULTILINE | re.DOTALL,
)
if plugin["sudo"]:
if check_sudo(event.sender_id):
return_values = await plugin["run"](
event, matches[0], chat_id, 0, crons=utilities.crons
)
for return_value in return_values:
if return_value:
await (return_value)
else:
return
else:
return_values = await plugin["run"](
event, matches[0], chat_id, 0, crons=utilities.crons
)
if return_values:
for return_value in return_values:
await (return_value)
elif message.media is not None or message.file is not None:
match = ""
if message.photo:
match = "__photo__"
if message.gif:
match = "__gif__"
for plugin in plugins:
for pattern in plugin["patterns"]:
if re.search(pattern, match, re.IGNORECASE | re.MULTILINE):
matches = re.findall(pattern, "__photo__", re.IGNORECASE)
if plugin["sudo"]:
if check_sudo(event.sender_id):
return_values = await plugin["run"](
event, matches[0], chat_id, 0
)
for return_value in return_values:
if return_value:
await (return_value)
else:
return
else:
return_values = await plugin["run"](
event, matches[0], chat_id, 0
)
if return_values:
for return_value in return_values:
await (return_value)
except Exception as e:
print(str(e))
await event.reply("Error : " + str(e))
async def clock():
while True:
if len(utilities.crons) != 0:
for data in utilities.crons:
if data["time"] < datetime.datetime.now():
for plugin in utilities.plugins:
if "cron" in plugin:
return_values = await plugin["cron"](data)
for return_value in return_values:
if return_value:
await (return_value)
utilities.crons.remove(data)
await asyncio.sleep(1)
if "updateChat" in utilities.config:
loop.create_task(
utilities.client.send_message(
utilities.config["updateChat"], "The bot restart successfully."
)
)
del utilities.config["updateChat"]
utilities.save_config()
loop.create_task(clock())
loop.create_task(saveBotId())
utilities.prCyan("Started Receveving Messages ...")
utilities.client.run_until_disconnected()