This repository was archived by the owner on Oct 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain.py
More file actions
453 lines (413 loc) · 18.5 KB
/
main.py
File metadata and controls
453 lines (413 loc) · 18.5 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
import os
os.system("sudo service redis-server start")
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
import json
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()
@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.chat_id
if event.is_private:
pr = utilities.prGreen
else:
pr = utilities.prPurple
if message.raw_text and not message.via_bot_id:
stri = (
str(from_id)
+ ": "
+ prefix
+ " text message : "
+ message.raw_text
+ " to "
+ str(to_id)
)
pr(stri)
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):
try:
message = event.message
chat_id = event.chat_id
from_id = event.sender_id
plugins = utilities.plugins
mutedUsers = getMutedUser(chat_id, from_id)
if mutedUsers:
remMuteUser(chat_id, from_id)
if message.raw_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 utilities.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:
try:
await (return_value)
except Exception as e:
print("step :" + str(e))
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.raw_text is not None and message.raw_text != "":
if "flood" not in utilities.config:
utilities.config["flood"] = True
utilities.save_config()
if utilities.config["flood"]:
pv = utilities.red.get("flood-" + str(message.sender_id)) or 0
# print("flood-" + str(message.sender_id), pv)
if pv == 0:
utilities.flood[message.sender_id] = True
utilities.red.set(
"flood-" + str(message.sender_id), (int(pv) + 1), ex=1
)
if (int(pv) + 1) == 5 and utilities.flood[message.sender_id]:
await message.reply("please do not flood...")
utilities.prRed(
str(message.sender_id) + " : is causing flood please stop..."
)
utilities.flood[message.sender_id] = False
return
elif (int(pv) + 1) >= 5:
return
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 utilities.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:
try:
await (return_value)
except Exception as e:
print("text main :" + str(e))
else:
continue
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, match, re.IGNORECASE)
if plugin["sudo"]:
if utilities.check_sudo(event.sender_id):
return_values = await plugin["run"](
event, matches[0], chat_id, 0
)
for return_value in return_values:
try:
await (return_value)
except Exception as e:
print("media :" + str(e))
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))
@utilities.client.on(events.InlineQuery)
async def my_event_handler(event):
builder = event.builder
try:
plugins = utilities.plugins
for plugin in plugins:
if "inlineQuery" not in plugin:
continue
for pattern in plugin["inlineData"]:
if re.search(pattern, str(event.text), re.IGNORECASE | re.MULTILINE):
matches = re.findall(
pattern,
str(event.text),
re.IGNORECASE | re.MULTILINE | re.DOTALL,
)
if plugin["sudo"]:
if utilities.check_sudo(event.sender_id):
return_values = await plugin["inlineQuery"](
event,
matches[0],
event.chat_id,
0
if (event.sender_id not in utilities.user_steps)
else utilities.user_steps[event.sender_id]["step"],
crons=utilities.crons,
)
for return_value in return_values:
if return_value:
try:
await (return_value)
except Exception as e:
print("inline :" + str(e))
else:
await event.answer(
[
builder.article(
"for sudors only", text="for sudors only"
)
]
)
else:
return_values = await plugin["inlineQuery"](
event,
matches[0],
event.chat_id,
0
if (event.sender_id not in utilities.user_steps)
else utilities.user_steps[event.sender_id]["step"],
)
if return_values:
for return_value in return_values:
await (return_value)
except Exception as e:
print(str(e))
@utilities.client.on(events.CallbackQuery)
async def handler(event):
try:
plugins = utilities.plugins
for plugin in plugins:
if "callbackQuery" not in plugin:
continue
for pattern in plugin["callbackData"]:
if re.search(
pattern, str(event.data.decode()), re.IGNORECASE | re.MULTILINE
):
matches = re.findall(
pattern,
str(event.data.decode()),
re.IGNORECASE | re.MULTILINE | re.DOTALL,
)
if plugin["sudo"]:
if utilities.check_sudo(event.sender_id):
return_values = await plugin["callbackQuery"](
event,
matches[0],
event.chat_id,
0
if (event.sender_id not in utilities.user_steps)
else utilities.user_steps[event.sender_id]["step"],
crons=utilities.crons,
)
for return_value in return_values:
if return_value:
try:
await (return_value)
except Exception as e:
print("callback :" + str(e))
else:
await event.answer("Sudors only!")
else:
return_values = await plugin["callbackQuery"](
event,
matches[0],
event.chat_id,
0
if (event.sender_id not in utilities.user_steps)
else utilities.user_steps[event.sender_id]["step"],
)
if return_values:
for return_value in return_values:
await (return_value)
except Exception as e:
print(str(e))
async def clock():
while True:
for _data in utilities.red.lrange("crons", 0, -1):
data = json.loads(_data)
if datetime.datetime.fromisoformat(data["time"]) < datetime.datetime.now():
for plugin in utilities.plugins:
if "cron" in plugin and plugin["name"] == data["name"]:
return_values = await plugin["cron"](data)
for return_value in return_values:
if return_value:
try:
await (return_value)
except Exception as e:
print("clock :" + str(e))
utilities.red.lrem("crons", 0, _data)
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 and plugin["name"] == data["name"]:
return_values = await plugin["cron"](data)
for return_value in return_values:
if return_value:
try:
await (return_value)
except Exception as e:
print("clock local :" + str(e))
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()