File size: 75,102 Bytes
cb254a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
import requests, os, sys, jwt, json, binascii, time, urllib3, xKEys, base64, datetime, re, socket, threading
import psutil
import random
from protobuf_decoder.protobuf_decoder import Parser
from byte import *
from byte import xSendTeamMsg, xSEndMsg
from byte import Auth_Chat
from xHeaders import *
from datetime import datetime, timedelta
from google.protobuf.timestamp_pb2 import Timestamp
from concurrent.futures import ThreadPoolExecutor
from threading import Thread
from black9 import (
    EnC_AEs, EnC_PacKeT, DeCode_PackEt, EnC_Uid, EnC_Vr,
    CrEaTe_ProTo, CrEaTe_VarianT, CrEaTe_LenGTh, DecodE_HeX,
    ArA_CoLor, xBunnEr, ghost_pakcet, GenJoinSquadsPacket, ExiT,
    ChEck_Commande, OpEnSq, SEnd_InV, SPamSq, openroom, spmroom,
    OpenCh, JoinSq, MsqSq, ExitSq, GeTSQDaTa
)
import telebot
from telebot import types
import logging
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

TOKEN = "8886229232:AAHdX3736qpknRjiF2v4qoz_Gv-LVsXvQM0"
ADMIN_IDS = [8368309157]

bot = telebot.TeleBot(TOKEN, parse_mode="HTML")

connected_clients = {}
connected_clients_lock = threading.Lock()

command_semaphore = threading.Semaphore(1)

active_spam_targets = {}
active_room_spam_targets = {}
active_spam_lock = threading.Lock()

spam_initiators = {}
spam_initiators_lock = threading.Lock()

spam_start_times = {}
spam_start_times_lock = threading.Lock()

busy_accounts = {}
busy_accounts_lock = threading.Lock()

account_queue = []
account_queue_lock = threading.Lock()
account_busy_for_commands = {}
account_busy_lock = threading.Lock()

spam_busy_accounts = {}
spam_busy_lock = threading.Lock()

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)

def dec_to_hex(ask):
    ask_result = hex(ask)
    final_result = str(ask_result)[2:]
    if len(final_result) == 1:
        final_result = "0" + final_result
        return final_result
    else:
        return final_result

def encrypt_packet(plain_text, key, iv):
    plain_text = bytes.fromhex(plain_text)
    cipher = AES.new(key, AES.MODE_CBC, iv)
    cipher_text = cipher.encrypt(pad(plain_text, AES.block_size))
    return cipher_text.hex()

def encode_varint(value):
    result = []
    while value > 0x7F:
        result.append((value & 0x7F) | 0x80)
        value >>= 7
    result.append(value & 0x7F)
    return bytes(result)

def create_protobuf_packet(fields, field_number=1):
    packet = b''
    for field, value in fields.items():
        if isinstance(value, dict):
            sub_packet = create_protobuf_packet(value, field)
            tag = (field << 3) | 2
            packet += encode_varint(tag)
            packet += encode_varint(len(sub_packet))
            packet += sub_packet
        elif isinstance(value, int):
            tag = (field << 3) | 0
            packet += encode_varint(tag)
            packet += encode_varint(value)
        elif isinstance(value, str):
            tag = (field << 3) | 2
            packet += encode_varint(tag)
            packet += encode_varint(len(value))
            packet += value.encode()
        elif isinstance(value, bytes):
            tag = (field << 3) | 2
            packet += encode_varint(tag)
            packet += encode_varint(len(value))
            packet += value
    if field_number == 1:
        return packet
    return packet

def get_available_account():
    with connected_clients_lock:
        with busy_accounts_lock:
            for account_id, client in connected_clients.items():
                if (account_id not in busy_accounts and 
                    hasattr(client, 'CliEnts2') and client.CliEnts2 and
                    hasattr(client, 'is_available') and client.is_available()):
                    return client
    return None

def mark_account_busy(account_id):
    with busy_accounts_lock:
        busy_accounts[account_id] = datetime.now()

def mark_account_free(account_id):
    with busy_accounts_lock:
        if account_id in busy_accounts:
            del busy_accounts[account_id]

def mark_account_busy_for_spam(account_id):
    with spam_busy_lock:
        spam_busy_accounts[account_id] = datetime.now()

def mark_account_free_for_spam(account_id):
    with spam_busy_lock:
        if account_id in spam_busy_accounts:
            del spam_busy_accounts[account_id]

def init_account_queue():
    global account_queue
    with account_queue_lock:
        with connected_clients_lock:
            account_queue = list(connected_clients.keys())

def get_next_available_account():
    with account_queue_lock:
        with account_busy_lock:
            for _ in range(len(account_queue)):
                if not account_queue:
                    return None
                
                account_id = account_queue.pop(0)
                
                if account_busy_for_commands.get(account_id, False):
                    account_queue.append(account_id)
                    continue
                
                with connected_clients_lock:
                    client = connected_clients.get(account_id)
                    if client and hasattr(client, 'is_available') and client.is_available():
                        account_queue.append(account_id)
                        return client
                    else:
                        continue
            
            return None

def mark_account_busy_for_commands(account_id):
    with account_busy_lock:
        account_busy_for_commands[account_id] = datetime.now()

def mark_account_free_for_commands(account_id):
    with account_busy_lock:
        if account_id in account_busy_for_commands:
            del account_busy_for_commands[account_id]

def get_squad_info(client, teamcode):
    try:
        if hasattr(client, 'CliEnts2') and client.CliEnts2 and hasattr(client, 'key') and client.key and hasattr(client, 'iv') and client.iv:
            
            join_packet = GenJoinSquadsPacket(teamcode, client.key, client.iv)
            client.CliEnts2.send(join_packet)
            
            start_time = time.time()
            response_received = False
            idT = None
            sq = None
            chat_code = None
            
            while time.time() - start_time < 8:
                try:
                    if hasattr(client, 'DaTa2') and client.DaTa2 and len(client.DaTa2.hex()) > 30:
                        hex_data = client.DaTa2.hex()
                        if '0500' in hex_data[0:4]:
                            try:
                                if "08" in hex_data:
                                    decoded_data = DeCode_PackEt(f'08{hex_data.split("08", 1)[1]}')
                                else:
                                    decoded_data = DeCode_PackEt(hex_data[10:])
                                
                                dT = json.loads(decoded_data)
                                
                                if "5" in dT and "data" in dT["5"]:
                                    team_data = dT["5"]["data"]
                                    
                                    if "31" in team_data and "data" in team_data["31"]:
                                        sq = team_data["31"]["data"]
                                        idT = team_data["1"]["data"]
                                        
                                        if "17" in team_data and "data" in team_data["17"]:
                                            chat_code = team_data["17"]["data"]
                                        elif "14" in team_data and "data" in team_data["14"]:
                                            chat_code = team_data["14"]["data"]
                                        
                                        response_received = True
                                        break
                            except Exception:
                                pass
                    time.sleep(0.1)
                except Exception:
                    time.sleep(0.1)
            
            if response_received and idT and sq:
                client.CliEnts2.send(ExiT('000000', client.key, client.iv))
                return {"success": True, "owner_uid": idT, "squad_code": sq, "chat_code": chat_code}
            
            return {"success": False, "reason": "No response"}
    except Exception as e:
        print(f"Error in get_squad_info: {e}")
        return {"success": False, "reason": str(e)}

def generate_random_color():
    colors = [
        "[00FF00]", "[FFDD00]", "[3813F3]", "[FF0000]",
        "[0000FF]", "[FFA500]", "[DF07F8]", "[11EAFD]",
        "[DCE775]", "[A8E6CF]", "[7CB342]", "[FFB300]",
        "[90EE90]", "[FF4500]", "[FFD700]", "[32CD32]",
        "[87CEEB]", "[9370DB]", "[FF69B4]", "[8A2BE2]"
    ]
    return random.choice(colors)

def execute_ghost_command(client, teamcode, name):
    success = False
    try:
        if hasattr(client, 'CliEnts2') and client.CliEnts2 and hasattr(client, 'key') and client.key and hasattr(client, 'iv') and client.iv:
            
            join_packet = GenJoinSquadsPacket(teamcode, client.key, client.iv)
            client.CliEnts2.send(join_packet)
            
            start_time = time.time()
            response_received = False
            idT = None
            sq = None
            
            while time.time() - start_time < 8:
                try:
                    if hasattr(client, 'DaTa2') and client.DaTa2 and len(client.DaTa2.hex()) > 30:
                        hex_data = client.DaTa2.hex()
                        if '0500' in hex_data[0:4]:
                            try:
                                if "08" in hex_data:
                                    decoded_data = DeCode_PackEt(f'08{hex_data.split("08", 1)[1]}')
                                else:
                                    decoded_data = DeCode_PackEt(hex_data[10:])
                                
                                dT = json.loads(decoded_data)
                                
                                if "5" in dT and "data" in dT["5"]:
                                    team_data = dT["5"]["data"]
                                    
                                    if "31" in team_data and "data" in team_data["31"]:
                                        sq = team_data["31"]["data"]
                                        idT = team_data["1"]["data"]
                                        response_received = True
                                        break
                            except Exception:
                                pass
                    time.sleep(0.1)
                except Exception:
                    time.sleep(0.1)
            
            if response_received and idT and sq:
                for _ in range(3):
                    try:
                        client.CliEnts2.send(GenJoinSquadsPacket(teamcode, client.key, client.iv))
                        client.CliEnts2.send(ghost_pakcet(idT, name, sq, client.key, client.iv))
                        time.sleep(0.1)
                        client.CliEnts2.send(ExiT('000000', client.key, client.iv))
                    except Exception:
                        break
                success = True
            else:
                try:
                    client.CliEnts2.send(ghost_pakcet(teamcode, name, "1", client.key, client.iv))
                    time.sleep(0.5)
                    success = True
                except Exception:
                    pass
    except Exception as e:
        print(f"Error in execute_ghost_command: {e}")
    
    return success

def execute_ghost_attack_command(client, teamcode, name):
    success = False
    try:
        if hasattr(client, 'CliEnts2') and client.CliEnts2 and hasattr(client, 'key') and client.key and hasattr(client, 'iv') and client.iv:
            
            join_packet = GenJoinSquadsPacket(teamcode, client.key, client.iv)
            client.CliEnts2.send(join_packet)
            
            start_time = time.time()
            response_received = False
            idT = None
            sq = None
            
            while time.time() - start_time < 8:
                try:
                    if hasattr(client, 'DaTa2') and client.DaTa2 and len(client.DaTa2.hex()) > 30:
                        hex_data = client.DaTa2.hex()
                        if '0500' in hex_data[0:4]:
                            try:
                                if "08" in hex_data:
                                    decoded_data = DeCode_PackEt(f'08{hex_data.split("08", 1)[1]}')
                                else:
                                    decoded_data = DeCode_PackEt(hex_data[10:])
                                
                                dT = json.loads(decoded_data)
                                
                                if "5" in dT and "data" in dT["5"]:
                                    team_data = dT["5"]["data"]
                                    
                                    if "31" in team_data and "data" in team_data["31"]:
                                        sq = team_data["31"]["data"]
                                        idT = team_data["1"]["data"]
                                        response_received = True
                                        break
                            except Exception:
                                pass
                    time.sleep(0.1)
                except Exception:
                    time.sleep(0.1)
            
            if response_received and idT and sq:
                for _ in range(99):
                    try:
                        client.CliEnts2.send(GenJoinSquadsPacket(teamcode, client.key, client.iv))
                        client.CliEnts2.send(ghost_pakcet(idT, name, sq, client.key, client.iv))
                        time.sleep(0.1)
                        client.CliEnts2.send(ExiT('000000', client.key, client.iv))
                        client.CliEnts2.send(ghost_pakcet(idT, name, sq, client.key, client.iv))
                    except Exception:
                        break
                success = True
            else:
                try:
                    for _ in range(99):
                        client.CliEnts2.send(ghost_pakcet(teamcode, name, "1", client.key, client.iv))
                        time.sleep(0.1)
                    success = True
                except Exception:
                    pass
    except Exception as e:
        print(f"Error in execute_ghost_attack_command: {e}")
    
    return success

@bot.message_handler(commands=['start'])
def start(message):
    user_id = message.from_user.id
    is_admin = user_id in ADMIN_IDS
    
    if is_admin:
        msg = (
            "<b>🤖 Welcome Admin</b>\n\n"
            "<b>📋 Available Commands:</b>\n"
            "• <code>/2 ID</code> - Send 2 player squad invite\n"
            "• <code>/3 ID</code> - Send 3 player squad invite\n"
            "• <code>/4 ID</code> - Send 4 player squad invite\n"
            "• <code>/5 ID</code> - Send 5 player squad invite\n"
            "• <code>/6 ID</code> - Send 6 player squad invite\n"
            "• <code>/attack TEAM_CODE</code> - Force start attack\n"
            "• <code>/ghost TEAM_CODE NAME</code> - Send 3 ghosts to team\n"
            "• <code>/lag TEAM_CODE NAME</code> - Ghost attack (99 ghosts)\n"
            "• <code>/spam ID HOURS/d</code> - Start normal spam\n"
            "• <code>/room ID HOURS/d</code> - Start room spam\n"
            "• <code>/status ID</code> - Check spam status\n"
            "• <code>/stop_spam ID</code> - Stop normal spam\n"
            "• <code>/stop_room ID</code> - Stop room spam\n"
            "• <code>/restart</code> - Restart accounts\n"
            "• <code>/accounts_status</code> - Check accounts status\n\n"
            "<b>👨‍💻 Support:</b> @OTMANHEX"
        )
    else:
        msg = (
            "<b>🤖 Free Fire Bot Commands</b>\n\n"
            "<b>📋 Available Commands:</b>\n"
            "• <code>/2 ID</code> - Send 2 player squad invite\n"
            "• <code>/3 ID</code> - Send 3 player squad invite\n"
            "• <code>/4 ID</code> - Send 4 player squad invite\n"
            "• <code>/5 ID</code> - Send 5 player squad invite\n"
            "• <code>/6 ID</code> - Send 6 player squad invite\n"
            "• <code>/attack TEAM_CODE</code> - Force start attack\n"
            "• <code>/ghost TEAM_CODE NAME</code> - Send 3 ghosts to team\n"
            "• <code>/lag TEAM_CODE NAME</code> - Ghost attack (99 ghosts)\n"
            "• <code>/spam ID HOURS</code> - Start normal spam (max 24h)\n"
            "• <code>/room ID HOURS</code> - Start room spam (max 24h)\n"
            "• <code>/status ID</code> - Check spam status\n"
            "• <code>/stop_spam ID</code> - Stop normal spam\n"
            "• <code>/stop_room ID</code> - Stop room spam\n\n"
            "<b>👨‍💻 Support:</b> @OTMANHEX"
        )
    
    bot.reply_to(message, msg)

@bot.message_handler(commands=['lag'])
def lag_command(message):
    user_id = message.from_user.id
    args = message.text.split()
    
    if len(args) != 3:
        bot.reply_to(message, "<b>❌ Wrong Format</b>\nUse: <code>/lag TEAM_CODE NAME</code>\nExample: <code>/lag 123456789 OTMAN HEX</code>")
        return
    
    team_code = args[1]
    name = args[2]
    
    if not ChEck_Commande(team_code):
        bot.reply_to(message, "<b>❌ Error</b>\nInvalid team code")
        return
    
    waiting_msg = bot.reply_to(
        message,
        f"<b>⚔️ <u>Starting Ghost Attack on team</u> <code>{team_code}</code> <u>...</u></b>\n\n"
        f"<b>👻 Status:</b> <i>Preparing accounts...</i>"
    )
    
    def process_ghost_attack():
        clients = []
        with connected_clients_lock:
            for account_id, client in connected_clients.items():
                if hasattr(client, 'CliEnts2') and client.CliEnts2 and hasattr(client, 'is_available') and client.is_available():
                    clients.append(client)
                    if len(clients) >= 3:
                        break
        
        if not clients:
            bot.edit_message_text(
                f"<b>❌ No accounts available. All accounts are busy. Please try again later.</b>",
                chat_id=message.chat.id,
                message_id=waiting_msg.message_id
            )
            return
        
        bot.edit_message_text(
            f"<b>⚔️ <u>Starting Ghost Attack on team</u> <code>{team_code}</code> <u>...</u></b>\n\n"
            f"<b>👻 Status:</b> <i>Sending 99 ghosts from {len(clients)} accounts...</i>\n"
            f"<b>👻 Attack 1/2 in progress...</b>",
            chat_id=message.chat.id,
            message_id=waiting_msg.message_id
        )
        
        success_count = 0
        threads = []
        
        for client in clients:
            account_id = client.id
            mark_account_busy_for_commands(account_id)
            
            def run_ghost_attack(c):
                nonlocal success_count
                try:
                    if execute_ghost_attack_command(c, team_code, name):
                        success_count += 1
                finally:
                    mark_account_free_for_commands(account_id)
            
            thread = threading.Thread(target=run_ghost_attack, args=(client,))
            threads.append(thread)
            thread.start()
        
        for thread in threads:
            thread.join(timeout=60)
        
        if success_count > 0:
            bot.edit_message_text(
                f"<b>⚔️ <u>Ghost Attack 1/2 completed!</u></b>\n\n"
                f"<b>🎯 Target Team:</b> <code>{team_code}</code>\n"
                f"<b>👻 Ghosts Sent:</b> 99 per account\n"
                f"<b>📊 Accounts Used:</b> {success_count}/{len(clients)}\n\n"
                f"<b>⏳ Waiting 3 seconds before second attack...</b>",
                chat_id=message.chat.id,
                message_id=waiting_msg.message_id
            )
            
            time.sleep(3)
            
            bot.edit_message_text(
                f"<b>⚔️ <u>Starting Ghost Attack 2/2 on team</u> <code>{team_code}</code> <u>...</u></b>\n\n"
                f"<b>👻 Status:</b> <i>Sending 99 ghosts again...</i>",
                chat_id=message.chat.id,
                message_id=waiting_msg.message_id
            )
            
            success_count2 = 0
            threads2 = []
            
            for client in clients:
                account_id = client.id
                mark_account_busy_for_commands(account_id)
                
                def run_ghost_attack2(c):
                    nonlocal success_count2
                    try:
                        if execute_ghost_attack_command(c, team_code, name):
                            success_count2 += 1
                    finally:
                        mark_account_free_for_commands(account_id)
                
                thread2 = threading.Thread(target=run_ghost_attack2, args=(client,))
                threads2.append(thread2)
                thread2.start()
            
            for thread2 in threads2:
                thread2.join(timeout=60)
            
            bot.edit_message_text(
                f"<b>✅ <u>Ghost Attack Completed Successfully!</u></b>\n\n"
                f"<b>🎯 Target Team:</b> <code>{team_code}</code>\n"
                f"<b>👻 Ghosts Sent:</b> 99 per account (2 times)\n"
                f"<b>📊 Attack 1:</b> {success_count}/{len(clients)} accounts\n"
                f"<b>📊 Attack 2:</b> {success_count2}/{len(clients)} accounts\n\n"
                f"<b>🏆 Team <code>{team_code}</code> has been lagged successfully!</b>",
                chat_id=message.chat.id,
                message_id=waiting_msg.message_id
            )
        else:
            bot.edit_message_text(
                f"<b>❌ Ghost Attack Failed!</b>\n\n"
                f"<b>🎯 Target Team:</b> <code>{team_code}</code>\n"
                f"<b>⚠️ Could not complete the attack. Please try again.</b>",
                chat_id=message.chat.id,
                message_id=waiting_msg.message_id
            )
    
    threading.Thread(target=process_ghost_attack, daemon=True).start()

@bot.message_handler(commands=['ghost'])
def ghost_command(message):
    user_id = message.from_user.id
    args = message.text.split()
    
    if len(args) != 3:
        bot.reply_to(message, "<b>❌ Wrong Format</b>\nUse: <code>/ghost TEAM_CODE NAME</code>\nExample: <code>/ghost 123456789 OTMAN HEX</code>")
        return
    
    team_code = args[1]
    name = args[2]
    
    if not ChEck_Commande(team_code):
        bot.reply_to(message, "<b>❌ Error</b>\nInvalid team code")
        return
    
    waiting_msg = bot.reply_to(
        message,
        f"<b>👻 Sending 3 ghosts to team <code>{team_code}</code>...</b>"
    )
    
    def process_ghost():
        clients = []
        with connected_clients_lock:
            for account_id, client in connected_clients.items():
                if hasattr(client, 'CliEnts2') and client.CliEnts2 and hasattr(client, 'is_available') and client.is_available():
                    clients.append(client)
                    if len(clients) >= 3:
                        break
        
        if not clients:
            bot.edit_message_text(
                f"<b>❌ No accounts available. All accounts are busy. Please try again later.</b>",
                chat_id=message.chat.id,
                message_id=waiting_msg.message_id
            )
            return
        
        success_count = 0
        threads = []
        
        for client in clients:
            account_id = client.id
            mark_account_busy_for_commands(account_id)
            
            def run_ghost(c):
                nonlocal success_count
                try:
                    if execute_ghost_command(c, team_code, name):
                        success_count += 1
                finally:
                    mark_account_free_for_commands(account_id)
            
            thread = threading.Thread(target=run_ghost, args=(client,))
            threads.append(thread)
            thread.start()
        
        for thread in threads:
            thread.join(timeout=30)
        
        if success_count > 0:
            bot.edit_message_text(
                f"<b>✅ Successfully sent {success_count} ghosts to team <code>{team_code}</code></b>",
                chat_id=message.chat.id,
                message_id=waiting_msg.message_id
            )
        else:
            bot.edit_message_text(
                f"<b>❌ Failed to send ghosts to team <code>{team_code}</code></b>",
                chat_id=message.chat.id,
                message_id=waiting_msg.message_id
            )
    
    threading.Thread(target=process_ghost, daemon=True).start()

@bot.message_handler(commands=['2', '3', '4', '5', '6'])
def invite_command(message):
    user_id = message.from_user.id
    args = message.text.split()
    
    if len(args) != 2:
        bot.reply_to(message, f"<b>❌ Wrong Format</b>\nUse: <code>{message.text.split()[0]} ID</code>")
        return
    
    command = message.text.split()[0][1:]
    target_id = args[1]
    
    waiting_msg = bot.reply_to(
        message, 
        f"<b>🔄 Opening {command} Player Squad for <code>{target_id}</code>...</b>"
    )
    
    def process_invite():
        client = get_next_available_account()
        
        if not client:
            bot.edit_message_text(
                f"<b>❌ No accounts available. All accounts are busy. Please try again later.</b>",
                chat_id=message.chat.id,
                message_id=waiting_msg.message_id
            )
            return
        
        account_id = client.id
        mark_account_busy_for_commands(account_id)
        
        try:
            success = client.execute_invite_command(target_id, command)
            
            if success:
                bot.edit_message_text(
                    f"<b>✅ Invite sent to <code>{target_id}</code> for {command} player squad</b>",
                    chat_id=message.chat.id,
                    message_id=waiting_msg.message_id
                )
            else:
                bot.edit_message_text(
                    f"<b>❌ Failed to send invite to <code>{target_id}</code></b>",
                    chat_id=message.chat.id,
                    message_id=waiting_msg.message_id
                )
        finally:
            mark_account_free_for_commands(account_id)
    
    threading.Thread(target=process_invite, daemon=True).start()

@bot.message_handler(commands=['attack'])
def attack_command(message):
    user_id = message.from_user.id
    args = message.text.split()
    
    if len(args) != 2:
        bot.reply_to(message, "<b>❌ Wrong Format</b>\nUse: <code>/attack TEAM_CODE</code>")
        return
    
    team_code = args[1]
    
    waiting_msg = bot.reply_to(
        message,
        f"<b>⚔️ Starting forced attack for team code <code>{team_code}</code>...</b>"
    )
    
    def process_attack():
        client = get_next_available_account()
        
        if not client:
            bot.edit_message_text(
                f"<b>❌ No accounts available. All accounts are busy. Please try again later.</b>",
                chat_id=message.chat.id,
                message_id=waiting_msg.message_id
            )
            return
        
        account_id = client.id
        mark_account_busy_for_commands(account_id)
        
        try:
            success = client.execute_attack_command(team_code)
            
            if success:
                bot.edit_message_text(
                    f"<b>✅ Attack completed on team <code>{team_code}</code></b>",
                    chat_id=message.chat.id,
                    message_id=waiting_msg.message_id
                )
            else:
                bot.edit_message_text(
                    f"<b>❌ Attack failed on team <code>{team_code}</code></b>",
                    chat_id=message.chat.id,
                    message_id=waiting_msg.message_id
                )
        finally:
            mark_account_free_for_commands(account_id)
    
    threading.Thread(target=process_attack, daemon=True).start()

@bot.message_handler(commands=['spam'])
def spam_command(message):
    user_id = message.from_user.id
    args = message.text.split()
    
    try:
        if len(args) != 3:
            bot.reply_to(message, "<b>❌ Wrong Format</b>\nUse: <code>/spam ID HOURS</code>")
            return
        
        target_id = args[1]
        
        if user_id in ADMIN_IDS:
            time_value = args[2]
            if 'd' in time_value:
                days = int(time_value.replace('d', ''))
                hours = days * 24
            else:
                hours = int(time_value)
        else:
            hours = int(args[2])
            if hours > 24:
                bot.reply_to(message, "<b>❌ Error</b>\nNormal users can only use up to 24 hours maximum")
                return
        
        if hours <= 0:
            bot.reply_to(message, "<b>❌ Error</b>\nHours must be a positive number")
            return
        
        if not ChEck_Commande(target_id):
            bot.reply_to(message, "<b>❌ Error</b>\nInvalid user_id")
            return
        
        with active_spam_lock:
            if target_id in active_spam_targets:
                bot.reply_to(message, f"<b>❌ This account is already in spam</b>")
                return
            
            active_spam_targets[target_id] = {
                'active': True,
                'start_time': datetime.now(),
                'duration': hours,
                'type': 'normal',
                'initiator': user_id
            }
            
            with spam_start_times_lock:
                spam_start_times[target_id] = datetime.now()
            
            with spam_initiators_lock:
                spam_initiators[target_id] = user_id
            
            threading.Thread(target=normal_spam_worker, args=(target_id, hours), daemon=True).start()
            
            time_text = "days" if hours >= 24 else "hours"
            time_value = hours // 24 if hours >= 24 else hours
            
            bot.reply_to(
                message, 
                f"<b>✅ Successfully started spam for ID <code>{target_id}</code> for {time_value} {time_text}</b>"
            )
            
    except ValueError:
        bot.reply_to(message, "<b>❌ Error</b>\nPlease enter a valid number")
    except Exception as e:
        bot.reply_to(message, f"<b>❌ Error:</b> {str(e)}")

@bot.message_handler(commands=['room'])
def room_command(message):
    user_id = message.from_user.id
    args = message.text.split()
    
    try:
        if len(args) != 3:
            bot.reply_to(message, "<b>❌ Wrong Format</b>\nUse: <code>/room ID HOURS</code>")
            return
        
        target_id = args[1]
        
        if user_id in ADMIN_IDS:
            time_value = args[2]
            if 'd' in time_value:
                days = int(time_value.replace('d', ''))
                hours = days * 24
            else:
                hours = int(time_value)
        else:
            hours = int(args[2])
            if hours > 24:
                bot.reply_to(message, "<b>❌ Error</b>\nNormal users can only use up to 24 hours maximum")
                return
        
        if hours <= 0:
            bot.reply_to(message, "<b>❌ Error</b>\nHours must be a positive number")
            return
        
        if not ChEck_Commande(target_id):
            bot.reply_to(message, "<b>❌ Error</b>\nInvalid user_id")
            return
        
        with active_spam_lock:
            if target_id in active_room_spam_targets:
                bot.reply_to(message, f"<b>❌ This account is already in room spam</b>")
                return
            
            active_room_spam_targets[target_id] = {
                'active': True,
                'start_time': datetime.now(),
                'duration': hours,
                'type': 'room',
                'initiator': user_id
            }
            
            with spam_start_times_lock:
                spam_start_times[f"room_{target_id}"] = datetime.now()
            
            with spam_initiators_lock:
                spam_initiators[f"room_{target_id}"] = user_id
            
            threading.Thread(target=room_spam_worker, args=(target_id, hours), daemon=True).start()
            
            time_text = "days" if hours >= 24 else "hours"
            time_value = hours // 24 if hours >= 24 else hours
            
            bot.reply_to(
                message, 
                f"<b>✅ Successfully started room spam for ID <code>{target_id}</code> for {time_value} {time_text}</b>"
            )
            
    except ValueError:
        bot.reply_to(message, "<b>❌ Error</b>\nPlease enter a valid number")
    except Exception as e:
        bot.reply_to(message, f"<b>❌ Error:</b> {str(e)}")

@bot.message_handler(commands=['status'])
def status_command(message):
    args = message.text.split()
    
    try:
        if len(args) != 2:
            bot.reply_to(message, "<b>❌ Wrong Format</b>\nUse: <code>/status ID</code>")
            return
        
        target_id = args[1]
        found = False
        
        with active_spam_lock:
            if target_id in active_spam_targets:
                with spam_start_times_lock:
                    if target_id in spam_start_times:
                        start_time = spam_start_times[target_id]
                        elapsed = datetime.now() - start_time
                        total_seconds = int(elapsed.total_seconds())
                        duration = active_spam_targets[target_id]['duration'] * 3600
                        remaining = duration - total_seconds
                        
                        if remaining > 0:
                            days = remaining // 86400
                            hours = (remaining % 86400) // 3600
                            minutes = (remaining % 3600) // 60
                            seconds = remaining % 60
                            
                            time_parts = []
                            if days > 0:
                                time_parts.append(f"{days}d")
                            if hours > 0:
                                time_parts.append(f"{hours}h")
                            if minutes > 0:
                                time_parts.append(f"{minutes}m")
                            if seconds > 0:
                                time_parts.append(f"{seconds}s")
                            
                            time_left = " ".join(time_parts)
                            
                            bot.reply_to(
                                message,
                                f"<b>📊 Spam Status</b>\n\n"
                                f"<b>Target:</b> <code>{target_id}</code>\n"
                                f"<b>Type:</b> Normal Spam\n"
                                f"<b>Time Left:</b> {time_left}\n"
                                f"<b>Started By:</b> <code>{active_spam_targets[target_id]['initiator']}</code>"
                            )
                            found = True
        
        if not found:
            with active_spam_lock:
                if target_id in active_room_spam_targets:
                    with spam_start_times_lock:
                        room_key = f"room_{target_id}"
                        if room_key in spam_start_times:
                            start_time = spam_start_times[room_key]
                            elapsed = datetime.now() - start_time
                            total_seconds = int(elapsed.total_seconds())
                            duration = active_room_spam_targets[target_id]['duration'] * 3600
                            remaining = duration - total_seconds
                            
                            if remaining > 0:
                                days = remaining // 86400
                                hours = (remaining % 86400) // 3600
                                minutes = (remaining % 3600) // 60
                                seconds = remaining % 60
                                
                                time_parts = []
                                if days > 0:
                                    time_parts.append(f"{days}d")
                                if hours > 0:
                                    time_parts.append(f"{hours}h")
                                if minutes > 0:
                                    time_parts.append(f"{minutes}m")
                                if seconds > 0:
                                    time_parts.append(f"{seconds}s")
                                
                                time_left = " ".join(time_parts)
                                
                                bot.reply_to(
                                    message,
                                    f"<b>📊 Spam Status</b>\n\n"
                                    f"<b>Target:</b> <code>{target_id}</code>\n"
                                    f"<b>Type:</b> Room Spam\n"
                                    f"<b>Time Left:</b> {time_left}\n"
                                    f"<b>Started By:</b> <code>{active_room_spam_targets[target_id]['initiator']}</code>"
                                )
                                found = True
        
        if not found:
            bot.reply_to(message, f"<b>❌ No active spam found for ID <code>{target_id}</code></b>")
            
    except Exception as e:
        bot.reply_to(message, f"<b>❌ Error:</b> {str(e)}")

@bot.message_handler(commands=['stop_spam'])
def stop_spam_command(message):
    user_id = message.from_user.id
    args = message.text.split()
    
    try:
        if len(args) != 2:
            bot.reply_to(message, "<b>❌ Wrong Format</b>\nUse: <code>/stop_spam ID</code>")
            return
        
        target_id = args[1]
        
        with active_spam_lock:
            if target_id not in active_spam_targets:
                bot.reply_to(message, f"<b>❌ No active spam found for ID <code>{target_id}</code></b>")
                return
            
            with spam_initiators_lock:
                initiator = spam_initiators.get(target_id)
                if initiator != user_id and user_id not in ADMIN_IDS:
                    bot.reply_to(
                        message, 
                        "<b>⛔ Sorry, you cannot stop this spam. Only the person who added the ID can stop it.</b>"
                    )
                    return
                
                if target_id in spam_initiators:
                    del spam_initiators[target_id]
            
            with spam_start_times_lock:
                if target_id in spam_start_times:
                    del spam_start_times[target_id]
            
            del active_spam_targets[target_id]
            
            bot.reply_to(
                message, 
                f"<b>✅ Successfully stopped spam for target ID <code>{target_id}</code></b>"
            )
            
    except Exception as e:
        bot.reply_to(message, f"<b>❌ Error:</b> {str(e)}")

@bot.message_handler(commands=['stop_room'])
def stop_room_command(message):
    user_id = message.from_user.id
    args = message.text.split()
    
    try:
        if len(args) != 2:
            bot.reply_to(message, "<b>❌ Wrong Format</b>\nUse: <code>/stop_room ID</code>")
            return
        
        target_id = args[1]
        room_key = f"room_{target_id}"
        
        with active_spam_lock:
            if target_id not in active_room_spam_targets:
                bot.reply_to(message, f"<b>❌ No active room spam found for ID <code>{target_id}</code></b>")
                return
            
            with spam_initiators_lock:
                initiator = spam_initiators.get(room_key)
                if initiator != user_id and user_id not in ADMIN_IDS:
                    bot.reply_to(
                        message, 
                        "<b>⛔ Sorry, you cannot stop this spam. Only the person who added the ID can stop it.</b>"
                    )
                    return
                
                if room_key in spam_initiators:
                    del spam_initiators[room_key]
            
            with spam_start_times_lock:
                if room_key in spam_start_times:
                    del spam_start_times[room_key]
            
            del active_room_spam_targets[target_id]
            
            bot.reply_to(
                message, 
                f"<b>✅ Successfully stopped room spam for target ID <code>{target_id}</code></b>"
            )
            
    except Exception as e:
        bot.reply_to(message, f"<b>❌ Error:</b> {str(e)}")

@bot.message_handler(commands=['restart'])
def restart_command(message):
    user_id = message.from_user.id
    
    if user_id not in ADMIN_IDS:
        bot.reply_to(message, "<b>⛔ This command is for admins only</b>")
        return
    
    bot.reply_to(message, "<b>🔄 Restarting accounts...</b>")
    threading.Thread(target=restart_accounts, daemon=True).start()

@bot.message_handler(commands=['accounts_status'])
def accounts_status_command(message):
    user_id = message.from_user.id
    
    if user_id not in ADMIN_IDS:
        bot.reply_to(message, "<b>⛔ This command is for admins only</b>")
        return
    
    with connected_clients_lock:
        total = len(connected_clients)
    
    with busy_accounts_lock:
        busy = len(busy_accounts)
        busy_ids = list(busy_accounts.keys())
    
    with spam_busy_lock:
        spam_busy = len(spam_busy_accounts)
    
    free = total - busy - spam_busy
    
    status_msg = f"<b>📊 Accounts Status</b>\n\n"
    status_msg += f"<b>Total:</b> {total}\n"
    status_msg += f"<b>Busy (Commands):</b> {busy}\n"
    status_msg += f"<b>Busy (Spam):</b> {spam_busy}\n"
    status_msg += f"<b>Free:</b> {free}\n"
    
    if busy_ids:
        status_msg += f"\n<b>Busy Accounts:</b>\n"
        for aid in list(busy_ids)[:5]:
            status_msg += f"• <code>{aid}</code>\n"
    
    bot.reply_to(message, status_msg)

def restart_accounts():
    time.sleep(2)
    ResTarT_BoT()

def AuTo_ResTartinG():
    time.sleep(6 * 60 * 60)
    print('Auto Restarting The Bot...')
    p = psutil.Process(os.getpid())
    for handler in p.open_files():
        try:
            os.close(handler.fd)
        except Exception as e:
            print(f"Error Close Files: {e}")
    for conn in p.net_connections():
        try:
            if hasattr(conn, 'fd'):
                os.close(conn.fd)
        except Exception as e:
            print(f"Error Close Connection: {e}")
    sys.path.append(os.path.dirname(os.path.abspath(sys.argv[0])))
    python = sys.executable
    os.execl(python, python, *sys.argv)
       
def ResTarT_BoT():
    print('Restarting The Bot...')
    p = psutil.Process(os.getpid())
    open_files = p.open_files()
    connections = p.net_connections()
    for handler in open_files:
        try:
            os.close(handler.fd)
        except Exception:
            pass           
    for conn in connections:
        try:
            conn.close()
        except Exception:
            pass
    sys.path.append(os.path.dirname(os.path.abspath(sys.argv[0])))
    python = sys.executable
    os.execl(python, python, *sys.argv)

Thread(target=AuTo_ResTartinG, daemon=True).start()

ACCOUNTS = []

def load_accounts_from_file(filename="accs.json"):
    accounts = []
    try:
        with open(filename, "r", encoding="utf-8") as file:
            data = json.load(file)
            
            if isinstance(data, list):
                for account in data:
                    if isinstance(account, dict):
                        account_id = account.get('uid', '')
                        password = account.get('password', '')
                        
                        if account_id:
                            accounts.append({
                                'id': str(account_id),
                                'password': password
                            })
            
            print(f"Loaded {len(accounts)} accounts from {filename}")
            
    except FileNotFoundError:
        print(f"File {filename} not found!")
    except json.JSONDecodeError:
        print(f"JSON format error in file {filename}!")
    except Exception as e:
        print(f"Error reading file: {e}")
    
    return accounts

ACCOUNTS = load_accounts_from_file()

def normal_spam_worker(target_id, duration_hours=None):
    print(f"Starting normal spam on target: {target_id}" + (f" for {duration_hours} hours" if duration_hours else ""))
    
    start_time = datetime.now()
    
    while True:
        with active_spam_lock:
            if target_id not in active_spam_targets:
                print(f"Normal spam stopped on target: {target_id}")
                break
                
            if duration_hours:
                elapsed = datetime.now() - start_time
                if elapsed.total_seconds() >= duration_hours * 3600:
                    print(f"Normal spam duration ended for target: {target_id}")
                    with spam_initiators_lock:
                        if target_id in spam_initiators:
                            del spam_initiators[target_id]
                    with spam_start_times_lock:
                        if target_id in spam_start_times:
                            del spam_start_times[target_id]
                    del active_spam_targets[target_id]
                    break
                
        try:
            send_normal_spam_from_all_accounts(target_id)
            time.sleep(0.1)
        except Exception as e:
            print(f"Error in normal spam on {target_id}: {e}")
            time.sleep(1)

def room_spam_worker(target_id, duration_hours=None):
    print(f"Starting room spam on target: {target_id}" + (f" for {duration_hours} hours" if duration_hours else ""))
    
    start_time = datetime.now()
    
    while True:
        with active_spam_lock:
            if target_id not in active_room_spam_targets:
                print(f"Room spam stopped on target: {target_id}")
                break
                
            if duration_hours:
                elapsed = datetime.now() - start_time
                if elapsed.total_seconds() >= duration_hours * 3600:
                    print(f"Room spam duration ended for target: {target_id}")
                    with spam_initiators_lock:
                        room_key = f"room_{target_id}"
                        if room_key in spam_initiators:
                            del spam_initiators[room_key]
                    with spam_start_times_lock:
                        if room_key in spam_start_times:
                            del spam_start_times[room_key]
                    del active_room_spam_targets[target_id]
                    break
                
        try:
            send_room_spam_from_all_accounts(target_id)
            time.sleep(0.1)
        except Exception as e:
            print(f"Error in room spam on {target_id}: {e}")
            time.sleep(1)

def send_normal_spam_from_all_accounts(target_id):
    with connected_clients_lock:
        for account_id, client in connected_clients.items():
            try:
                if (hasattr(client, 'CliEnts2') and client.CliEnts2 and 
                    hasattr(client, 'key') and client.key and 
                    hasattr(client, 'iv') and client.iv):
                    
                    mark_account_busy_for_spam(account_id)
                    
                    try:
                        for i in range(10):
                            try:
                                client.CliEnts2.send(SEnd_InV(1, target_id, client.key, client.iv))                           
                                client.CliEnts2.send(OpEnSq(client.key, client.iv))                            
                                client.CliEnts2.send(SPamSq(target_id, client.key, client.iv))
                            except (BrokenPipeError, ConnectionResetError, OSError) as e:
                                print(f"Connection error for account {account_id}: {e}")
                                break
                            except Exception as e:
                                print(f"Error sending from account {account_id}: {e}")
                                break
                    finally:
                        mark_account_free_for_spam(account_id)
                else:
                    print(f"Account {account_id} connection is inactive")
            except Exception as e:
                print(f"Error sending normal spam from account {account_id}: {e}")

def send_room_spam_from_all_accounts(target_id):
    with connected_clients_lock:
        for account_id, client in connected_clients.items():
            try:
                if (hasattr(client, 'CliEnts2') and client.CliEnts2 and 
                    hasattr(client, 'key') and client.key and 
                    hasattr(client, 'iv') and client.iv):
                    
                    mark_account_busy_for_spam(account_id)
                    
                    try:
                        try:
                            client.CliEnts2.send(openroom(client.key, client.iv))
                        except Exception as e:
                            print(f"Error opening room from account {account_id}: {e}")
                        
                        for i in range(10):  
                            try:
                                client.CliEnts2.send(spmroom(client.key, client.iv, target_id))
                            except (BrokenPipeError, ConnectionResetError, OSError) as e:
                                print(f"Connection error for account {account_id}: {e}")
                                break
                            except Exception as e:
                                print(f"Error sending from account {account_id}: {e}")
                                break
                    finally:
                        mark_account_free_for_spam(account_id)
                else:
                    print(f"Account {account_id} connection is inactive")
            except Exception as e:
                print(f"Error sending room spam from account {account_id}: {e}")

class FF_CLient():

    def __init__(self, id, password):
        self.id = id
        self.password = password
        self.key = None
        self.iv = None
        self.connection_active = True
        self.Get_FiNal_ToKen_0115()     
            
    def is_available(self):
        return (hasattr(self, 'CliEnts2') and 
                self.CliEnts2 and 
                hasattr(self, 'key') and 
                self.key and
                hasattr(self, 'iv') and 
                self.iv and
                self.connection_active)
            
    def Connect_SerVer_OnLine(self, Token, tok, host, port, key, iv, host2, port2):
        try:
            self.AutH_ToKen_0115 = tok    
            self.CliEnts2 = socket.create_connection((host2, int(port2)))
            self.CliEnts2.send(bytes.fromhex(self.AutH_ToKen_0115))                  
        except:
            pass        
        while self.connection_active:
            try:
                self.DaTa2 = self.CliEnts2.recv(99999)
                if '0500' in self.DaTa2.hex()[0:4] and len(self.DaTa2.hex()) > 30:
                    self.packet = json.loads(DeCode_PackEt(f'08{self.DaTa2.hex().split("08", 1)[1]}'))
                    self.AutH = self.packet['5']['data']['7']['data']
            except:
                pass
                                                            
    def Connect_SerVer(self, Token, tok, host, port, key, iv, host2, port2):
        self.AutH_ToKen_0115 = tok    
        self.CliEnts = socket.create_connection((host, int(port)))
        self.CliEnts.send(bytes.fromhex(self.AutH_ToKen_0115))  
        self.DaTa = self.CliEnts.recv(1024)          	        
        threading.Thread(target=self.Connect_SerVer_OnLine, args=(Token, tok, host, port, key, iv, host2, port2)).start()
        self.Exemple = xMsGFixinG('12345678')
        
        self.key = key
        self.iv = iv
        
        with connected_clients_lock:
            connected_clients[self.id] = self
            with account_queue_lock:
                if self.id not in account_queue:
                    account_queue.append(self.id)
            print(f"Account {self.id} registered, total accounts: {len(connected_clients)}")
        
        while True:      
            try:
                self.DaTa = self.CliEnts.recv(1024)   
                if len(self.DaTa) == 0 or (hasattr(self, 'DaTa2') and len(self.DaTa2) == 0):
                    print(f"Connection lost for account {self.id}, reconnecting...")
                    try:
                        self.CliEnts.close()
                        if hasattr(self, 'CliEnts2'):
                            self.CliEnts2.close()
                        
                        time.sleep(3)
                        self.Connect_SerVer(Token, tok, host, port, key, iv, host2, port2)
                    except:
                        print(f"Failed to reconnect account {self.id}")
                        with connected_clients_lock:
                            if self.id in connected_clients:
                                del connected_clients[self.id]
                        break	            
                                      
                if '/pp/' in self.input_msg[:4]:
                    self.target_id = self.input_msg[4:]	 
                    self.Zx = ChEck_Commande(self.target_id)
                    if True == self.Zx:
                        threading.Thread(target=send_normal_spam_from_all_accounts, args=(self.target_id,)).start()
                        time.sleep(2.5)
                        self.CliEnts.send(xSEndMsg(f'\n[b][c][{ArA_CoLor()}] Success Spam To {xMsGFixinG(self.target_id)} From All Accounts\n', 2, self.DeCode_CliEnt_Uid, self.DeCode_CliEnt_Uid, key, iv))
                        time.sleep(1.3)
                        self.CliEnts.close()
                        if hasattr(self, 'CliEnts2'):
                            self.CliEnts2.close()
                        self.Connect_SerVer(Token, tok, host, port, key, iv, host2, port2)
                    elif False == self.Zx:
                        self.CliEnts.send(xSEndMsg(f'\n[b][c][{ArA_CoLor()}] - Please Use /pp/<id>\n - Ex : /pp/{self.Exemple}\n', 2, self.DeCode_CliEnt_Uid, self.DeCode_CliEnt_Uid, key, iv))	
                        time.sleep(1.1)
                        self.CliEnts.close()
                        if hasattr(self, 'CliEnts2'):
                            self.CliEnts2.close()
                        self.Connect_SerVer(Token, tok, host, port, key, iv, host2, port2)

            except Exception as e:
                print(f"Error in Connect_SerVer: {e}")
                try:
                    self.CliEnts.close()
                    if hasattr(self, 'CliEnts2'):
                        self.CliEnts2.close()
                except:
                    pass
                time.sleep(5)
                try:
                    self.Connect_SerVer(Token, tok, host, port, key, iv, host2, port2)
                except:
                    print(f"Final reconnection failed for account {self.id}")
                    with connected_clients_lock:
                        if self.id in connected_clients:
                            del connected_clients[self.id]
                    break
                                    
    def GeT_Key_Iv(self, serialized_data):
        my_message = xKEys.MyMessage()
        my_message.ParseFromString(serialized_data)
        timestamp, key, iv = my_message.field21, my_message.field22, my_message.field23
        timestamp_obj = Timestamp()
        timestamp_obj.FromNanoseconds(timestamp)
        timestamp_seconds = timestamp_obj.seconds
        timestamp_nanos = timestamp_obj.nanos
        combined_timestamp = timestamp_seconds * 1_000_000_000 + timestamp_nanos
        return combined_timestamp, key, iv    

    def Guest_GeneRaTe(self, uid, password):
        self.url = "https://100067.connect.garena.com/oauth/guest/token/grant"
        self.headers = {"Host": "100067.connect.garena.com","User-Agent": "GarenaMSDK/4.0.19P4(G011A ;Android 9;en;US;)","Content-Type": "application/x-www-form-urlencoded","Accept-Encoding": "gzip, deflate, br","Connection": "close",}
        self.dataa = {"uid": f"{uid}","password": f"{password}","response_type": "token","client_type": "2","client_secret": "2ee44819e9b4598845141067b281621874d0d5d7af9d8f7e00c1e54715b7d1e3","client_id": "100067",}
        try:
            self.response = requests.post(self.url, headers=self.headers, data=self.dataa).json()
            self.Access_ToKen, self.Access_Uid = self.response['access_token'], self.response['open_id']
            time.sleep(0.2)
            return self.ToKen_GeneRaTe(self.Access_ToKen, self.Access_Uid)
        except Exception as e: 
            print(f"Error in Guest_GeneRaTe: {e}")
            time.sleep(10)
            return self.Guest_GeneRaTe(uid, password)
                                        
    def GeT_LoGin_PorTs(self, JwT_ToKen, PayLoad):
        self.UrL = 'https://clientbp.ggpolarbear.com/GetLoginData'
        self.HeadErs = {
            'Expect': '100-continue',
            'Authorization': f'Bearer {JwT_ToKen}',
            'X-Unity-Version': '2022.3.47f1',
            'X-GA': 'v1 1',
            'ReleaseVersion': 'OB53',
            'Content-Type': 'application/x-www-form-urlencoded',
            'User-Agent': 'UnityPlayer/2022.3.47f1 (UnityWebRequest/1.0, libcurl/8.5.0-DEV)',
            'Host': 'clientbp.ggpolarbear.com',
            'Connection': 'close',
            'Accept-Encoding': 'deflate, gzip',}        
        try:
            self.Res = requests.post(self.UrL, headers=self.HeadErs, data=PayLoad, verify=False)
            self.BesTo_data = json.loads(DeCode_PackEt(self.Res.content.hex()))  
            address, address2 = self.BesTo_data['32']['data'], self.BesTo_data['14']['data'] 
            ip, ip2 = address[:len(address) - 6], address2[:len(address2) - 6]
            port, port2 = address[len(address) - 5:], address2[len(address2) - 5:]             
            return ip, port, ip2, port2          
        except requests.RequestException as e:
            print(f"Bad Requests")
        print("Failed To Get Ports")
        return None, None, None, None
        
    def ToKen_GeneRaTe(self, Access_ToKen, Access_Uid):
        self.UrL = "https://loginbp.ggpolarbear.com/MajorLogin"
        self.HeadErs = {
            'X-Unity-Version': '2022.3.47f1',
            'ReleaseVersion': 'OB53',
            'Content-Type': 'application/x-www-form-urlencoded',
            'X-GA': 'v1 1',
            'Content-Length': '928',
            'User-Agent': 'UnityPlayer/2022.3.47f1 (UnityWebRequest/1.0, libcurl/8.5.0-DEV)',
            'Host': 'loginbp.ggpolarbear.com',
            'Connection': 'Keep-Alive',
            'Accept-Encoding': 'deflate, gzip'}   
        
        self.dT = bytes.fromhex('1a13323032352d31312d32362030313a35313a3238220966726565206669726528013a07312e3132332e314232416e64726f6964204f532039202f204150492d3238202850492f72656c2e636a772e32303232303531382e313134313333294a0848616e6468656c64520c4d544e2f537061636574656c5a045749464960800a68d00572033234307a2d7838362d3634205353453320535345342e3120535345342e32204156582041565832207c2032343030207c20348001e61e8a010f416472656e6f2028544d292036343092010d4f70656e474c20455320332e329a012b476f6f676c657c36323566373136662d393161372d343935622d396631362d303866653964336336353333a2010e3137362e32382e3133392e313835aa01026172b201203433303632343537393364653836646134323561353263616164663231656564ba010134c2010848616e6468656c64ca010d4f6e65506c7573204135303130ea014063363961653230386661643732373338623637346232383437623530613361316466613235643161313966616537343566633736616334613065343134633934f00101ca020c4d544e2f537061636574656cd2020457494649ca03203161633462383065636630343738613434323033626638666163363132306635e003b5ee02e8039a8002f003af13f80384078004a78f028804b5ee029004a78f029804b5ee02b00404c80401d2043d2f646174612f6170702f636f6d2e6474732e667265656669726574682d66705843537068495636644b43376a4c2d574f7952413d3d2f6c69622f61726de00401ea045f65363261623933353464386662356662303831646233333861636233333439317c2f646174612f6170702f636f6d2e6474732e667265656669726574682d66705843537068495636644b43376a4c2d574f7952413d3d2f626173652e61706bf00406f804018a050233329a050a32303139313139303236a80503b205094f70656e474c455332b805ff01c00504e005be7eea05093372645f7061727479f205704b717348543857393347646347335a6f7a454e6646775648746d377171316552554e6149444e67526f626f7a4942744c4f695943633459367a767670634943787a514632734f453463627974774c7334785a62526e70524d706d5752514b6d654f35766373386e51594268777148374bf805e7e4068806019006019a060134a2060134b2062213521146500e590349510e460900115843395f005b510f685b560a6107576d0f0366')
        
        self.dT = self.dT.replace(b'2026-01-14 12:19:02', str(datetime.now())[:-7].encode())        
        self.dT = self.dT.replace(b'c69ae208fad72738b674b2847b50a3a1dfa25d1a19fae745fc76ac4a0e414c94', Access_ToKen.encode())
        self.dT = self.dT.replace(b'4306245793de86da425a52caadf21eed', Access_Uid.encode())
        
        try:
            hex_data = self.dT.hex()
            encoded_data = EnC_AEs(hex_data)
            
            if not all(c in '0123456789abcdefABCDEF' for c in encoded_data):
                print("Invalid hex output from EnC_AEs, using alternative encoding")
                encoded_data = hex_data
            
            self.PaYload = bytes.fromhex(encoded_data)
        except Exception as e:
            print(f"Error in encoding: {e}")
            self.PaYload = self.dT
        
        self.ResPonse = requests.post(self.UrL, headers=self.HeadErs, data=self.PaYload, verify=False)        
        if self.ResPonse.status_code == 200 and len(self.ResPonse.text) > 10:
            try:
                self.BesTo_data = json.loads(DeCode_PackEt(self.ResPonse.content.hex()))
                self.JwT_ToKen = self.BesTo_data['8']['data']           
                self.combined_timestamp, self.key, self.iv = self.GeT_Key_Iv(self.ResPonse.content)
                ip, port, ip2, port2 = self.GeT_LoGin_PorTs(self.JwT_ToKen, self.PaYload)            
                return self.JwT_ToKen, self.key, self.iv, self.combined_timestamp, ip, port, ip2, port2
            except Exception as e:
                print(f"Error parsing response: {e}")
                time.sleep(5)
                return self.ToKen_GeneRaTe(Access_ToKen, Access_Uid)
        else:
            print(f"Error in ToKen_GeneRaTe, status: {self.ResPonse.status_code}")
            time.sleep(5)
            return self.ToKen_GeneRaTe(Access_ToKen, Access_Uid)
      
    def Get_FiNal_ToKen_0115(self):
        try:
            result = self.Guest_GeneRaTe(self.id, self.password)
            if not result:
                print("Failed to get tokens, retrying...")
                time.sleep(5)
                return self.Get_FiNal_ToKen_0115()
                
            token, key, iv, Timestamp, ip, port, ip2, port2 = result
            
            if not all([ip, port, ip2, port2]):
                print("Failed to get ports, retrying...")
                time.sleep(5)
                return self.Get_FiNal_ToKen_0115()
                
            self.JwT_ToKen = token        
            try:
                self.AfTer_DeC_JwT = jwt.decode(token, options={"verify_signature": False})
                self.AccounT_Uid = self.AfTer_DeC_JwT.get('account_id')
                self.EncoDed_AccounT = hex(self.AccounT_Uid)[2:]
                self.HeX_VaLue = DecodE_HeX(Timestamp)
                self.TimE_HEx = self.HeX_VaLue
                self.JwT_ToKen_ = token.encode().hex()
                print(f'Processing Uid : {self.AccounT_Uid}')
            except Exception as e:
                print(f"Error In Token : {e}")
                time.sleep(5)
                return self.Get_FiNal_ToKen_0115()
                
            try:
                self.Header = hex(len(EnC_PacKeT(self.JwT_ToKen_, key, iv)) // 2)[2:]
                length = len(self.EncoDed_AccounT)
                self.__ = '00000000'
                if length == 9:
                    self.__ = '0000000'
                elif length == 8:
                    self.__ = '00000000'
                elif length == 10:
                    self.__ = '000000'
                elif length == 7:
                    self.__ = '000000000'
                else:
                    print('Unexpected length encountered')                
                self.Header = f'0115{self.__}{self.EncoDed_AccounT}{self.TimE_HEx}00000{self.Header}'
                self.FiNal_ToKen_0115 = self.Header + EnC_PacKeT(self.JwT_ToKen_, key, iv)
            except Exception as e:
                print(f"Error In Final Token : {e}")
                time.sleep(5)
                return self.Get_FiNal_ToKen_0115()
                
            self.AutH_ToKen = self.FiNal_ToKen_0115
            self.Connect_SerVer(self.JwT_ToKen, self.AutH_ToKen, ip, port, key, iv, ip2, port2)        
            return self.AutH_ToKen, key, iv
            
        except Exception as e:
            print(f"Error in Get_FiNal_ToKen_0115: {e}")
            time.sleep(10)
            return self.Get_FiNal_ToKen_0115()

    def skwad_maker(self):
        fields = {
            1: 1,
            2: {
                2: "\u0001",
                3: 1,
                4: 1,
                5: "en",
                9: 1,
                11: 1,
                13: 1,
                14: {
                    2: 5756,
                    6: 11,
                    8: "1.109.5",
                    9: 3,
                    10: 2
                },
            }
        }

        packet = create_protobuf_packet(fields)
        packet = packet.hex()
        header_lenth = len(encrypt_packet(packet, self.key, self.iv))//2
        header_lenth_final = dec_to_hex(header_lenth)
        if len(header_lenth_final) == 2:
            final_packet = "0515000000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 3:
            final_packet = "051500000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 4:
            final_packet = "05150000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 5:
            final_packet = "0515000" + header_lenth_final + self.nmnmmmmn(packet)
        return bytes.fromhex(final_packet)

    def changes(self, num):
        fields = {
            1: 17,
            2: {
                1: 11371687918,
                2: 1,
                3: int(num),
                4: 62,
                5: "\u001a",
                8: 5,
                13: 329
            }
        }

        packet = create_protobuf_packet(fields)
        packet = packet.hex()
        header_lenth = len(encrypt_packet(packet, self.key, self.iv))//2
        header_lenth_final = dec_to_hex(header_lenth)
        if len(header_lenth_final) == 2:
            final_packet = "0515000000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 3:
            final_packet = "051500000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 4:
            final_packet = "05150000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 5:
            final_packet = "0515000" + header_lenth_final + self.nmnmmmmn(packet)
        return bytes.fromhex(final_packet)

    def start_autooo(self):
        fields = {
            1: 9,
            2: {
                1: 11371687918
            }
        }
        packet = create_protobuf_packet(fields)
        packet = packet.hex()
        header_lenth = len(encrypt_packet(packet, self.key, self.iv))//2
        header_lenth_final = dec_to_hex(header_lenth)
        if len(header_lenth_final) == 2:
            final_packet = "0515000000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 3:
            final_packet = "051500000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 4:
            final_packet = "05150000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 5:
            final_packet = "0515000" + header_lenth_final + self.nmnmmmmn(packet)
        return bytes.fromhex(final_packet)

    def invite_skwad(self, idplayer):
        fields = {
            1: 2,
            2: {
                1: int(idplayer),
                2: "ME",
                4: 1
            }
        }
        packet = create_protobuf_packet(fields)
        packet = packet.hex()
        header_lenth = len(encrypt_packet(packet, self.key, self.iv))//2
        header_lenth_final = dec_to_hex(header_lenth)
        if len(header_lenth_final) == 2:
            final_packet = "0515000000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 3:
            final_packet = "051500000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 4:
            final_packet = "05150000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 5:
            final_packet = "0515000" + header_lenth_final + self.nmnmmmmn(packet)
        return bytes.fromhex(final_packet)

    def leave_s(self):
        fields = {
            1: 7,
            2: {
                1: 11371687918
            }
        }

        packet = create_protobuf_packet(fields)
        packet = packet.hex()
        header_lenth = len(encrypt_packet(packet, self.key, self.iv))//2
        header_lenth_final = dec_to_hex(header_lenth)
        if len(header_lenth_final) == 2:
            final_packet = "0515000000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 3:
            final_packet = "051500000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 4:
            final_packet = "05150000" + header_lenth_final + self.nmnmmmmn(packet)
        elif len(header_lenth_final) == 5:
            final_packet = "0515000" + header_lenth_final + self.nmnmmmmn(packet)
        return bytes.fromhex(final_packet)

    def nmnmmmmn(self, data):
        if not self.key or not self.iv:
            return ""
        try:
            key = self.key if isinstance(self.key, bytes) else bytes.fromhex(self.key)
            iv = self.iv if isinstance(self.iv, bytes) else bytes.fromhex(self.iv)
            data = bytes.fromhex(data)
            cipher = AES.new(key, AES.MODE_CBC, iv)
            cipher_text = cipher.encrypt(pad(data, AES.block_size))
            return cipher_text.hex()
        except Exception as e:
            print(f"Error in nmnmmmmn: {e}")
            return ""

    def execute_invite_command(self, player_id, squad_type):
        try:
            if not hasattr(self, 'CliEnts2') or not self.CliEnts2:
                print(f"Account {self.id}: Not connected, cannot execute invite")
                return False

            numsc = int(squad_type) - 1
            
            packetmaker = self.skwad_maker()
            self.CliEnts2.send(packetmaker)
            time.sleep(0.5)
            
            packetfinal = self.changes(int(numsc))
            self.CliEnts2.send(packetfinal)
            time.sleep(0.5)
            
            invite_packet = self.invite_skwad(player_id)
            self.CliEnts2.send(invite_packet)
            
            time.sleep(5)
            
            leave_packet = self.leave_s()
            self.CliEnts2.send(leave_packet)
            time.sleep(0.5)
            
            solo_packet = self.changes(1)
            self.CliEnts2.send(solo_packet)
            
            print(f"Account {self.id}: Invite sent to player {player_id} (squad: {squad_type})")
            return True
            
        except Exception as e:
            print(f"Account {self.id}: Error in execute_invite_command: {e}")
            return False

    def execute_attack_command(self, team_code):
        try:
            if not hasattr(self, 'CliEnts2') or not self.CliEnts2:
                print(f"Account {self.id}: Not connected, cannot execute attack")
                return False

            start_packet = self.start_autooo()
            leave_packet = self.leave_s()

            print(f"Account {self.id}: Starting forced attack on team {team_code}")
            
            attack_start_time = time.time()
            while time.time() - attack_start_time < 45:
                try:
                    join_packet = GenJoinSquadsPacket(team_code, self.key, self.iv)
                    self.CliEnts2.send(join_packet)
                    self.CliEnts2.send(start_packet)
                    self.CliEnts2.send(leave_packet)
                    time.sleep(0.15)
                except:
                    pass

            print(f"Account {self.id}: Forced attack completed on team {team_code}")
            return True
            
        except Exception as e:
            print(f"Account {self.id}: Error in execute_attack_command: {e}")
            return False

def start_account(account):
    try:
        print(f"Starting account: {account['id']}")
        FF_CLient(account['id'], account['password'])
    except Exception as e:
        print(f"Error starting account {account['id']}: {e}")
        time.sleep(5)
        start_account(account)

def run_bot():
    print("Bot started...")
    bot.infinity_polling()

def StarT_SerVer():
    threads = []
    
    for account in ACCOUNTS:
        thread = threading.Thread(target=start_account, args=(account,))
        thread.daemon = True
        threads.append(thread)
        thread.start()
        time.sleep(1)
    
    time.sleep(3)
    init_account_queue()
    
    for thread in threads:
        thread.join()

if __name__ == "__main__":
    bot_thread = threading.Thread(target=run_bot, daemon=True)
    bot_thread.start()
    
    StarT_SerVer()