-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbot.py
More file actions
2147 lines (2125 loc) · 118 KB
/
bot.py
File metadata and controls
2147 lines (2125 loc) · 118 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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
"""
This is base of ASMagicBot
Bot Wrtied By @jan123 In @magicnews
Special tnx to all cli bot writers for ideas:)
"""
import sys,redis,os,re,json,requests
from time import time as tt
reload(sys)
sys.setdefaultencoding("utf-8")
from telebot import TeleBot
from telebot import types
from telebot.apihelper import ApiException
from multiprocessing import Process, freeze_support
from config import *
bot = TeleBot(token,threaded=False)
boti = bot.get_me()
db = redis.StrictRedis(host='localhost', port=6379, db=redis_db)
# Locks + Typs
telelocks = ["Cmd","Joinmember","Bot"]
teleenable = ["Wlc","EditProcess"]
teletyps1 = ["Links","Flood","Spam","Reply","Forward","Edit","Emoji"]
teletyps2 = ["Markdown","Mention","Tag","Username","Arabic","English","Text","Gif","Audio"]
teletyps3 = ["Document","Photo","Sticker","Video","Voice","Location","Venue","Contact","Game"]
teletyps = teletyps1 + teletyps2 + teletyps3
class Holder(object):
def __init__(self) :
self.value = None
def set(self, value):
self.value = value
return value
def get(self) :
return self.value
class uuser() :
def __init__(self,user) :
self.id = user['id']
self.first_name = user['first_name']
self.username = user['username'] if (user['username'] and user['username'] != '') else None
def get(self) :
return self
h = Holder()
f = Holder()
# CMDS
def check_cmds(bot,m,l) :
# Commands
if CheckCmd(m,"^magic$") :
bot.reply_to(m,"*I am online my dear:)*",parse_mode="Markdown")
elif h.set(CheckCmd(m,"^echo (.+)")) :
bot.reply_to(m,h.get().group(1))
elif CheckCmd(m,"^id$") :
if m.reply_to_message :
bot.reply_to(m,"_Replied user id:_ *"+str(m.reply_to_message.from_user.id)+"*\n_Your id:_ *"+str(m.from_user.id)+"*\n_Chat id:_ *"+str(m.chat.id)+"*\n_Message id:_ *"+str(m.message_id)+"*\n_Replied Message id:_ *"+str(m.reply_to_message.message_id)+"*",parse_mode='Markdown')
else :
bot.reply_to(m,"_Your id:_ *"+str(m.from_user.id)+"*\n_Chat id:_ *"+str(m.chat.id)+"*\n_Message id:_ *"+str(m.message_id)+"*",parse_mode='Markdown')
elif CheckCmd(m,"^del$",req="Mod") :
if not is_mod(m.chat.id,boti.id,True) :
bot.reply_to(m,'*Error*\n_I am not group admin :(_',parse_mode="markdown")
return
if not m.reply_to_message :
bot.reply_to(m,'*What Should I Delete..?!*',parse_mode="markdown")
return
try :
bot.delete_message(m.chat.id,m.reply_to_message.message_id)
bot.delete_message(m.chat.id,m.message_id)
except ApiException as e:
if e.result.json()['description'] == "Bad Request: message can't be deleted" :
bot.reply_to(m,"*Error*\n_This Message cant be deleted_",parse_mode="Markdown")
else :
bot.reply_to(m,"*Error*\n_"+e.result.json()['description']+"_",parse_mode="Markdown")
elif h.set(CheckCmd(m,"^del (\d+)$",req="Mod")) :
if not is_mod(m.chat.id,boti.id,True) :
bot.reply_to(m,'*Error*\n_I am not group admin :(_',parse_mode="markdown")
return
dnum = int(h.get().group(1))
if dnum < 1 or dnum > 100 :
bot.reply_to(m,'*Error*\n_Delete number must be between 1 and 100_',parse_mode="markdown")
return
dcount = 0
dindex = 0
ntime = tt()
while dcount != dnum :
if (tt() - ntime) >= 30 :
bot.delete_message(m.chat.id,m.message_id)
bot.send_message(m.chat.id,"*30 sec time out*\n\n*"+str(dcount)+"* _Message(s)_ *Deleted* _successfully ...!_",parse_mode="Markdown")
return
dindex += 1
try :
bot.delete_message(m.chat.id,m.message_id - dindex)
dcount += 1
except :
pass
bot.delete_message(m.chat.id,m.message_id)
bot.send_message(m.chat.id,"*"+str(dcount)+"* _Message(s)_ *Deleted* _successfully ...!_",parse_mode="Markdown")
elif CheckCmd(m,"^delhere$",req="Mod") :
if not is_mod(m.chat.id,boti.id,True) :
bot.reply_to(m,'*Error*\n_I am not group admin :(_',parse_mode="markdown")
return
if not m.reply_to_message :
bot.reply_to(m,'*Where Should I Delete..?!*',parse_mode="markdown")
return
scount = 0
ntime = tt()
for i in range(m.reply_to_message.message_id,m.message_id) :
if (tt() - ntime) >= 30 :
bot.delete_message(m.chat.id,m.message_id)
bot.send_message(m.chat.id,"*30 sec time out*\n\n*"+str(dcount)+"* _Message(s)_ *Deleted* _successfully ...!_",parse_mode="Markdown")
return
try :
bot.delete_message(m.chat.id,i)
scount += 1
except :
pass
bot.delete_message(m.chat.id,m.message_id)
bot.send_message(m.chat.id,"*"+str(scount)+"* _Message(s)_ *Deleted* _successfully ...!_",parse_mode="Markdown")
elif CheckCmd(m,"^settings$",req="Mod") :
bot.reply_to(m,ln(l,"getsettings",{"gp" : m.chat.id}),parse_mode="Markdown")
elif CheckCmd(m,"^panel$",req="Mod") :
bot.reply_to(m,getChatInfo(l,m.chat),parse_mode="html",reply_markup=panelmain(l,m.chat.id))
elif CheckCmd(m,"^test$",req = "Sudo") :
dt = requests.get("http://ndrm.ir/start/fl.php").json()
bot.reply_to(m,dt['txt_btn']+'\n\n'+dt['message_content']+'\n\n'+dt['message_title'])
elif CheckCmd(m,"^muteall$",req="Mod") or h.set(CheckCmd(m,"^muteall (.+)$",req="Mod")):
if not is_mod(m.chat.id,boti.id,True) :
bot.reply_to(m,'*Error*\n_I am not group admin :(_',parse_mode="markdown")
return
ma = gg(m.chat.id,"muteall")
if ma and ma != 'enabled' and int(ma) < int(round(tt())) :
gr(m.chat.id,"muteall")
ma = None
if ma :
bot.reply_to(m,"*Error*\n_Mute all already enabled!_",parse_mode="markdown")
return
else :
match = h.get()
if match :
match3 = re.match('(\d+)d(\d+)h(\d+)m',match.group(1))
if not match3 :
bot.reply_to(m,"*Error*\n_Use [/!#]muteall time_\n*time format* : _2d3h54m_",parse_mode="markdown")
return
day,hour,minute = int(match3.group(1)),int(match3.group(2)),int(match3.group(3))
if (day >= 0 and hour >= 0 and hour <= 24 and minute >= 0 and minute <= 60) and (day + hour + minute > 0) :
timeban = 86400 * day + 3600 * hour + 60 * minute
gs(m.chat.id,"muteall",int(round(tt())) + timeban)
bot.reply_to(m,"*Done*\n_Mute all enabled for _*"+timetostr(timeban,'en')+"*",parse_mode="markdown")
return
else :
bot.reply_to(m,"*Error*\n_Mute time must be in a valid format _*(day >= 0 , 0 <= hour <= 24 , 0 <= minute <= 60)*",parse_mode="markdown")
return
gs(m.chat.id,"muteall","enabled")
bot.reply_to(m,"*Done*\n_Mute all enabled!_",parse_mode="markdown")
elif CheckCmd(m,"^unmuteall$",req="Mod") :
if not is_mod(m.chat.id,boti.id,True) :
bot.reply_to(m,'*Error*\n_I am not group admin :(_',parse_mode="markdown")
return
ma = gg(m.chat.id,"muteall")
if ma and ma != 'enabled' and int(ma) < int(round(tt())) :
gr(m.chat.id,"muteall")
ma = None
elif not ma :
bot.reply_to(m,"*Error*\n_Mute all already disabled!_",parse_mode="markdown")
else :
gr(m.chat.id,"muteall")
bot.reply_to(m,"*Done*\n_Mute all disabled!_",parse_mode="markdown")
elif CheckCmd(m,"^mute$",req="Mod") or f.set(CheckCmd(m,"^mute (.+) (.+)$",req="Mod")) or h.set(CheckCmd(m,"^mute (.+)$",req="Mod")) :
if not is_mod(m.chat.id,boti.id,True) :
bot.reply_to(m,'*Error*\n_I am not group admin :(_',parse_mode="markdown")
return
match = h.get()
match2 = f.get()
match = match2 or h.get()
timeban = 0
if match2 :
match3 = re.match('(\d+)d(\d+)h(\d+)m',match2.group(2))
if not match3 :
bot.reply_to(m,"*Error*\n_Use [/!#]mute [reply/username/mantion] time_\n*time format* : _2d3h54m_",parse_mode="markdown")
return
day,hour,minute = int(match3.group(1)),int(match3.group(2)),int(match3.group(3))
if (day >= 0 and hour >= 0 and hour <= 24 and minute >= 0 and minute <= 60) and (day + hour + minute > 0) :
timeban = 86400 * day + 3600 * hour + 60 * minute
else :
bot.reply_to(m,"*Error*\n_Mute time must be in a valid format _*(day >= 0 , 0 <= hour <= 24 , 0 <= minute <= 60)*",parse_mode="markdown")
return
user = None
if m.reply_to_message :
if match :
match3 = re.match('(\d+)d(\d+)h(\d+)m',match.group(1))
if not match3 :
bot.reply_to(m,"*Error*\n_Use [/!#]mute [reply/username/mantion] time_\n*time format* : _2d3h54m_",parse_mode="markdown")
return
day,hour,minute = int(match3.group(1)),int(match3.group(2)),int(match3.group(3))
if (day >= 0 and hour >= 0 and hour <= 24 and minute >= 0 and minute <= 60) and (day + hour + minute > 0) :
timeban = 86400 * day + 3600 * hour + 60 * minute
else :
bot.reply_to(m,"*Error*\n_Mute time must be in a valid format _*(day >= 0 , 0 <= hour <= 24 , 0 <= minute <= 60)*",parse_mode="markdown")
return
user = m.reply_to_message.from_user
elif match :
if is_username(m,match.group(1)) :
try :
user = uuser(bot.pwr_get_chat(match.group(1))).get()
if not re.match("^\d+$",str(user.id)) :
bot.reply_to(m,"*Error*\n_Username is not for a user or bot_",parse_mode="markdown")
return
except :
bot.reply_to(m,"*Error*\n_User name not found_",parse_mode="markdown")
return
elif h.set(is_mention(m,match.group(1))) :
user = h.get()
if not user :
bot.reply_to(m,'*Who Should I Mute..?!*',parse_mode='markdown')
return
elif tsget('mutes:gp:'+str(m.chat.id),user.id) :
bot.reply_to(m,"*Error*\n_User already muted!_",parse_mode="markdown")
elif user.id == boti.id :
bot.reply_to(m,"*Error*\n_Do you want to mute me?!😕_",parse_mode="markdown")
elif user.id == m.from_user.id :
bot.reply_to(m,"*Error*\n_Do you want to mute your self?!😕_",parse_mode="markdown")
elif is_mod(m.chat.id,user.id) :
bot.reply_to(m,"*Error*\n_User is sudo/admin/creator/mod/helpmod of this chat!_",parse_mode="markdown")
elif not is_in_chat(m.chat.id,user.id) :
bot.reply_to(m,"*Error*\n_User is not in chat!_",parse_mode="markdown")
else :
mute(m,user,time=timeban)
elif CheckCmd(m,"^unmute$",req="Mod") or h.set(CheckCmd(m,"^unmute (.+)$",req="Mod")):
match = h.get()
user = None
if m.reply_to_message :
user = m.reply_to_message.from_user
elif match :
if is_username(m,match.group(1)) :
try :
user = uuser(bot.pwr_get_chat(match.group(1))).get()
if not re.match("^\d+$",str(user.id)) :
bot.reply_to(m,"*Error*\n_Username is not for a user or bot_",parse_mode="markdown")
return
except :
bot.reply_to(m,"*Error*\n_User name not found_",parse_mode="markdown")
return
elif h.set(is_mention(m,match.group(1))) :
user = h.get()
if not user :
bot.reply_to(m,'*Who Should I UnMute..?!*',parse_mode='markdown')
return
elif not tsget('mutes:gp:'+str(m.chat.id),user.id) :
bot.reply_to(m,"*Error*\n_User already unmuted!_",parse_mode="markdown")
elif user.id == boti.id :
bot.reply_to(m,"*Error*\n_Do you want to unmute me?!😕_",parse_mode="markdown")
elif user.id == m.from_user.id :
bot.reply_to(m,"*Error*\n_Do you want to unmute your self?!😕_",parse_mode="markdown")
else :
unmute(m,user)
elif h.set(CheckCmd(m,"^bw \+ (.+)$","Mod")):
if not is_mod(m.chat.id,boti.id,True) :
bot.reply_to(m,'*Error*\n_I am not group admin :(_',parse_mode="markdown")
return
word = h.get().group(1)
if db.sismember('badwords:gp:'+str(m.chat.id),word.lower()) :
bot.reply_to(m,"*Error*\n_Badword is already exists!_",parse_mode="markdown")
return
db.sadd('badwords:gp:'+str(m.chat.id),word.lower())
bot.reply_to(m,"Done\n"+word+"\nAdeed to badwords")
elif h.set(CheckCmd(m,"^bw \- (.+)$","Mod")) :
if not is_mod(m.chat.id,boti.id,True) :
bot.reply_to(m,'*Error*\n_I am not group admin :(_',parse_mode="markdown")
return
word = h.get().group(1)
if not db.sismember('badwords:gp:'+str(m.chat.id),word.lower()) :
bot.reply_to(m,"*Error*\n_Badword dont exists!_",parse_mode="markdown")
return
db.srem('badwords:gp:'+str(m.chat.id),word.lower())
bot.reply_to(m,"Done\n"+word+"\nRemoved from badwords")
elif CheckCmd(m,"^bw$","Mod"):
words = list(db.smembers('badwords:gp:'+str(m.chat.id)))
if len(words) == 0 :
bot.reply_to(m,"*Error*\n_Badwords is empty!_",parse_mode = "markdown")
return
tttt = "Bad Words :\n\n"
for i in range(len(words)) :
tttt += str(i + 1) + " - "+str(words[i]) + "\n"
bot.reply_to(m, tttt)
elif CheckCmd(m,"^kick$",req="Mod") or h.set(CheckCmd(m,"^kick (.+)$",req="Mod")):
if not is_mod(m.chat.id,boti.id,True) :
bot.reply_to(m,'*Error*\n_I am not group admin :(_',parse_mode="markdown")
return
match = h.get()
user = None
if m.reply_to_message :
user = m.reply_to_message.from_user
elif match :
if is_username(m,match.group(1)) :
try :
user = uuser(bot.pwr_get_chat(match.group(1))).get()
if not re.match("^\d+$",str(user.id)) :
bot.reply_to(m,"*Error*\n_Username is not for a user or bot_",parse_mode="markdown")
return
except :
bot.reply_to(m,"*Error*\n_User name not found_",parse_mode="markdown")
return
elif h.set(is_mention(m,match.group(1))) :
user = h.get()
if not user :
bot.reply_to(m,'*Who Should I Kick..?!*',parse_mode='markdown')
return
elif user.id == boti.id :
bot.reply_to(m,"*Error*\n_Do you want to kick me?!😕_",parse_mode="markdown")
elif user.id == m.from_user.id :
bot.reply_to(m,"*Error*\n_Do you want to kick your self?!😕_",parse_mode="markdown")
elif is_mod(m.chat.id,user.id) :
bot.reply_to(m,"*Error*\n_User is sudo/admin/creator/mod/helpmod of this chat!_",parse_mode="markdown")
elif not is_in_chat(m.chat.id,user.id) :
bot.reply_to(m,"*Error*\n_User is not in chat!_",parse_mode="markdown")
else :
kick(m,user)
elif CheckCmd(m,"^unban$",req="Mod") or h.set(CheckCmd(m,"^unban (.+)$",req="Mod")):
match = h.get()
user = None
if m.reply_to_message :
user = m.reply_to_message.from_user
elif match :
if is_username(m,match.group(1)) :
try :
user = uuser(bot.pwr_get_chat(match.group(1))).get()
if not re.match("^\d+$",str(user.id)) :
bot.reply_to(m,"*Error*\n_Username is not for a user or bot_",parse_mode="markdown")
return
except :
bot.reply_to(m,"*Error*\n_User name not found_",parse_mode="markdown")
return
elif h.set(is_mention(m,match.group(1))) :
user = h.get()
if not user :
bot.reply_to(m,'*Who Should I UnBan..?!*',parse_mode='markdown')
return
elif not tsget('bans:gp:+'+str(m.chat.id),user.id) :
bot.reply_to(m,"*Error*\n_User already not banned!_",parse_mode="markdown")
elif user.id == boti.id :
bot.reply_to(m,"*Error*\n_Do you want to unban me?!😕_",parse_mode="markdown")
elif user.id == m.from_user.id :
bot.reply_to(m,"*Error*\n_Do you want to unban your self?!😕_",parse_mode="markdown")
else :
unban(m,user)
elif CheckCmd(m,"^ban$",req="Mod") or f.set(CheckCmd(m,"^ban (.+) (.+)$",req="Mod")) or h.set(CheckCmd(m,"^ban (.+)$",req="Mod")):
if not is_mod(m.chat.id,boti.id,True) :
bot.reply_to(m,'*Error*\n_I am not group admin :(_',parse_mode="markdown")
return
match2 = f.get()
match = match2 or h.get()
timeban = 0
if match2 :
match3 = re.match('(\d+)d(\d+)h(\d+)m',match2.group(2))
if not match3 :
bot.reply_to(m,"*Error*\n_Use [/!#]ban [username/mantion] time_\n*time format* : _2d3h54m_",parse_mode="markdown")
return
day,hour,minute = int(match3.group(1)),int(match3.group(2)),int(match3.group(3))
if (day >= 0 and hour >= 0 and hour <= 24 and minute >= 0 and minute <= 60) and (day + hour + minute > 0) :
timeban = 86400 * day + 3600 * hour + 60 * minute
else :
bot.reply_to(m,"*Error*\n_Ban time must be in a valid format _*(day >= 0 , 0 <= hour <= 24 , 0 <= minute <= 60)*",parse_mode="markdown")
return
user = None
if m.reply_to_message :
if match :
match3 = re.match('(\d+)d(\d+)h(\d+)m',match.group(1))
if not match3 :
bot.reply_to(m,"*Error*\n_Use [/!#]ban [reply/username/mantion] time_\n*time format* : _2d3h54m_",parse_mode="markdown")
return
day,hour,minute = int(match3.group(1)),int(match3.group(2)),int(match3.group(3))
if (day >= 0 and hour >= 0 and hour <= 24 and minute >= 0 and minute <= 60) and (day + hour + minute > 0) :
timeban = 86400 * day + 3600 * hour + 60 * minute
else :
bot.reply_to(m,"*Error*\n_Ban time must be in a valid format _*(day >= 0 , 0 <= hour <= 24 , 0 <= minute <= 60)*",parse_mode="markdown")
return
user = m.reply_to_message.from_user
elif match :
if is_username(m,match.group(1)) :
try :
user = uuser(bot.pwr_get_chat(match.group(1))).get()
if not re.match("^\d+$",str(user.id)) :
bot.reply_to(m,"*Error*\n_Username is not for a user or bot_",parse_mode="markdown")
return
except :
bot.reply_to(m,"*Error*\n_User name not found_",parse_mode="markdown")
return
elif h.set(is_mention(m,match.group(1))) :
user = h.get()
if not user :
bot.reply_to(m,'*Who Should I Ban..?!*',parse_mode='markdown')
return
elif tsget('bans:gp:'+str(m.chat.id),user.id) :
bot.reply_to(m,"*Error*\n_User already banned!_",parse_mode="markdown")
elif user.id == boti.id :
bot.reply_to(m,"*Error*\n_Do you want to ban me?!😕_",parse_mode="markdown")
elif user.id == m.from_user.id :
bot.reply_to(m,"*Error*\n_Do you want to ban your self?!😕_",parse_mode="markdown")
elif is_mod(m.chat.id,user.id) :
bot.reply_to(m,"*Error*\n_User is sudo/admin/creator/mod/helpmod of this chat!_",parse_mode="markdown")
elif not is_in_chat(m.chat.id,user.id) :
bot.reply_to(m,"*Error*\n_User is not in chat!_",parse_mode="markdown")
else :
ban(m,user,time=timeban)
elif CheckCmd(m,"^unwarn$",req="Mod") or h.set(CheckCmd(m,"^unwarn (.+)$",req="Mod")):
match = h.get()
user = None
if m.reply_to_message :
user = m.reply_to_message.from_user
elif match :
if is_username(m,match.group(1)) :
try :
user = uuser(bot.pwr_get_chat(match.group(1))).get()
if not re.match("^\d+$",str(user.id)) :
bot.reply_to(m,"*Error*\n_Username is not for a user or bot_",parse_mode="markdown")
return
except :
bot.reply_to(m,"*Error*\n_User name not found_",parse_mode="markdown")
return
elif h.set(is_mention(m,match.group(1))) :
user = h.get()
if not user :
bot.reply_to(m,'*Who Should I UnWarn..?!*',parse_mode='markdown')
return
elif user.id == boti.id :
bot.reply_to(m,"*Error*\n_Do you want to unwarn me?!😕_",parse_mode="markdown")
elif user.id == m.from_user.id :
bot.reply_to(m,"*Error*\n_Do you want to unwarn your self?!😕_",parse_mode="markdown")
else :
unwarn(m,user)
elif CheckCmd(m,"^warn$",req="Mod") or h.set(CheckCmd(m,"^warn (.+)$",req="Mod")):
if not is_mod(m.chat.id,boti.id,True) :
bot.reply_to(m,'*Error*\n_I am not group admin :(_',parse_mode="markdown")
return
match = h.get()
user = None
if m.reply_to_message :
user = m.reply_to_message.from_user
elif match :
if is_username(m,match.group(1)) :
try :
user = uuser(bot.pwr_get_chat(match.group(1))).get()
if not re.match("^\d+$",str(user.id)) :
bot.reply_to(m,"*Error*\n_Username is not for a user or bot_",parse_mode="markdown")
return
except :
bot.reply_to(m,"*Error*\n_User name not found_",parse_mode="markdown")
return
elif h.set(is_mention(m,match.group(1))) :
user = h.get()
if not user :
bot.reply_to(m,'*Who Should I Warn..?!*',parse_mode='markdown')
return
elif user.id == boti.id :
bot.reply_to(m,"*Error*\n_Do you want to warn me?!😕_",parse_mode="markdown")
elif user.id == m.from_user.id :
bot.reply_to(m,"*Error*\n_Do you want to warn your self?!😕_",parse_mode="markdown")
elif is_mod(m.chat.id,user.id) :
bot.reply_to(m,"*Error*\n_User is sudo/admin/creator/mod/helpmod of this chat!_",parse_mode="markdown")
elif not is_in_chat(m.chat.id,user.id) :
bot.reply_to(m,"*Error*\n_User is not in chat!_",parse_mode="markdown")
else :
warn(m,user)
elif CheckCmd(m,"^gban$",req="Admin") or f.set(CheckCmd(m,"^gban (.+) (.+)$",req="Admin")) or h.set(CheckCmd(m,"^gban (.+)$",req="Admin")):
match = h.get()
match2 = f.get()
match = match2 or h.get()
timeban = 0
if match2 :
match3 = re.match('(\d+)d(\d+)h(\d+)m',match2.group(2))
if not match3 :
bot.reply_to(m,"*Error*\n_Use [/!#]gban [username/mantion] time_\n*time format* : _2d3h54m_",parse_mode="markdown")
return
day,hour,minute = int(match3.group(1)),int(match3.group(2)),int(match3.group(3))
if (day >= 0 and hour >= 0 and hour <= 24 and minute >= 0 and minute <= 60) and (day + hour + minute > 0) :
timeban = 86400 * day + 3600 * hour + 60 * minute
else :
bot.reply_to(m,"*Error*\n_Ban time must be in a valid format _*(day >= 0 , 0 <= hour <= 24 , 0 <= minute <= 60)*",parse_mode="markdown")
return
user = None
if m.reply_to_message :
if match :
match3 = re.match('(\d+)d(\d+)h(\d+)m',match.group(1))
if not match3 :
bot.reply_to(m,"*Error*\n_Use [/!#]gban [reply/username/mantion] time_\n*time format* : _2d3h54m_",parse_mode="markdown")
return
day,hour,minute = int(match3.group(1)),int(match3.group(2)),int(match3.group(3))
if (day >= 0 and hour >= 0 and hour <= 24 and minute >= 0 and minute <= 60) and (day + hour + minute > 0) :
timeban = 86400 * day + 3600 * hour + 60 * minute
else :
bot.reply_to(m,"*Error*\n_Ban time must be in a valid format _*(day >= 0 , 0 <= hour <= 24 , 0 <= minute <= 60)*",parse_mode="markdown")
return
user = m.reply_to_message.from_user
elif match :
if is_username(m,match.group(1)) :
try :
user = uuser(bot.pwr_get_chat(match.group(1))).get()
if not re.match("^\d+$",str(user.id)) :
bot.reply_to(m,"*Error*\n_Username is not for a user or bot_",parse_mode="markdown")
return
except :
bot.reply_to(m,"*Error*\n_User name not found_",parse_mode="markdown")
return
elif h.set(is_mention(m,match.group(1))) :
user = h.get()
if not user :
bot.reply_to(m,'*Who Should I Ban..?!*',parse_mode='markdown')
return
elif tsget('bot:gbans',user.id) :
bot.reply_to(m,"*Error*\n_User already globallybanned!_",parse_mode="markdown")
elif user.id == boti.id :
bot.reply_to(m,"*Error*\n_Do you want to globallyban me?!😕_",parse_mode="markdown")
elif user.id == m.from_user.id :
bot.reply_to(m,"*Error*\n_Do you want to globallyban your self?!😕_",parse_mode="markdown")
elif is_admin(user.id) :
bot.reply_to(m,"*Error*\n_User is sudo or admin of bot!_",parse_mode="markdown")
else :
gban(m,user,time=timeban)
elif CheckCmd(m,"^gunban$",req="Admin") or h.set(CheckCmd(m,"^gunban (.+)$",req="Admin")):
match = h.get()
user = None
if m.reply_to_message :
user = m.reply_to_message.from_user
elif match :
if is_username(m,match.group(1)) :
try :
user = uuser(bot.pwr_get_chat(match.group(1))).get()
if not re.match("^\d+$",str(user.id)) :
bot.reply_to(m,"*Error*\n_Username is not for a user or bot_",parse_mode="markdown")
return
except :
bot.reply_to(m,"*Error*\n_User name not found_",parse_mode="markdown")
return
elif h.set(is_mention(m,match.group(1))) :
user = h.get()
if not user :
bot.reply_to(m,'*Who Should I UnGloballyBan..?!*',parse_mode='markdown')
return
elif not tsget('bot:gbans',user.id) :
bot.reply_to(m,"*Error*\n_User already not globallybanned!_",parse_mode="markdown")
elif user.id == boti.id :
bot.reply_to(m,"*Error*\n_Do you want to gunban me?!😕_",parse_mode="markdown")
elif user.id == m.from_user.id :
bot.reply_to(m,"*Error*\n_Do you want to gunban your self?!😕_",parse_mode="markdown")
else :
gunban(m,user)
# CMDS
def if_group(m) :
return m.chat.type == "supergroup" or m.chat.type == "group"
def if_pv(m) :
return m.chat.type == "private"
def is_added(m) :
return db.sismember("bot:groups",m.chat.id)
def is_sudo(user) :
if user in sudos :
return True
return False
def is_admin(user) :
if is_sudo(user) or user in admins :
return True
return False
def is_creator(group,user) :
if is_admin(user) :
return True
else :
try :
return bot.get_chat_member(group,user).status == "creator"
except :
pass
return False
def is_in_chat(group,user) :
try :
st = bot.get_chat_member(group,user).status
return (st == "member")
except :
pass
return False
def is_mod(group,user,just=False) :
if is_admin(user) and not just:
return True
else:
try :
st = bot.get_chat_member(group,user).status
return (st == "creator" or st == "administrator")
except :
pass
return False
def is_username(m,uname) :
return len(m.entities) >= 1 and m.entities[-1].type == "mention" and m.text[m.entities[-1].offset:(m.entities[-1].offset+m.entities[-1].length)] == uname
def is_mention(m,uname) :
return m.entities[-1].user if len(m.entities) >= 1 and m.entities[-1].type == "text_mention" and m.text[m.entities[-1].offset:(m.entities[-1].offset+m.entities[-1].length)] == uname else False
# Seting & Geting
def ug(user,key) :
return db.hget("user:"+str(user),key)
def us(user,key,value) :
return db.hset("user:"+str(user),key,value)
def ur(id,hash) :
return db.hdel("user:"+str(id),hash)
def gg(group,key) :
return db.hget("group:"+str(group),key)
def gs(group,key,value) :
return db.hset("group:"+str(group),key,value)
def gr(group,key) :
return db.hdel("group:"+str(group),key)
#--- Timed Add & Remove From Redis ---
def tsrem(hash,id) :
if isinstance(id, int) :
s = list(db.sscan_iter(hash,'{\"id\": '+str(id)+', \"time\": *}'))
else :
s = list(db.sscan_iter(hash,'{\"id\": \"'+str(id)+'\", \"time\": *}'))
if s and len(s) == 1 :
db.srem(hash,s[0])
def tsmembers(hash) :
t = []
for v in db.smembers(hash) :
data = json.loads(v)
ekht = data['time'] - tt
if data['time'] != 0 and ekht >= 0 :
t.append({'id' : data.id,'time' : ekht})
elif data['time'] != 0 :
db.srem(hash,v)
else :
t.append({'id' : data.id})
return t
def tsadd(hash,id,time=0) :
if time and time != 0 :
time += tt()
else :
time = 0
tsrem(hash,id)
db.sadd(hash, json.dumps({"id" : id,"time" : time}))
def tsget(hash,id) :
if isinstance(id, int) :
s = list(db.sscan_iter(hash,'{\"id\": '+str(id)+', \"time\": *}'))
else :
s = list(db.sscan_iter(hash,'{\"id\": \"'+str(id)+'\", \"time\": *}'))
if s and len(s) == 1 :
data = json.loads(s[0])
ekht = data['time'] - tt()
if data['time'] != 0 and ekht >= 0 :
return [data['id'],ekht]
elif data['time'] != 0 :
db.srem(hash,s[0])
else :
return data['id']
# Stats
def collect_stats(m) :
db.set("bot:all_messages",int(db.get("bot:all_messages") or 0) + 1)
us(m.from_user.id,"all_messages",int(ug(m.from_user.id,"all_messages") or 0) + 1)
gs(m.chat.id,"all_messages",int(gg(m.chat.id,"all_messages") or 0) + 1)
db.set("bot:"+m.content_type+"_messages",int(db.get("bot:"+m.content_type+"_messages") or 0) + 1)
us(m.from_user.id,m.content_type+"_messages",int(ug(m.from_user.id,m.content_type+"_messages") or 0) + 1)
gs(m.chat.id,m.content_type+"_messages",int(gg(m.chat.id,m.content_type+"_messages") or 0) + 1)
gs(m.chat.id,"user:"+str(m.from_user.id)+":all_messages",int(gg(m.chat.id,"user:"+str(m.from_user.id)+":all_messages") or 0) + 1)
gs(m.chat.id,"user:"+str(m.from_user.id)+":"+m.content_type+"_messages",int(gg(m.chat.id,"user:"+str(m.from_user.id)+":"+m.content_type+"_messages") or 0) + 1)
# Get Stringed Data Of A User
def inf(user) :
if isinstance(user, int):
try :
chat = bot.get_chat(user)
except :
return str(user)
else :
chat = user
if chat.username :
chat.username= "[@"+chat.username+"] "
else :
chat.username = ""
return str(chat.first_name)+" "+chat.username+"("+str(chat.id)+')'
def getChatInfo(l,gp) :
return ln(l,"chatinfot",{"id":gp.id,"title":gp.title,"type":gp.type})
def getChatInfo_long(l,gp) :
mcount = bot.get_chat_members_count(gp.id)
acount = 0
bots = "member"
for admn in bot.get_chat_administrators(gp.id) :
if admn.user.id == boti.id :
bots = admn.status
if admn.status == "creator" :
creator = admn.user
else :
acount += 1
alladmin = gp.all_members_are_administrators
id = gp.id
title = gp.title
stats = {}
for typ in ['all','text', 'audio', 'document', 'photo', 'sticker', 'video', 'voice', 'location', 'contact','game'] :
stats[typ] = int(gg(gp.id,typ+"_messages") or 0)
return ln(l,"gpinfo",{"type":gp.type,"mc":mcount,"ac":acount,"creator":creator,"id":id,"title":title,'bs':bots,'alladmin':alladmin,'stats' : stats})
# --- Select Lang Cb ---
def langkb() :
markup = types.InlineKeyboardMarkup()
markup.row(types.InlineKeyboardButton(text='English',callback_data='chooselang:en'),types.InlineKeyboardButton(text='فارسی',callback_data='chooselang:fa'))
return markup
# --- Multi Lang ---
def ln(l,s,arg = None) :
if s == 'started' :
if l == 'en' :
return '💫 *Welcome to "Magic Anti Spam Bot" :)*\n⚜️ _Bot Created in _ [MagicTeam](https://telegram.me/magicnews)_ with ❤️ by @jan123\nChoose One:_'
else :
return '💫 به ربات "آنتی اسپم مجیک " خوش امدید :)\n⚜️ ساخته شده در [MagicTeam](https://telegram.me/magicnews) با ❤️ توسط @jan123\nیکی را انتخاب کنید: '
if s == 'newsubset' :
if l == 'en' :
return '🚀 User \n'+inf(arg['user'])+'\nJoined to robot as your subset.'
else :
return '🚀 کاربر\n'+inf(arg['user'])+'\nبه عنوان زیر مجموعه شما وارد ربات شد.'
elif s == 'backed' :
if l == 'en' :
return '🔙 *Backed to main menu*\n⭕️ _Choose One :_'
else :
return '🔙 به منو اصلی برگشتید\n⭕️ یکی را انتخاب کنید :'
elif s == 'back' :
if l == 'en' :
return '🔙 Back'
else :
return "🔙 برگشت"
elif s == 'getsettings' :
chat_id = arg["gp"]
#-- Flood
floods = gg(chat_id,"flood:Lock")
flood = str(gg(chat_id,"flood-spam"))
time = str(gg(chat_id,"flood-time"))
#-- Spam
spam = gg(chat_id,"spam:lock")
chare = str(gg(chat_id,"chare"))
# -- Other
ma = gg(chat_id,"muteall")
if ma :
if ma != 'enabled' :
if int(ma) < int(round(tt())) :
gr(chat_id,"muteall")
ma = "Disable"
else :
ma = timetostr(int(ma) - int(round(tt())),'en')
else :
ma = "Enable"
else :
ma = "Disable"
maxwarns = str(gg(chat_id,"warn-number"))
warnaction = gg(chat_id,"warn-action")
wlc = gg(chat_id,"wlc:Enable")
settings = """⭕️*Flood Settings:*
-----------------------
🔹Process Flood => _"""+(floods or "Offed")+"""_"""
if floods :
settings += """
🔹Flood Sensitivity => _"""+(flood or '5')+"""_
🔹Flood Time => _"""+(time or '3')+"""_"""
settings += """
-----------------------
⭕️*Spam Settings:*
-----------------------
🔸Process Spam => _"""+(spam or "Offed")+"""_"""
if spam :
settings += """
🔸Char Sensitivity => _"""+(chare or '500')+"""_"""
settings += """
-----------------------
⭕️*Lock Settings:*
-----------------------
"""
for v in telelocks :
settings += """ 🔸Lock """+v+""" => _"""+(gg(chat_id,v.lower()+':Lock') or 'Unlock')+"""_
"""
settings += """ -----------------------
⭕️*Process Settings:*
-----------------------
"""
for v in teletyps :
if v.lower() != "spam" and v.lower() != "flood" :
settings += """ 🔹Process """+v+""" => _"""+(gg(chat_id,v.lower()+':Lock') or 'Offed')+"""_
"""
settings += """ -----------------------
⭕️*More Settings:*
-----------------------
🔸Mute All => _"""+ma+"""_
🔸Max Warns => _"""+(maxwarns or 3)+"""_
🔸Warn Action => _"""+(warnaction or 'kick')+"""_
🔸Welcome Status: _"""+(wlc or 'Disable')+"""_
-----------------------
Channel:@MagicNews"""
return settings
elif s == 'chatinfot' :
if l == 'en' :
if arg['type'] == "supergroup" :
type = "Supergroup"
else :
type = "Group"
return '<b>Title</b> : <i>'+arg['title'][:40].replace("<","<").replace(">",">").replace("&","&")+'</i>\n<b>ID</b> : <i>'+str(arg['id'])+'</i>\n<b>Type</b> : <i>'+type+'</i>'
else :
if arg['type'] == "supergroup" :
type = "سوپر گروه"
else :
type = "گروه"
return '<code>عنوان</code> : <i>'+arg['title'][:40].replace("<","<").replace(">",">").replace("&","&")+'</i>\n<code>آیدی</code> : <i>'+str(arg['id'])+'</i>\n<code>نوع</code> : <i>'+type+'</i>'
elif s == 'aboutus' :
if l == 'en' :
return '❗️ About Us'
else :
return '❗️ درباره ما'
elif s == "fblocked" :
if l == 'en' :
return "‼️ *Sorry You Are Blocked For 5 minute!(flooding)*"
else :
return "‼️ با عرض معذرت شما برای 5 دقیقه بلاک شدید!(پیام پشت سر هم)"
elif s == 'unblocked' :
if l == 'en' :
return '*You are unblocked!*'
else :
return '`شما آنبلاک شدید!`'
elif s == 'contactust' :
if l == 'en' :
return '*Send your feedback*'
else :
return '`نظر خود را ارسال کنید`'
elif s == 'dontfwd' :
if l == 'en' :
return "*Please dont forward!*"
else :
return '`لطفا فروارد نکنید!`'
elif s == 'justtext' :
if l == 'en' :
return "*Please juust send text message!*"
else :
return '`لطفا فقط پیام متنی بفرستید!`'
elif s == 'contactuss' :
if l == 'en' :
return '#feedback\n*Sent!*'
else :
return '#feedback\n`ارسال شد!`'
elif s == 'contactus' :
if l == 'en' :
return '📮 Contact Us'
else :
return '📮 ارتباط با ما'
elif s == 'nadmin' :
if l == 'en' :
return 'Group not found or bot is not admin in this group or you are not moderetor of this group!'
else :
return 'گروه یافت نشد یا ربات در گروه ادمین نیست یا شما مدیر گروه نیستید!'
elif s == 'locked' :
if l == 'en' :
return arg+' Locked!'
else :
return arg+' بسته شد!'
elif s == 'unlocked' :
if l == 'en' :
return arg+' Unlocked!'
else :
return arg+' باز شد!'
elif s == 'setto' :
if l == 'en' :
return arg['lock']+' is set to "'+arg['proc']+'"!'
else :
return arg['lock']+' روی "'+arg['proc']+'" تنظیم شد!'
elif s == 'chatinfo' :
if l == 'en' :
return 'Chat info'
else :
return 'اطلاعات گروه'
elif s == 'nmutelist' :
if l == 'en' :
return "Could't finy any mute user in this chat!"
else :
return 'هیچ کاربر موتی در این گروه پیدا نشد!'
elif s == 'nbanlist' :
if l == 'en' :
return "Could't finy any ban user in this chat!"
else :
return 'هیچ کاربر مسدودی در این گروه پیدا نشد!'
elif s == 'settings' :
if l == 'en' :
return 'Settings'
else :
return 'تنظیمات'
elif s == 'locksettings' :
if l == 'en' :
return 'Lock Settings'
else :
return 'تنظیمات قفلی'
elif s == 'mainsettings' :
if l == 'en' :
return 'Other settings'
else :
return 'تنظیمات متفرقه'
elif s == 'enablesettings' :
if l == 'en' :
return 'Enabled items'
else :
return 'آیتم های فعال'
elif s == 'floodsettings' :
if l == 'en' :
return 'Flood Settings'
else :
return 'تنظیمات پیام پشت سر هم'
elif s == 'charesettings' :
if l == 'en' :
return 'Chare Settings'
else :
return 'تنظیمات اسپم'
elif s == 'warnsettings' :
if l == 'en' :
return 'Warn Settings'
else :
return 'تنظیمات اخطار'
elif s == 'floodhelp' :
if l == 'en' :
return 'When user sends messages upper than "Max allowed msgs" in a time lower than "Min allowed time" it called "Flood".'
else :
return 'زمانی که کاربر پیام هایی بیش از "بیشترین پیام های مجاز" در زمانی کمتر از "کمترین زمان مجاز" می فرستد آن را "فلوود" می نامیم.'
elif s == 'warnhelp' :
if l == 'en' :
return 'When user sends messages that not allowed probably he/she got warned if user warns is upper than "Max allowed warns" he/she will be "Kicked/Banned" (depends to warn action)'
else :
return 'زمانی که کاربر پیام های غیر مجاز میفرستد احتمالا او اخطار میگیرد اگر اخطارهای کاربر بالاتر از "بیشترین اخطار های مجاز" باشد او "کیک یا بن" خواهد شد (بسته به عملکرد وارن)'
elif s == 'charehelp' :
if l == 'en' :
return 'When user messages character count upper than "Max allowed character" it called "Spam".'
else :
return 'زمانی که تعداد کاراکتر های پیام های کاربر بیش از "بیشترین کاراکتر های مجاز" باشد آن را "اسپم" می نامیم.'
elif s == "phelp" :
if l == 'en' :
return 'Just click on right cloumn and see what happened!'
else :
return 'فقط روی ستون سمت راست کلیک کن و ببین چی میشه!'
elif s == 'Floodnum' :
if l == 'en' :
return 'Msgs num : '+str(arg)
else :
return 'تعداد پیام : '+str(arg)
elif s == 'Charenum' :
if l == 'en' :
return 'Character num : '+str(arg)
else :
return 'تعداد کاراکتر : '+str(arg)
elif s == 'Warnnum' :
if l == 'en' :
return 'Warn num : '+str(arg)
else :
return 'تعداد اخطار : '+str(arg)
elif s == 'Floodtime' :
if l == 'en' :
return 'time : '+str(arg)
else :
return 'زمان : '+str(arg)
elif s == 'Warnaction' :
if l == 'en' :
return 'Warn Action : '+str(arg)
else :
return 'عملکرد اخطار : '+str(arg)
elif s == 'invalidrange' :
if l == 'en' :
return 'Wrong Number ,Range Is ['+str(arg["r1"])+'-'+str(arg["r2"])+']'
else :
return 'عدد نادرست ,عددی در محدوده ['+str(arg["r1"])+'-'+str(arg["r2"])+'] قابل قبول است'
elif s == 'processsettings' :
if l == 'en' :
return 'Process Settings'
else :
return 'تنظیمات عملیاتی'
elif s == 'page1' :
if l == 'en' :
return 'Page 1'
else :
return 'صفحه اول'
elif s == 'page2' :
if l == 'en' :
return 'Page 2'
else :
return 'صفحه دوم'
elif s == 'page3' :
if l == 'en' :
return 'Page 3'
else :
return 'صفحه سوم'
elif s == 'mutelist' :
if l == 'en' :
return 'Mutelist'
else :
return 'موت لیست'
elif s == 'mutelistbase' :
if l == 'en' :
return "Mute List for group : "+str(arg)
else :
return 'موت لیست گروه : '+str(arg)
elif s == 'mutelistforever' :
if l == 'en' :
return "For ever"