Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·1149 lines (1073 loc) · 63.2 KB
/
Copy pathindex.js
File metadata and controls
executable file
·1149 lines (1073 loc) · 63.2 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
#!/usr/bin/env node
/**
* DoggoReportBot - index.js - main file with all the commands.
* Copyright (C) 2023 Croluy
*
* This program comes with ABSOLUTELY NO WARRANTY;
* This is free software, and you are welcome to redistribute it under certain conditions;
* See https://www.gnu.org/licenses/ for details.
*/
require('dotenv').config();
const {Telegraf} = require('telegraf');
const editJsonFile = require("edit-json-file");
let blacklist = editJsonFile('./blacklist.json', {autosave: true});
let admins = editJsonFile('./admins.json', {autosave: true});
const User = require('./User');
const functions = require('./functions');
const fs = require("fs");
const path = require("path");
global.bot_test=true;
//get the id of the bot from its token
global.botID = process.env.BOT_TOKEN.toString().split(':')[0];
//set environment variables
global.adminID = process.env.CREATOR_ID; //ID of the creator
global.adminName = process.env.CREATOR_NAME; //Name && Username of the creator
global.LogChannel = process.env.LOG_CHANNEL; //Log Channel
global.channelName = process.env.CHANNEL_NAME; //Name of Channel linked to bot
global.privacy=false; //privacy variable for user forwarded messages
global.creator = new User(adminID,adminName,false,undefined,adminName,adminName,"EN",1,true,true,true,false,true);
global.current_user = new User();
const bot = new Telegraf(process.env.BOT_TOKEN);
const res = fs.readFileSync(path.resolve(__dirname, "BotReplies.json"));
const BotReplies = JSON.parse(res);
const {
index: {
strict_privacy_messsage,
__start: {
creator_start,
creator_no_username_start,
superior_start,
superior_no_username_start,
admin_start,
admin_no_username_start,
user_start,
user_no_username_start
},
__help: {
creator_help,
superior_help,
admin_help,
user_help
},
__commands: {
creator_commands,
superior_commands,
admin_commands,
user_commands
},
__credits,
__info: {
reply_to_dummy_info,
from_other_message_info,
on_bot_info,
no_message_info
},
__setusername: {
username_changed_setusername,
log_username_changed_setusername_nousername,
log_username_changed_setusername_nousername_creator,
log_username_changed_setusername_withusername,
log_username_changed_setusername_withusername_creator
},
__admin: {
banned_admin,
banned_no_username_admin,
already_admin,
creator_is_admin,
become_admin,
promoted_admin,
promoted_no_username_admin,
log: {
creator:{
c_username:{
c_promoted_username,
c_promoted_no_username
},
c_no_username:{
c_no_promoted_username,
c_no_promoted_no_username
}
},
superior:{
s_username:{
s_promoted_username,
s_promoted_no_username
},
s_no_username:{
s_no_promoted_username,
s_no_promoted_no_username
}
}
},
self_admin,
bot_admin,
no_user_admin,
argument_after_admin
},
__demote: {
not_admin_demote,
sup_and_sup_err_demote,
success_to_admin_demote,
success_to_admin_no_username_demote,
message_to_admin_demote,
success_to_user_demote,
success_to_user_no_username_demote,
message_to_user_demote,
couldnt_set_demote,
log:{
creator:{
to_admin:{
c_username:{
ca_demoted_username,
ca_demoted_no_username
},
c_no_username:{
ca_no_demoted_username,
ca_no_demoted_no_username
}
},
to_user:{
c_username:{
cu_demoted_username,
cu_demoted_no_username
},
c_no_username:{
cu_no_demoted_username,
cu_no_demoted_no_username
}
}
},
superior:{
s_username:{
s_demoted_username,
s_demoted_no_username
},
s_no_username:{
s_no_demoted_username,
s_no_demoted_no_username
}
}
},
self_demote,
bot_demote,
no_user_demote,
creator_demote
},
__ban: {
message_ban,
success_ban,
success_no_username_ban,
is_admin_ban,
is_admin_no_username_ban,
self_ban,
bot_ban,
no_user_ban,
argument_after_ban
},
__unban: {
no_banned_users_unban,
not_banned_unban,
success_unban,
success_no_username_unban,
message_unban,
self_unban,
bot_unban,
no_user_unban,
couldnt_set_unban
},
__adminlist: {
ask_creator_adminlist,
unable_adminlist
},
__blacklist: {
no_banned_users_blacklist
},
__promote: {
no_user_promote,
already_superior_promote,
not_admin_promote,
error_promote,
success_promote,
success_no_username_promote,
couldnt_set_promote,
message_promote
},
__resetadmins: {
only_one_resetadmins,
cleared_resetadmins
},
__hears: {
not_found_hears,
self_hears,
bot_hears,
no_message_hears
}
}
} = BotReplies;
//if it's not a test, then send classic log messages
if(!bot_test) functions.startup(bot);
//Bot start
bot.start((ctx) => {
functions.setUser(ctx);
if(functions.checkBanned(current_user)) return;
functions.update_admin(ctx,current_user);
if(functions.checkCreator(current_user)) {
//creator started the bot
if(creator.get_username != undefined || creator.get_username != null){
//has username set
ctx.reply(functions.s(BotReplies.index.__start.creator_start, {username: creator.get_username, channel: channelName}), {parse_mode: 'HTML'});
}else{
//doesn't have username set
ctx.reply(functions.s(BotReplies.index.__start.creator_no_username_start, {name: creator.get_fullname, channel: channelName}), {parse_mode: 'HTML'});
}
}else if(functions.checkSuperior(current_user)) {
//superior admin started the bot
if(current_user.get_username != undefined || current_user.get_username != null){
//has username set
ctx.reply(functions.s(BotReplies.index.__start.superior_start, {username: ctx.message.chat.username, channel: channelName}), {parse_mode: 'HTML'});
}else{
//doesn't have username set
ctx.reply(functions.s(BotReplies.index.__start.superior_no_username_start, {name: ctx.message.chat.firstName, channel: channelName}), {parse_mode: 'HTML'});
}
}else if(functions.checkAdmin(current_user)) {
//admin started the bot
if(current_user.get_username != undefined || current_user.get_username != null){
//has username set
ctx.reply(functions.s(BotReplies.index.__start.admin_start, {username: ctx.message.chat.username, channel: channelName}), {parse_mode: 'HTML'});
}else{
//doesn't have username set
ctx.reply(functions.s(BotReplies.index.__start.admin_no_username_start, {name: ctx.message.chat.firstName, channel: channelName}), {parse_mode: 'HTML'});
}
}else{
//normal user started the bot
if(current_user.get_username != undefined || current_user.get_username != null){
//has username set
ctx.reply(functions.s(BotReplies.index.__start.user_start, {username: ctx.message.chat.username, channel: channelName}), {parse_mode: 'HTML'});
}else{
//doesn't have username set
ctx.reply(functions.s(BotReplies.index.__start.user_no_username_start, {name: ctx.message.chat.firstName, channel: channelName}), {parse_mode: 'HTML'});
}
functions.toAdmin(ctx);
}
functions.clearUser();
});
//help command
bot.help((ctx) => {
functions.setUser(ctx);
if(functions.checkBanned(current_user)) return;
functions.update_admin(ctx,current_user);
if(functions.checkCreator(current_user)){
//creator help
ctx.reply(BotReplies.index.__help.creator_help,{parse_mode: 'HTML'});
}else if(functions.checkSuperior(current_user)) {
//superior admin help
ctx.reply(BotReplies.index.__help.superior_help,{parse_mode: 'HTML'});
functions.update_admin(ctx,current_user);
}else if(functions.checkAdmin(current_user)) {
//admin help
ctx.reply(BotReplies.index.__help.admin_help,{parse_mode: 'HTML'});
functions.update_admin(ctx,current_user);
}else{
//user help
ctx.reply(functions.s(BotReplies.index.__help.user_help, {channel: channelName}),{parse_mode: 'HTML'});
}
functions.clearUser();
});
//commands
bot.command('commands', (ctx) => {
functions.setUser(ctx);
if(functions.checkBanned(current_user)) return;
functions.update_admin(ctx,current_user);
if(functions.checkCreator(current_user)){
//creator commands
ctx.reply(BotReplies.index.__commands.creator_commands,{parse_mode: 'HTML'});
}else if(functions.checkSuperior(current_user)) {
//superior admin commands
ctx.reply(BotReplies.index.__commands.superior_commands,{parse_mode: 'HTML'});
functions.update_admin(ctx,current_user);
}else if(functions.checkAdmin(current_user)) {
//admin commands
ctx.reply(BotReplies.index.__commands.admin_commands,{parse_mode: 'HTML'});
functions.update_admin(ctx,current_user);
}else{
//user commands
ctx.reply(functions.s(BotReplies.index.__commands.user_commands, {channel: channelName}),{parse_mode: 'HTML'});
}
functions.clearUser();
});
//credits
bot.command('credits', (ctx) => {
functions.setUser(ctx);
if(functions.checkBanned(current_user)) return;
functions.update_admin(ctx,current_user);
ctx.reply(BotReplies.index.__credits,{parse_mode: 'HTML'});
functions.clearUser();
});
//Get info about user
bot.command('info', (ctx) => {
functions.setUser(ctx);
functions.update_admin(ctx,current_user);
if(functions.checkBanned(current_user)) return;
//only superior admins can use this command
if(functions.checkSuperiorId(ctx.message.chat.id)) {
//check if I am actually replying to someone
if(ctx.message.hasOwnProperty('reply_to_message')){
if(ctx.message.reply_to_message.hasOwnProperty('forward_from')){
//user has not limited privacy setting of forwarding, bot can know the original id of the forwarded message
ctx.telegram.sendMessage(ctx.message.chat.id, functions.infoCommand(ctx),{parse_mode: 'HTML'});
}else{
if(ctx.message.reply_to_message.forward_sender_name !== undefined){
//user has blocked the bot from sending his ID alongside forwarded messages
//admin tries to reply to the user message but it will NOT work
//admin has to reply to dummy message instead
ctx.reply(BotReplies.index.__info.reply_to_dummy_info);
}else if(ctx.message.reply_to_message.from.id == adminID){
//Admin uses info on himself
ctx.reply(functions.info(ctx));
}else if(ctx.message.reply_to_message.from.id==botID && /^👆/.test(ctx.message.reply_to_message.text)){
//admin tries to reply to bot - dummy message
//ctx.telegram.sendMessage(adminID,infoStrictCommand(ctx));
ctx.reply(BotReplies.index.__info.from_other_message_info);
}else{
//User uses comand on bot
ctx.reply(BotReplies.index.__info.on_bot_info);
}
}
}else{
//admin hasn't selected any message to check the user info
ctx.reply(BotReplies.index.__info.no_message_info);
}
}
functions.clearUser();
});
//set username for the channel - it's used to print it during the "/start" comand
bot.command('setusername', (ctx) => {
functions.setUser(ctx);
functions.update_admin(ctx,current_user);
if(functions.checkBanned(current_user)) return;
if(functions.checkSuperior(current_user)) {
let input=ctx.message.text;
let inputArray=input.split(' ');
inputArray.shift();
input=inputArray.join(' ');
if(input[0]=="@") input.shift(); //User started username with @, so I remove that
channelName=input;
ctx.reply(functions.s(BotReplies.index.__setusername.username_changed_setusername, {channel: channelName}));
//Log action on the log channel
if(current_user.username !== undefined){
//User has a username
if(current_user.id !== adminID) //Admin is not creator
functions.log(ctx,functions.s(BotReplies.index.__setusername.log_username_changed_setusername_withusername,{admin_fullname: current_user.fullName, username: current_user.username, id: current_user.id, channel: channelName}));
else //Admin is creator
functions.log(ctx,functions.s(BotReplies.index.__setusername.log_username_changed_setusername_withusername_creator,{admin_fullname: current_user.fullName, username: current_user.username, id: current_user.id, channel: channelName}));
}else{
//User does not have a username
if(current_user.id !== adminID) //Admin is not creator
functions.log(ctx,functions.s(BotReplies.index.__setusername.log_username_changed_setusername_nousername,{admin_fullname: current_user.fullName, id: current_user.id, channel: channelName}));
else //Admin is creator
functions.log(ctx,functions.s(BotReplies.index.__setusername.log_username_changed_setusername_nousername_creator,{admin_fullname: current_user.fullName, id: current_user.id, channel: channelName}));
}
}
functions.clearUser();
});
//Make user into admin
bot.command(['admin'], (ctx) => {
// Reload file from the disk
admins = editJsonFile(`./admins.json`, {autosave: true});
//only superior admins can add an admin
if(functions.checkSuperiorId(ctx.message.chat.id)) {
functions.setUser(ctx);
//FIXME:current_user is the user you reply to, so I can't update admin with current_user
//console.log(JSON.stringify(current_user));
//functions.update_admin(ctx,current_user);
if(functions.checkBanned(current_user)){
//User can NOT be promoted, he is currently banned.. have to unban first
if(current_user.get_username != undefined && current_user.get_username != "") //user has username
ctx.reply(functions.s(BotReplies.index.__admin.banned_admin, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id, id: current_user.get_id}),{parse_mode: 'HTML'});
else //user doesn't have an username
ctx.reply(functions.s(BotReplies.index.__admin.banned_no_username_admin, {name: current_user.get_fullName, id: current_user.get_id, id: current_user.get_id}),{parse_mode: 'HTML'});
return;
}
//check if username with userId is an admin and has replied to someone and isn't a bot
if(functions.checkAdmin(current_user) && !current_user.get_isBot && current_user.get_id!=creator.get_id) ctx.reply(BotReplies.index.__admin.already_admin);
else if(current_user.get_id==creator.get_id) ctx.reply(BotReplies.index.__admin.creator_is_admin);
else{
//Make sure only bot creator can use this command
//also make sure the user isn't already Admin
let input=ctx.message.text;
let inputArray=input.split(' ');
inputArray.shift();
input=inputArray.join(' ');
if(input[0]=="@") input.shift(); //User started username with @, so I remove that
const UserID=input;
const m=BotReplies.index.__admin.become_admin;
//+'\n\n\nNow that you\'ve joined the admins, you should provide us with your timezone.\n'
//+'So that we can show you your correct local time when it\'s needed.\n'
//+'Just type /timezone in chat.\n'
//+'NOTICE: You are free to skip this step if you aren\'t interested. ✌🏻';
if(UserID=="") {
//There is no text after the command
//check if I am actually replying to someone
if(ctx.message.hasOwnProperty('reply_to_message')){
//success condition - admin replied to user message or bot message if the user had privacy setting up
if(ctx.message.reply_to_message.hasOwnProperty('forward_from') || (ctx.message.reply_to_message.from.id==botID && /^👆/.test(ctx.message.reply_to_message.text))){
//user has not limited privacy setting of forwarding, bot can know the original id of the forwarded message
//OR
//user has limited privacy and I can only print up a little amount of info
current_user.set_isAdmin=true;
if(functions.add_AdminToFile(current_user,ctx.message.date)){
//User can be promoted, he is not banned
//FIXME: bug, user in admins.json "isAdmin" field isn't set to true
if(current_user.get_username != undefined && current_user.get_username != ""){ //user to promote has username
ctx.reply(functions.s(BotReplies.index.__admin.promoted_admin, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id}),{parse_mode: 'HTML'});
if(ctx.message.from.id==creator.get_id){ //Creator executed command
if(ctx.message.from.username != undefined && ctx.message.from.username != "") //creator has username
functions.log(ctx,functions.s(BotReplies.index.__admin.log.creator.c_username.c_promoted_username, {name: creator.fullName, username: creator.username, id: creator.id, promoted_user: current_user.get_fullName, username_promoted: current_user.username, id_promoted: current_user.get_id}));
else //creator doesn't have an username
functions.log(ctx,functions.s(BotReplies.index.__admin.log.creator.c_no_username.c_no_promoted_username, {name: creator.fullName, id: creator.id, promoted_user: current_user.get_fullName, username_promoted: current_user.username, id_promoted: current_user.get_id}));
}else{ //Admin executed command
const sup_full_name=ctx.message.from.first_name+" "+ctx.message.from.last_name;
if(ctx.message.from.username != undefined && ctx.message.from.username != "") //admin has username
functions.log(ctx,functions.s(BotReplies.index.__admin.log.superior.s_username.s_promoted_username, {name: sup_full_name, username: ctx.message.from.username, id: ctx.message.from.id, promoted_user: current_user.get_fullName, username_promoted: current_user.username, id_promoted: current_user.get_id}));
else //admin doesn't have an username
functions.log(ctx,functions.s(BotReplies.index.__admin.log.superior.s_no_username.s_no_promoted_username, {name: sup_full_name, id: ctx.message.from.id, promoted_user: current_user.get_fullName, username_promoted: current_user.username, id_promoted: current_user.get_id}));
}
}else{ //user doesn't have an username
ctx.reply(functions.s(BotReplies.index.__admin.promoted_no_username_admin, {name: current_user.get_fullName, id: current_user.get_id}),{parse_mode: 'HTML'});
if(ctx.message.from.id==creator.get_id){ //Creator executed command
if(ctx.message.from.username != undefined && ctx.message.from.username != "") //creator has username
functions.log(ctx,functions.s(BotReplies.index.__admin.log.creator.c_username.c_promoted_no_username, {name: creator.fullName, username: creator.username, id: creator.get_id, promoted_user: current_user.get_fullName, id_promoted: current_user.get_id}));
else //creator doesn't have an username
functions.log(ctx,functions.s(BotReplies.index.__admin.log.creator.c_no_username.c_no_promoted_no_username, {name: creator.fullName, id: creator.get_id, promoted_user: current_user.get_fullName, id_promoted: current_user.get_id}));
}else{ //Admin executed command
const sup_full_name=ctx.message.from.first_name+" "+ctx.message.from.last_name;
if(ctx.message.from.username != undefined && ctx.message.from.username != "") //admin has username
functions.log(ctx,functions.s(BotReplies.index.__admin.log.superior.s_username.s_promoted_no_username, {name: sup_full_name, username: ctx.message.from.username, id: ctx.message.from.id, promoted_user: current_user.get_fullName, id_promoted: current_user.get_id}));
else //admin doesn't have an username
functions.log(ctx,functions.s(BotReplies.index.__admin.log.superior.s_no_username.s_no_promoted_no_username, {name: sup_full_name, id: ctx.message.from.id, promoted_user: current_user.get_fullName, id_promoted: current_user.get_id}));
}
}
ctx.telegram.sendMessage(current_user.get_id,m); //tell user of the admin privileges
}else if(functions.add_AdminToFile(current_user,ctx.message.date) == -1){
//User can NOT be promoted, he is currently banned.. have to unban first
if(current_user.get_username != undefined && current_user.get_username != "") //user has username
ctx.reply(functions.s(BotReplies.index.__admin.banned_admin, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id, id: current_user.get_id}),{parse_mode: 'HTML'});
else //user doesn't have an username
ctx.reply(functions.s(BotReplies.index.__admin.banned_no_username_admin, {name: current_user.get_fullName, id: current_user.get_id, id: current_user.get_id}),{parse_mode: 'HTML'});
}
}else{
//errors
if(ctx.message.reply_to_message.hasOwnProperty('forward_sender_name')){
//user has blocked the bot from sending his ID alongside forwarded messages
//admin tries to reply to the user message but it will NOT work
ctx.reply(BotReplies.index.strict_privacy_messsage);
}else if(ctx.message.reply_to_message.from.id==adminID){
//admin tries to reply to its own message
ctx.reply(BotReplies.index.__admin.self_admin);
}else{
//admin tries to reply to bot - no important message
ctx.reply(BotReplies.index.__admin.bot_admin);
}
}
}else{
//admin hasn't selected any message to reply to
ctx.reply(BotReplies.index.__admin.no_user_admin);
}
}else{
//admin has written something after the admin command
ctx.reply(BotReplies.index.__admin.argument_after_admin);
}
}
functions.clearUser();
}
});
//Demote an user from admin
bot.command(['unadmin','demote'], (ctx) => {
// Reload file from the disk
admins = editJsonFile(`./admins.json`, {autosave: true});
//only superior admins can demote an admin
if(functions.checkSuperiorId(ctx.message.chat.id)) {
functions.setUser(ctx);
//If the admin is replying to a message the user will be the message he replies to, otherwise if he is demoting using id, the user will be the admin
//functions.update_admin(ctx,current_user);
if(functions.checkBanned(current_user)) return;
//check if user is creator and isn't a bot
if(functions.checkAdmin(current_user) && !current_user.get_isBot) {
let input=ctx.message.text;
let inputArray=input.split(' ');
inputArray.shift();
input=inputArray.join(' ');
if(input[0]=="@") input.shift(); //User started username with @, so I remove that
const UserID=input;
//check if the user who exectues command is creator
let is_creator=false;
if(ctx.message.from.id==creator.get_id) is_creator=true;
if(UserID=="") {
//There is no text after the command
//check if I am actually replying to someone
if(ctx.message.hasOwnProperty('reply_to_message')){
//current_user is the user to which the admin is replying to
//success condition - admin replied to user message or bot message if the user had privacy setting up
if(ctx.message.reply_to_message.hasOwnProperty('forward_from') || (ctx.message.reply_to_message.from.id==botID && /^👆/.test(ctx.message.reply_to_message.text))){
//user has not limited privacy setting of forwarding, bot can know the original id of the forwarded message
//OR
//user has limited privacy and I can only print up a little amount of info
switch(functions.demote_AdminToFile(current_user.get_id,is_creator)){
case -1000:
//ERROR: User is not admin, can't be demoted
ctx.reply(BotReplies.index.__demote.not_admin_demote);
break;
case -1005:
//ERROR: Loop ended without finding a match to demote
ctx.reply(BotReplies.index.__demote.not_admin_demote);
break;
case -1010:
//ERROR: Superior Admin tries to demote another Superior Admin
ctx.reply(BotReplies.index.__demote.sup_and_sup_err_demote);
break;
case -1:
//SUCCESS: Superior Admin has been successfully demoted to admin role
//Assume the admin who executed the command is the creator, because only creator can demote superior admins
if(current_user.get_username != undefined && current_user.get_username != ""){ //demoted user has username
ctx.reply(functions.s(BotReplies.index.__demote.success_to_admin_demote, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id}),{parse_mode: 'HTML'});
if(ctx.message.from.hasOwnProperty('username')) //creator has username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.creator.to_admin.c_username.ca_demoted_username, {name: creator.get_fullName, username: creator.get_username, id: creator.get_id, demoted_user: current_user.get_fullName, username_demoted: current_user.get_username, id_demoted: current_user.get_id}));
else //creator doesn't have username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.creator.to_admin.c_no_username.ca_no_demoted_username, {name: creator.get_fullName, id: creator.get_id, demoted_user: current_user.get_fullName, username_demoted: current_user.get_username, id_demoted: current_user.get_id}));
}else{ //demoted user doesn't have username
ctx.reply(functions.s(BotReplies.index.__demote.success_to_admin_no_username_demote, {name: current_user.get_fullName, id: current_user.get_id}),{parse_mode: 'HTML'});
if(ctx.message.from.hasOwnProperty('username')) //creator has username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.creator.to_admin.c_username.ca_demoted_no_username, {name: creator.get_fullName, username: creator.get_username, id: creator.get_id, demoted_user: current_user.get_fullName, id_demoted: current_user.get_id}));
else //creator doesn't have username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.creator.to_admin.c_no_username.ca_no_demoted_no_username, {name: creator.get_fullName, id: creator.get_id, demoted_user: current_user.get_fullName, id_demoted: current_user.get_id}));
}
ctx.telegram.sendMessage(current_user.get_id,BotReplies.index.__demote.message_to_admin_demote); //send message to demoted user
current_user.set_isSuperior=false;
break;
case 1:
//SUCCESS: Admin has been successfully demoted to user role
//Both creator and any superior admin can demote an admin to user, check if the admin who executed the command is the creator
if(ctx.message.from.id==creator.get_id){
//creator executed the command
if(current_user.get_username != undefined && current_user.get_username != ""){ //demoted user has username
ctx.reply(functions.s(BotReplies.index.__demote.success_to_user_demote, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id}),{parse_mode: 'HTML'});
if(ctx.message.from.hasOwnProperty('username')) //creator has username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.creator.to_user.c_username.cu_demoted_username, {name: creator.get_fullName, username: creator.get_username, id: creator.get_id, demoted_user: current_user.get_fullName, username_demoted: current_user.get_username, id_demoted: current_user.get_id}));
else //creator doesn't have username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.creator.to_user.c_no_username.cu_no_demoted_username, {name: creator.get_fullName, id: creator.get_id, demoted_user: current_user.get_fullName, username_demoted: current_user.get_username, id_demoted: current_user.get_id}));
}else{ //demoted user doesn't have username
ctx.reply(functions.s(BotReplies.index.__demote.success_to_user_no_username_demote, {name: current_user.get_fullName, id: current_user.get_id}),{parse_mode: 'HTML'});
if(ctx.message.from.hasOwnProperty('username')) //creator has username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.creator.to_user.c_username.cu_demoted_no_username, {name: creator.get_fullName, username: creator.get_username, id: creator.get_id, demoted_user: current_user.get_fullName, id_demoted: current_user.get_id}));
else //creator doesn't have username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.creator.to_user.c_no_username.cu_no_demoted_no_username, {name: creator.get_fullName, id: creator.get_id, demoted_user: current_user.get_fullName, id_demoted: current_user.get_id}));
}
}else{
//superior admin executed the command
if(current_user.get_username != undefined && current_user.get_username != ""){ //demoted user has username
ctx.reply(functions.s(BotReplies.index.__demote.success_to_user_demote, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id}),{parse_mode: 'HTML'});
if(ctx.message.from.hasOwnProperty('username')) //superior admin has username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.superior.s_username.su_demoted_username, {name: superior.get_fullName, username: superior.get_username, id: superior.get_id, demoted_user: current_user.get_fullName, username_demoted: current_user.get_username, id_demoted: current_user.get_id}));
else //superior admin doesn't have username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.superior.s_no_username.su_no_demoted_username, {name: superior.get_fullName, id: superior.get_id, demoted_user: current_user.get_fullName, username_demoted: current_user.get_username, id_demoted: current_user.get_id}));
}else{ //demoted user doesn't have username
ctx.reply(functions.s(BotReplies.index.__demote.success_to_user_no_username_demote, {name: current_user.get_fullName, id: current_user.get_id}),{parse_mode: 'HTML'});
if(ctx.message.from.hasOwnProperty('username')) //superior admin has username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.superior.s_username.su_demoted_no_username, {name: superior.get_fullName, username: superior.get_username, id: superior.get_id, demoted_user: current_user.get_fullName, id_demoted: current_user.get_id}));
else //superior admin doesn't have username
functions.log(ctx,functions.s(BotReplies.index.__demote.log.superior.s_no_username.su_no_demoted_no_username, {name: superior.get_fullName, id: superior.get_id, demoted_user: current_user.get_fullName, id_demoted: current_user.get_id}));
}
}
const m=BotReplies.index.__demote.message_to_user_demote;
ctx.telegram.sendMessage(current_user.get_id,m);
current_user.set_isAdmin=false;
break;
}
}else{
//errors
if(ctx.message.reply_to_message.hasOwnProperty('forward_sender_name')){
//user has blocked the bot from sending his ID alongside forwarded messages
//admin tries to reply to the user message but it will NOT work
ctx.reply(BotReplies.index.strict_privacy_messsage);
}else if(ctx.message.reply_to_message.from.id==adminID){
//admin tries to reply to its own message
ctx.reply(BotReplies.index.__demote.self_demote);
}else{
//admin tries to reply to bot - no important message
ctx.reply(BotReplies.index.__demote.bot_demote);
}
}
}else{
//admin hasn't selected any message to reply to
ctx.reply(BotReplies.index.__demote.no_user_demote);
}
}else{
//set the user before removing it from the admin list
let user_set=false;
if(functions.setAdminUser(UserID)) user_set=true;
//admin has written something (ID of the user) after the admin command
//current_user is the admin who executes the command
switch(functions.demote_AdminToFile(UserID,is_creator)){
case -1000:
//ERROR: User is not admin, can't be demoted
ctx.reply(BotReplies.index.__demote.not_admin_demote);
break;
case -1005:
//ERROR: Loop ended without finding a match to demote
ctx.reply(BotReplies.index.__demote.not_admin_demote);
break;
case -1010:
//ERROR: Superior Admin tries to demote another Superior Admin
ctx.reply(BotReplies.index.__demote.sup_and_sup_err_demote);
break;
case -1:
//SUCCESS: User has been successfully demoted back to admin role
//TODO: send log messages
if(functions.setAdminUser(UserID)){
//User has been set successfully
if(current_user.get_username != undefined && current_user.get_username != "")
ctx.reply(functions.s(BotReplies.index.__demote.success_to_admin_demote, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id}),{parse_mode: 'HTML'});
else
ctx.reply(functions.s(BotReplies.index.__demote.success_to_admin_no_username_demote, {name: current_user.get_fullName, id: current_user.get_id}),{parse_mode: 'HTML'});
current_user.set_isSuperior=false;
}else{
//User couldn't be set correctly
ctx.reply(functions.s(BotReplies.index.__demote.couldnt_set_demote, {id: UserID}),{parse_mode: 'HTML'});
}
const mm=BotReplies.index.__demote.message_to_admin_demote;
ctx.telegram.sendMessage(UserID,mm);
break;
case 1:
//SUCCESS: User has been successfully demoted back to user role
if(user_set){
//User has been set successfully
if(current_user.get_username != undefined && current_user.get_username != "")
ctx.reply(functions.s(BotReplies.index.__demote.success_to_user_demote, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id}),{parse_mode: 'HTML'});
else
ctx.reply(functions.s(BotReplies.index.__demote.success_to_user_no_username_demote, {name: current_user.get_fullName, id: current_user.get_id}),{parse_mode: 'HTML'});
current_user.set_isAdmin=false;
}else{
//User couldn't be set correctly
ctx.reply(functions.s(BotReplies.index.__demote.couldnt_set_demote, {id: UserID}),{parse_mode: 'HTML'});
}
const m=BotReplies.index.__demote.message_to_user_demote;
ctx.telegram.sendMessage(UserID,m);
break;
}
}
}
else if(current_user.get_id==creator.get_id) ctx.reply(BotReplies.index.__demote.self_demote);
else{
//User is not admin, he can't be demoted from admin role
ctx.reply(BotReplies.index.__demote.not_admin_demote);
}
functions.clearUser();
}
});
//ban user from the bot, impeding him to write to admins
bot.command(['ban','terminate'], (ctx) => {
// Reload file from the disk
banned = editJsonFile(`./blacklist.json`, {autosave: true});
//check if the person who sent the command is admin
if(functions.checkAdminId(ctx.message.chat.id)) {
functions.setUser(ctx);
functions.update_admin(ctx,current_user);
if(functions.checkBanned(current_user)) return;
let input=ctx.message.text;
let inputArray=input.split(' ');
inputArray.shift();
input=inputArray.join(' ');
const m=BotReplies.index.__ban.message_ban;
if(input==""){
//there is no parameter/id specified
//There is no text after the command
//check if I am actually replying to someone
if(ctx.message.hasOwnProperty('reply_to_message')){
//success condition - admin replied to user message or bot message if the user had privacy setting up
if(ctx.message.reply_to_message.hasOwnProperty('forward_from') || (ctx.message.reply_to_message.from.id==botID && /^👆/.test(ctx.message.reply_to_message.text))){
//user has not limited privacy setting of forwarding, bot can know the original id of the forwarded message
//OR
//user has limited privacy and I can only print up a little amount of info
if(functions.add_BannedToFile(current_user,ctx.message.date) == 1){
//User can be banned, he is not admin
current_user.set_isBan=true;
if(current_user.get_username != undefined && current_user.get_username != "") //user has username
ctx.reply(functions.s(BotReplies.index.__ban.success_ban, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id}), {parse_mode: 'HTML'});
else //user doesn't have an username
ctx.reply(functions.s(BotReplies.index.__ban.success_no_username_ban, {name: current_user.get_fullName, id: current_user.get_id}), {parse_mode: 'HTML'});
ctx.telegram.sendMessage(current_user.get_id,m);
}else if(functions.add_BannedToFile(current_user,ctx.message.date) == -1){
//User can NOT be banned, he is currently admin.. have to unadmin first
if(current_user.get_username != undefined && current_user.get_username != "") //user has username
ctx.reply(functions.s(BotReplies.index.__ban.is_admin_ban, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id}),{parse_mode: 'HTML'});
else //user doesn't have an username
ctx.reply(functions.s(BotReplies.index.__ban.is_admin_no_username_ban, {name: current_user.get_fullName, id: current_user.get_id}),{parse_mode: 'HTML'});
}
}else{
//errors
if(ctx.message.reply_to_message.hasOwnProperty('forward_sender_name')){
//user has blocked the bot from sending his ID alongside forwarded messages
//admin tries to reply to the user message but it will NOT work
ctx.reply(BotReplies.index.strict_privacy_messsage);
}else if(ctx.message.reply_to_message.from.id==adminID){
//admin tries to reply to its own message
ctx.reply(BotReplies.index.__ban.self_ban);
}else{
//admin tries to reply to bot - no important message
ctx.reply(BotReplies.index.__ban.bot_ban);
}
}
}else{
//admin hasn't selected any message to reply to
ctx.reply(BotReplies.index.__ban.no_user_ban);
}
}else{
ctx.reply(BotReplies.index.__ban.argument_after_ban);
}
functions.clearUser();
}
});
//Unban user
bot.command('unban', (ctx) => {
// Reload file from the disk
banned = editJsonFile(`./blacklist.json`, {autosave: true});
//only admin can unban users
if(functions.checkAdminId(ctx.message.chat.id)) {
functions.setUser(ctx);
functions.update_admin(ctx,current_user);
//check if user is admin and isn't a bot
//if(functions.checkBanned(current_user)) {
let input=ctx.message.text;
let inputArray=input.split(' ');
inputArray.shift();
input=inputArray.join(' ');
if(input[0]=="@") input.shift(); //User started username with @, so I remove that
const UserID=input;
if(UserID=="") {
//There is no text after the command
//check if I am actually replying to someone
if(ctx.message.hasOwnProperty('reply_to_message')){
//success condition - admin replied to user message or bot message if the user had privacy setting up
if(ctx.message.reply_to_message.hasOwnProperty('forward_from') || (ctx.message.reply_to_message.from.id==botID && /^👆/.test(ctx.message.reply_to_message.text))){
//user has not limited privacy setting of forwarding, bot can know the original id of the forwarded message
//OR
//user has limited privacy and I can only print up a little amount of info
switch(functions.remove_BannedFromFile(current_user.get_id)){
case -1000:
//ERROR: There is no banned user
ctx.reply(BotReplies.index.__unban.no_banned_users_unban);
break;
case -1005:
//ERROR: Loop ended without finding a match to unban
ctx.reply(BotReplies.index.__unban.not_banned_unban);
break;
case 1:
//SUCCESS: User has been successfully unbanned
if(current_user.get_username != undefined && current_user.get_username != "")
ctx.reply(functions.s(BotReplies.index.__unban.success_unban, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id}),{parse_mode: 'HTML'});
else
ctx.reply(functions.s(BotReplies.index.__unban.success_no_username_unban, {name: current_user.get_fullName, id: current_user.get_id}),{parse_mode: 'HTML'});
const mm=BotReplies.index.__unban.message_unban;
ctx.telegram.sendMessage(current_user.get_id,mm);
break;
}
current_user.set_isBan=false;
}else{
//errors
if(ctx.message.reply_to_message.hasOwnProperty('forward_sender_name')){
//user has blocked the bot from sending his ID alongside forwarded messages
//admin tries to reply to the user message but it will NOT work
ctx.reply(BotReplies.index.strict_privacy_messsage);
}else if(ctx.message.reply_to_message.from.id==adminID){
//admin tries to reply to its own message
ctx.reply(BotReplies.index.__unban.self_unban);
}else{
//admin tries to unban the bot
ctx.reply(BotReplies.index.__unban.bot_unban);
}
}
}else{
//admin hasn't selected any message to reply to
ctx.reply(BotReplies.index.__unban.no_user_unban);
}
}else{
//admin has written something after the unban command
//set the user before removing it from the banned list
let user_set=false;
if(functions.setBannedUser(UserID)) user_set=true;
switch(functions.remove_BannedFromFile(UserID)){
case -1000:
//ERROR: User is not banned, can't be unbanned
ctx.reply(BotReplies.index.__unban.not_banned_unban);
break;
case -1005:
//ERROR: Loop ended without finding a match to demote
ctx.reply(BotReplies.index.__unban.not_banned_unban);
break;
case 1:
//SUCCESS: User has been successfully demoted back to user role
if(user_set){
//User has been set successfully
if(current_user.get_username != undefined && current_user.get_username != "")
ctx.reply(functions.s(BotReplies.index.__unban.success_unban, {name: current_user.get_fullName, username: current_user.get_username, id: current_user.get_id}),{parse_mode: 'HTML'});
else
ctx.reply(functions.s(BotReplies.index.__unban.success_no_username_unban, {name: current_user.get_fullName, id: current_user.get_id}),{parse_mode: 'HTML'});
current_user.set_isBan=false;
}else{
//User couldn't be set correctly
ctx.reply(functions.s(BotReplies.index.__unban.couldnt_set_unban, {id: UserID}),{parse_mode: 'HTML'});
}
const m=BotReplies.index.__unban.message_unban;
ctx.telegram.sendMessage(UserID,m);
break;
}
}
//}
//else if(current_user.get_id==creator.get_id) ctx.reply('You can\'t unban yourself! ✌🏻\nBut you already knew that, since you developed me. 💡');
//else{
// //User is not banned, he can't be unbanned
// ctx.reply('The user is not banned so he can\'t be unbanned. 👀');
//}
functions.clearUser();
}
});
//list admin users
bot.command('adminlist', (ctx) => {
// Reload file from the disk
admins = editJsonFile(`./admins.json`, {autosave: true});
functions.setUser(ctx);
functions.update_admin(ctx,current_user);
if(functions.checkBanned(current_user)) return;
if(functions.checkSuperior(current_user)) {
//only superior admins can use this command
functions.adminsToMessage(ctx);
/**OLD MODE //const list=JSON.stringify(admins.toObject(),null,'\t\t');
//ctx.reply(list);*/
}else if(functions.checkAdmin(current_user)){
//normal admins
const m=functions.s(BotReplies.index.__adminlist.ask_creator_adminlist, {name: current_user.get_fullName, id: current_user.get_id});
//only creator can grant Superior permissions to an admin
bot.sendMessage(creator.get_id,m,{parse_mode: 'HTML'});
//Warn the user that he isn't allowed to use this command yet, but a request has been done to the creator
ctx.reply(BotReplies.index.__adminlist.unable_adminlist);
}
functions.clearUser();
});
//list banned users
bot.command('blacklist', (ctx) => {
// Reload file from the disk
banned = editJsonFile(`./blacklist.json`, {autosave: true});
functions.setUser(ctx);
functions.update_admin(ctx,current_user);
//if the user who wrote the command is banned from the bot, ignore message
if(functions.checkBanned(current_user)){
functions.clearUser();
return;
}
if(functions.checkAdmin(current_user)) {
if(functions.existsBannedUsers()){
ctx.reply(BotReplies.index.__blacklist.no_banned_users_blacklist);
functions.clearUser();
return;
}
//only admins can use this command
functions.bannedToMessage(ctx);
/*OLD MODE//blacklist = editJsonFile('./blacklist.json');
//const list=JSON.stringify(blacklist.toObject(),null,'\t\t');
//ctx.reply(list);*/
}
functions.clearUser();
});
//grant an admin the superior admin permissions
bot.command(['promote','superior'], (ctx) => {
// Reload file from the disk
admins = editJsonFile(`./admins.json`, {autosave: true});
functions.setUser(ctx);
functions.update_admin(ctx,current_user);
if(functions.checkBanned(current_user)) return;
if(functions.checkCreator(current_user)) {
//only creator of the bot can use the superior command
let input=ctx.message.text;
let inputArray=input.split(' ');
inputArray.shift();
input=inputArray.join(' ');
const id=input;
if(id==undefined || id==null || id=='') {
//creator has not specified an ID to promote
ctx.reply(BotReplies.index.__promote.no_user_promote);
}else if(functions.checkAdminId(id)){
//User is admin already, so he can be promoted
if(functions.checkSuperiorId(id)){
//User is already superior admin
ctx.reply(functions.s(BotReplies.index.__promote.already_superior_promote, {idd: id}),{parse_mode: 'HTML'});
}else{
//User is admin, but not superior
switch(functions.setSuperiorId(id)){
case -1000:
//ERROR: There is no admin other than creator, user can't be promoted
ctx.reply(BotReplies.index.__promote.not_admin_promote);
break;
case -1005:
//ERROR: Loop has ended without a match
ctx.reply(functions.s(BotReplies.index.__promote.error_promote, {idd: id}),{parse_mode: 'HTML'});
break;
case 1:
//SUCCESS: User has been promoted to superior admin.. tell the user that he has been promoted
if(functions.setAdminUser(id)){
//User was set successfully
if(current_user.username==null || current_user.username==undefined) ctx.reply(functions.s(BotReplies.index.__promote.success_no_username_promote, {name: current_user.get_fullName, idd: id}));
else ctx.reply(functions.s(BotReplies.index.__promote.success_promote, {name: current_user.get_fullName, username: current_user.get_username, idd: id}));
}else{
//User couldn't be set
ctx.reply(functions.s(BotReplies.index.__promote.couldnt_set_promote, {idd: id}));
}
const m=BotReplies.index.__promote.message_promote;
ctx.telegram.sendMessage(id,m);
break;
}
}
}else{
//User is not admin, can't be superior
ctx.reply(BotReplies.index.__promote.not_admin_promote);