-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutilidades.lua
More file actions
executable file
·1233 lines (1077 loc) · 33.2 KB
/
utilidades.lua
File metadata and controls
executable file
·1233 lines (1077 loc) · 33.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
function owners(msg)
local var = false
for v,owners in pairs(config.owners) do
if owners == msg.from.id then
var = true
end
if owners == tonumber(id) then
var = true
end
end
return var
end
function owners_listed(user_id)
local var = false
for v,owners in pairs(config.owners) do
if user_id == owners then
var = true
end
end
return var
end
function user_gbanned(msg)
local var = false
for v,users in pairs(gbans.gbans) do
if msg.from.id == users then
var = true
if msg.from.username then
print("Usuario globalmente baneado ("..msg.from.id..")", msg.from.first_name.."(@"..msg.from.username..")")
else
print("Usuario globalmente baneado ("..msg.from.id..")", msg.from.first_name)
end
end
end
return var
end
function user_in_gbans(msg)
local var = false
for v,users in pairs(gbans.gbans) do
if msg.added.id == users then
var = true
if msg.added.username then
print("Usuario globalmente baneado ("..msg.added.id..")", msg.added.first_name.."(@"..msg.added.username..")")
else
print("Usuario globalmente baneado ("..msg.added.id..")", msg.added.first_name)
end
end
end
return var
end
-- Plugins
function disable_plugin(msg, blocks)
local var = false
for k,disable in pairs(config.plugins) do
plugin = disable:gsub('.lua', '')
if blocks[3] == plugin then
os.execute('sed -i "/'..config.plugins[k]..'/d" ./config.lua')
var = true
end
end
return var
end
function plugin_exist(msg, blocks)
local var = false
for k,exist in pairs(config.plugins) do
plugin = exist:gsub('.lua', '')
if blocks[3] == plugin then
var = true
end
end
return var
end
function enable_plugin(msg, blocks)
local var = false
plugin_existing = io.open("./plugins/"..blocks[3]..".lua","r")
if plugin_existing then
if plugin_exist(msg, blocks) == false then
os.execute('perl -pi -e "s[plugins = \\{][plugins = \\{\n\t\t\\"'..blocks[3]..'.lua\\",]g" ./config.lua')
var = true
end
end
return var
end
function load_plugins()
local text = ''
for k,v in pairs(config.plugins) do
if (v:match(".lua$")) then
list = v:gsub('.lua', '')
text = text.."► "..list..'\n'
end
end
return text
end
function get_word(s, i) -- get the indexed word in a string
s = s or ''
i = i or 1
local t = {}
for w in s:gmatch('%g+') do
table.insert(t, w)
end
return t[i] or false
end
function string:input() -- Returns the string after the first space.
if not self:find(' ') then
return false
end
return self:sub(self:find(' ')+1)
end
function string:mEscape() -- Remove the markdown.
self = self:gsub('*', '\\*'):gsub('_', '\\_'):gsub('`', '\\`'):gsub('%]', '\\]'):gsub('%[', '\\[')
return self
end
function string:mEscape_hard() -- Remove the markdown.
self = self:gsub('*', ''):gsub('_', ''):gsub('`', ''):gsub('%[', ''):gsub('%]', '')
return self
end
function is_admin(msg)
local id
if msg.adder and msg.adder.id then
id = msg.adder.id
else
id = msg.from.id
end
if id and owners(msg) then
return true
end
return false
end
function is_owner(msg)
local var = false
local hash = 'chat:'..msg.chat.id..':owner'
local owner_list = db:hkeys(hash) --get the current owner id
local owner = owner_list[1]
if owner == tostring(msg.from.id) then
var = true
end
if owners(msg) then
var = true
end
return var
end
function is_owner2(chat_id, user_id)
local var = false
local hash = 'chat:'..chat_id..':owner'
local owner_list = db:hkeys(hash) --get the current owner id
local owner = owner_list[1]
if owner == tostring(user_id) then
var = true
end
if owners_listed(user_id) then
var = true
end
return var
end
function is_mod(msg)
local var = false
local hash = 'chat:'..msg.chat.id..':mod'
local modlist = db:hkeys(hash) --get the list of ids
for i=1, #modlist do
if tostring(modlist[i]) == tostring(msg.from.id) then
var = true
end
end
if owners(msg) then
var = true
end
--no need to check if is owner: the owner id is already saved in the modlist
return var
end
function is_mod2(chat_id, user_id)
local var = false
local hash = 'chat:'..chat_id..':mod'
local modlist = db:hkeys(hash) --get the list of ids
for i=1, #modlist do
if tostring(modlist[i]) == tostring(user_id) then
var = true
end
end
if owners_listed(user_id) then
var = true
end
--no need to check if is owner: the owner id is already saved in the modlist
return var
end
function set_owner(chat_id, user_id, nick)
db:hset('chat:'..chat_id..':mod', user_id, nick) --mod
db:hset('chat:'..chat_id..':owner', user_id, nick) --owner
end
function is_blocked(id)
if db:sismember('bot:blocked', id) then
return true
else
return false
end
end
function is_locked(msg, cmd)
local hash = 'chat:'..msg.chat.id..':settings'
local current = db:hget(hash, cmd)
if current == 'yes' then
return true
end
return false
end
function is_banned(chat_id, user_id)
--useful only for normal groups
local hash = 'chat:'..chat_id..':banned'
local res = db:sismember(hash, user_id)
if res then
return true
else
return false
end
end
function mystat(cmd)
local hash = 'commands:stats'
db:hincrby(hash, cmd, 1)
end
function string:trim() -- Trims whitespace from a string.
local s = self:gsub('^%s*(.-)%s*$', '%1')
return s
end
function load_data(filename) -- Loads a JSON file as a table.
local f = io.open(filename)
if not f then
return {}
end
local s = f:read('*all')
f:close()
local data = JSON.decode(s)
return data
end
function save_data(filename, data) -- Saves a table to a JSON file.
local s = JSON.encode(data)
local f = io.open(filename, 'w')
f:write(s)
f:close()
end
function clean_owner_modlist(chat)
--clear the owner
local hash = 'chat:'..chat..':owner'
local owner_list = db:hkeys(hash) --get the current owner id
local owner = owner_list[1]
db:hdel(hash, owner)
--clean the modlist
hash = 'chat:'..chat..':mod'
local mod_list = db:hkeys(hash) --get mods id
for id,nick in pairs(mod_list) do
if id then
db:hdel(hash, id)
else
api.sendLog('#removed\nUnknown group\nId: '..chat)
end
end
end
function vardump(value)
print(serpent.block(value, {comment=false}))
end
function vtext(value)
return serpent.block(value, {comment=false})
end
local function per_away(text)
local text = tostring(text):gsub('%%', '£&£')
return text
end
function make_text(text, par1, par2, par3, par4, par5)
if par1 then text = text:gsub('&&&1', per_away(par1)) end
if par2 then text = text:gsub('&&&2', per_away(par2)) end
if par3 then text = text:gsub('&&&3', per_away(par3)) end
if par4 then text = text:gsub('&&&4', per_away(par4)) end
if par5 then text = text:gsub('&&&5', per_away(par5)) end
text = text:gsub('£&£', '%%')
return text
end
function write_file(path, text, mode)
if not mode then
mode = "w"
end
file = io.open(path, mode)
if not file then
return false --path uncorrect
else
file:write(text)
file:close()
return true
end
end
local function create_folder(name)
local cmd = io.popen('sudo mkdir '..name)
cmd:read('*all')
cmd = io.popen('sudo chmod -R 777 '..name)
cmd:read('*all')
cmd:close()
end
function save_log(action, arg1, arg2, arg3, arg4)
if action == 'send_msg' then
local text = os.date('[%A, %d %B %Y at %X]')..'\n'..arg1..'\n\n'
local path = "./logs/msgs_errors.txt"
local res = write_file(path, text, "a")
if not res then
create_folder('logs')
write_file(path, text, "a")
end
elseif action == 'errors' then
--error, from, chat, text
local path = "./logs/errors.txt"
local text = os.date('[%A, %d %B %Y at %X]')..'\nERROR: '..arg1
if arg2 then
text = text..'\nFROM: '..arg2
end
if arg3 then
text = text..'\nCHAT: '..arg3
end
if arg4 then
text = text..'\nTEXT: '..arg4
end
text = text..'\n\n'
local res = write_file(path, text, "a")
if not res then
create_folder('logs')
write_file(path, text, "a")
end
elseif action == 'starts' then
local path = "./logs/starts.txt"
local text = 'Started: '..os.date('%A, %d %B %Y at %X')..'\n'
local res = write_file(path, text, "a")
if not res then
create_folder('logs')
write_file(path, text, "a")
end
elseif action == 'added' then
local text = 'BOT ADDED ['..os.date('%A, %d %B %Y at %X')..'] to ['..arg1..'] ['..arg2..'] by ['..arg3..'] ['..arg4..']\n'
local path = "./logs/additions.txt"
local res = write_file(path, text, "a")
if not res then
create_folder('logs')
write_file(path, text, "a")
end
end
end
function clone_table(t) --doing "table1 = table2" in lua = create a pointer to table2
local new_t = {}
local i, v = next(t, nil)
while i do
new_t[i] = v
i, v = next(t, i)
end
return new_t
end
function res_user(username)
local hash = 'bot:usernames'
local stored = db:hget(hash, username)
if not stored then
return false
else
return stored
end
end
function res_user_group(username, chat_id)
username = username:lower()
local hash = 'bot:usernames:'..chat_id
local stored = db:hget(hash, username)
if stored then
return stored
else
hash = 'bot:usernames'
stored = db:hget(hash, username)
if stored then
return stored
else
return false
end
end
end
function is_lang_supported(code)
for i=1,#config.available_languages do
if code:lower() == config.available_languages[i] then
return true
end
end
return false
end
function get_media_type(msg)
if msg.photo then
return 'image'
elseif msg.video then
return 'video'
elseif msg.audio then
return 'audio'
elseif msg.voice then
return 'voice'
elseif msg.document then
if msg.document.mime_type == 'video/mp4' then
return 'gif'
else
return 'file'
end
elseif msg.sticker then
return 'sticker'
elseif msg.contact then
return 'contact'
end
return false
end
function group_table(chat_id)
local group = {
id = chat_id
}
local redis = {
hgetall = {
mods = 'chat:'..chat_id..':mod',
owner = 'chat:'..chat_id..':owner',
settings = 'chat:'..chat_id..':settings',
mediasettings = 'chat:'..chat_id..':media',
flood = 'chat:'..chat_id..':flood',
extra = 'chat:'..chat_id..':extra',
welcome = 'chat:'..chat_id..':welcome'
},
get = {
about = 'chat:'..chat_id..':about',
rules = 'chat:'..chat_id..':rules'
},
smembers = {
admblock = 'chat:'..chat_id..':reportblocked'
}
}
for k,v in pairs(redis.hgetall) do
local tab = db:hgetall(v)
group[k] = tab
end
for k,v in pairs(redis.get) do
local tab = db:get(v)
group[k] = tab
end
for k,v in pairs(redis.smembers) do
local tab = db:smembers(v)
group[k] = tab
end
return group
end
voice_updated = 0
voice_succ = 0
function give_result(res)
--doesn't handle a nil "res"
if res == 1 then
voice_succ = voice_succ + 1
return ' done (res: 1)'
else
voice_updated = voice_updated + 1
return ' updated (res: 0)'
end
end
function migrate_table(t, hash)
if not next(t) then
return '[empty table]\n'
end
local txt = ''
for k,v in pairs(t) do
txt = txt..k..' ('..v..') [migration:'
local res = db:hset(hash, k, v)
txt = txt..give_result(res)..']\n'
end
return txt
end
function migrate_chat_info(old, new, on_request)
if not old or not new then
print('A group id is missing')
return false
end
local mods = db:hgetall('chat:'..old..':mod')
local owner_id = db:hkeys('chat:'..old..':owner')
local owner_name = db:hvals('chat:'..old..':owner')
local settings = db:hgetall('chat:'..old..':settings')
local media = db:hgetall('chat:'..old..':media')
local flood = db:hgetall('chat:'..old..':flood')
local about = db:get('chat:'..old..':about')
local rules = db:get('chat:'..old..':rules')
local extra = db:hgetall('chat:'..old..':extra')
local admblock = db:smembers('chat:'..old..':reportblocked')
local logtxt = 'FROM ['..old..'] TO ['..new..']\n'
--migrate mods
logtxt = logtxt..'Migrating moderators (#'..#mods..')...\n'
logtxt = logtxt..migrate_table(mods, 'chat:'..new..':mod')
--migrate owner
logtxt = logtxt..'Migrating owner...'
if next(owner_id) and next(owner_name) then
local res = db:hset('chat:'..new..':owner', owner_id[1], owner_name[1])
logtxt = logtxt..give_result(res)..'\n'
else logtxt = logtxt..' empty\n' end
--migrate about
logtxt = logtxt..'Migrating about...'
if about then
res = db:set('chat:'..new..':about', about)
logtxt = logtxt..give_result(res)..'\n'
else logtxt = logtxt..' empty\n' end
--migrate rules
logtxt = logtxt..'Migrating rules...'
if rules then
res = db:set('chat:'..new..':rules', rules)
logtxt = logtxt..give_result(res)..'\n'
else logtxt = logtxt..' empty\n' end
--migrate settings
logtxt = logtxt..'Migrating settings...\n'
logtxt = logtxt..migrate_table(settings, 'chat:'..new..':settings')
--migrate media settings
logtxt = logtxt..'Migrating media settings...\n'
logtxt = logtxt..migrate_table(media, 'chat:'..new..':media')
--migrate extra
logtxt = logtxt..'Migrating extra...\n'
logtxt = logtxt..migrate_table(extra, 'chat:'..new..':extra')
--migrate flood settings
logtxt = logtxt..'Migrating flood settings...\n'
logtxt = logtxt..migrate_table(flood, 'chat:'..new..':flood')
--migrate adminblocked list
logtxt = logtxt..'Migrating admin-blocked...\n'
if admblocked and next(admblocked) then
for k,v in pairs(admblock) do
logtxt = logtxt..v..' migration: '
local res = db:sadd('chat:'..new..':reportblocked')
logtxt = logtxt..give_result(res)..'\n'
end
else
logtxt = logtxt..'List empty\n'
end
--flood
print(logtxt)
local log_path = "./logs/migration_from["..tostring(old):gsub('-', '').."]to["..tostring(new):gsub('-', '').."].txt"
file = io.open(log_path, "w")
file:write(logtxt)
file:close()
if on_request then
api.sendDocument(config.admin, log_path)
end
end
function div()
print('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
print('XXXXXXXXXXXXXXXXXX BREAK XXXXXXXXXXXXXXXXXXX')
print('XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
end
local function migrate_ban_list(old, new)
local hash = 'chat:'..old..':banned'
local banned = db:smembers(hash)
if next(banned) then
for i=1, #banned do
api.kickChatMember(new, banned[i])
end
end
end
function to_supergroup(msg)
local old = msg.chat.id
local new = msg.migrate_to_chat_id
migrate_chat_info(old, new, false)
migrate_ban_list(old, new)
api.sendMessage(new, '(_grupo convertido a supergrupo_)', true)
end
function remove_plugin()
local text = ''
for k,v in pairs(config.plugins) do
if (v:match(".lua$")) then
table.remove(config.plugins, 1)
text = text..v..'\n'
end
end
return text
end
function getname(msg)
local name = msg.from.first_name
if msg.from.username then name = name..' (@'..msg.from.username..')' end
return name
end
function change_one_header(id)
voice_succ = 0
voice_updated = 0
logtxt = ''
logtxt = logtxt..'\n-----------------------------------------------------\nGROUP ID: '..id..'\n'
print('Group:', id) --first: print this, once the for is done, print logtxt
logtxt = logtxt..'---> PORTING MODS...\n'
local mods = db:hgetall('bot:'..id..':mod')
logtxt = logtxt..migrate_table(mods, 'chat:'..id..':mod')
logtxt = logtxt..'---> PORTING OWNER...\n'
local owner_id = db:hkeys('bot:'..id..':owner')
local owner_name = db:hvals('bot:'..id..':owner')
if not next(owner_id) or not next(owner_name) then
logtxt = logtxt..'No owner!\n'
else
logtxt = logtxt..'Owner info: '..owner_id[1]..', '..owner_name[1]..' [migration:'
local res = db:hset('chat:'..id..':owner', owner_id[1], owner_name[1])
logtxt = logtxt..give_result(res)..'\n'
end
logtxt = logtxt..'---> PORTING MEDIA SETTINGS...\n'
local media = db:hgetall('media:'..id)
logtxt = logtxt..migrate_table(media, 'chat:'..id..':media')
logtxt = logtxt..'---> PORTING ABOUT...\n'
local about = db:get('bot:'..id..':about')
if not about then
logtxt = logtxt..'No about!\n'
else
logtxt = logtxt..'About found! [migration:'
local res = db:set('chat:'..id..':about', about)
logtxt = logtxt..give_result(res)..']\n'
end
logtxt = logtxt..'---> PORTING RULES...\n'
local rules = db:get('bot:'..id..':rules')
if not rules then
logtxt = logtxt..'No rules!\n'
else
logtxt = logtxt..'Rules found! [migration:'
local res = db:set('chat:'..id..':rules', rules)
logtxt = logtxt..give_result(res)..']\n'
end
logtxt = logtxt..'---> PORTING EXTRA...\n'
local extra = db:hgetall('extra:'..id)
logtxt = logtxt..migrate_table(extra, 'chat:'..id..':extra')
print('\n\n\n')
logtxt = 'Successful: '..voice_succ..'\nUpdated: '..voice_updated..'\n\n'..logtxt
print(logtxt)
local log_path = "./logs/changehashes"..id..".txt"
file = io.open(log_path, "w")
file:write(logtxt)
file:close()
api.sendDocument(config.admin, log_path)
end
function change_extra_header(id)
voice_succ = 0
voice_updated = 0
logtxt = ''
logtxt = logtxt..'\n-----------------------------------------------------\nGROUP ID: '..id..'\n'
print('Group:', id) --first: print this, once the for is done, print logtxt
logtxt = logtxt..'---> PORTING EXTRA...\n'
local extra = db:hgetall('extra:'..id)
logtxt = logtxt..migrate_table(extra, 'chat:'..id..':extra')
print('\n\n\n')
logtxt = 'Successful: '..voice_succ..'\nUpdated: '..voice_updated..'\n\n'..logtxt
print(logtxt)
local log_path = "./logs/changehashesEXTRA"..id..".txt"
file = io.open(log_path, "w")
file:write(logtxt)
file:close()
api.sendDocument(config.admin, log_path)
end
function download_to_file(url, file_path)--https://github.com/yagop/telegram-bot/blob/master/bot/utils.lua
print("url to download: "..url)
local respbody = {}
local options = {
url = url,
sink = ltn12.sink.table(respbody),
redirect = true
}
-- nil, code, headers, status
local response = nil
options.redirect = false
response = {https.request(options)}
local code = response[2]
local headers = response[3]
local status = response[4]
if code ~= 200 then return false, code end
print("Saved to: "..file_path)
file = io.open(file_path, "w+")
file:write(table.concat(respbody))
file:close()
return file_path, code
end
function telegram_file_link(res)
--res = table returned by getFile()
return "https://api.telegram.org/file/bot"..config.bot_api_key.."/"..res.result.file_path
end
----------------------- specific cross-plugins functions---------------------
local function getAbout(chat_id, ln)
local hash = 'chat:'..chat_id..':about'
local about = db:get(hash)
if not about then
return lang[ln].setabout.no_bio
else
return about
end
end
local function getRules(chat_id, ln)
local hash = 'chat:'..chat_id..':rules'
local rules = db:get(hash)
if not rules then
return lang[ln].setrules.no_rules
else
return rules
end
end
local function getModlist(chat_id)
local hash = 'chat:'..chat_id..':mod'
local mlist = db:hvals(hash) --the array can't be empty: there is always the owner in
local message = ''
--build the list
for i=1, #mlist do
message = message..i..' - '..mlist[i]..'\n'
end
if message == '' then
return '()'
else
return message
end
end
local function getExtraList(chat_id, ln)
local hash = 'chat:'..chat_id..':extra'
local commands = db:hkeys(hash)
local text = ''
if commands[1] == nil then
return make_text(lang[ln].extra.no_commands)
else
for k,v in pairs(commands) do
text = text..v..'\n'
end
return make_text(lang[ln].extra.commands_list, text)
end
end
local function getSettings(chat_id, ln)
--get settings from redis
local hash = 'chat:'..chat_id..':settings'
local settings_key = db:hkeys(hash)
local settings_val = db:hvals(hash)
local key
local val
message = make_text(lang[ln].bonus.settings_header, ln)
--build the message
for i=1, #settings_key do
key = settings_key[i]
val = settings_val[i]
local text
if val == 'yes' then
text = make_text(lang[ln].settings[key])..': 🚫\n'
else
text = '*'..make_text(lang[ln].settings[key])..'*: ✅\n'
end
message = message..text --concatenete the text
if key == 'Flood' then
local max_msgs = db:hget('chat:'..chat_id..':flood', 'MaxFlood') or 5
local action = db:hget('chat:'..chat_id..':flood', 'ActionFlood')
message = message..make_text(lang[ln].settings.resume.flood_info, max_msgs, action)
end
end
--build the "welcome" line
hash = 'chat:'..chat_id..':welcome'
local type = db:hget(hash, 'type')
if type == 'composed' then
local wel = db:hget(hash, 'content')
if wel == 'a' then
message = message..lang[ln].settings.resume.w_a
elseif wel == 'r' then
message = message..lang[ln].settings.resume.w_r
elseif wel == 'm' then
message = message..lang[ln].settings.resume.w_m
elseif wel == 'ra' then
message = message..lang[ln].settings.resume.w_ra
elseif wel == 'rm' then
message = message..lang[ln].settings.resume.w_rm
elseif wel == 'am' then
message = message..lang[ln].settings.resume.w_am
elseif wel == 'ram' then
message = message..lang[ln].settings.resume.w_ram
elseif wel == 'no' then
message = message..lang[ln].settings.resume.w_no
end
elseif type == 'media' then
message = message..lang[ln].settings.resume.w_media
elseif type == 'custom' then
message = message..lang[ln].settings.resume.w_custom
end
return message
end
local function enableSetting(chat_id, field, ln)
local hash = 'chat:'..chat_id..':settings'
local field_lower = field:lower()
local now = db:hget(hash, field)
if now == 'no' then
return lang[ln].settings.enable[field_lower..'_already']
else
db:hset(hash, field, 'no')
return lang[ln].settings.enable[field_lower..'_unlocked']
end
end
local function disableSetting(chat_id, field, ln)
local hash = 'chat:'..chat_id..':settings'
local field_lower = field:lower()
local now = db:hget(hash, field)
if now == 'yes' then
return lang[ln].settings.disable[field_lower..'_already']
else
db:hset(hash, field, 'yes')
return lang[ln].settings.disable[field_lower..'_locked']
end
end
local function changeSettingStatus(chat_id, field, ln)
local hash = 'chat:'..chat_id..':settings'
local field_lower = field:lower()
local now = db:hget(hash, field)
if now == 'no' then
db:hset(hash, field, 'yes')
return lang[ln].settings.disable[field_lower..'_locked']
else
db:hset(hash, field, 'no')
return lang[ln].settings.enable[field_lower..'_unlocked']
end
end
local function changeFloodSettings(chat_id, screm, ln)
local hash = 'chat:'..chat_id..':flood'
if type(screm) == 'string' then
if screm == 'kick' then
db:hset(hash, 'ActionFlood', 'ban')
return lang[ln].floodmanager.ban
elseif screm == 'ban' then
db:hset(hash, 'ActionFlood', 'kick')
return lang[ln].floodmanager.kick
end
elseif type(screm) == 'number' then
local old = tonumber(db:hget(hash, 'MaxFlood')) or 5
local new
if screm > 0 then
new = db:hincrby(hash, 'MaxFlood', 1)
if new > 25 then
db:hincrby(hash, 'MaxFlood', -1)
return make_text(lang[ln].floodmanager.number_invalid, new)
end
elseif screm < 0 then
new = db:hincrby(hash, 'MaxFlood', -1)
if new < 4 then
db:hincrby(hash, 'MaxFlood', 1)
return make_text(lang[ln].floodmanager.number_invalid, new)
end
end
return make_text(lang[ln].floodmanager.changed, old, new)
end
end
local function changeMediaStatus(chat_id, media, new_status, ln)
local old_status = db:hget('chat:'..chat_id..':media', media)
if new_status == 'next' then
if not old_status then
new_status = 'kick'
elseif old_status == 'kick' then
new_status = 'ban'
elseif old_status == 'ban' then
new_status = 'allowed'
elseif old_status == 'allowed' then
new_status = 'kick'
end
end
if new_status == 'allow' then
new_status = 'allowed'
end
if old_status == new_status then
return make_text(lang[ln].mediasettings.already, media, new_status), false
else
db:hset('chat:'..chat_id..':media', media, new_status)
return make_text(lang[ln].mediasettings.changed, media, new_status), true
end
end
local function sendStartMe(msg, ln)
local keyboard = {}
keyboard.inline_keyboard = {
{
{text = 'Start me', url = 'https://telegram.me/'..bot.username}
}
}
api.sendKeyboard(msg.chat.id, lang[ln].help.group_not_success, keyboard, true)
end
local function initGroup(chat_id, owner_id, nick)
--add owner as moderator
local hash = 'chat:'..chat_id..':mod'
db:hset(hash, owner_id, nick)
--add owner as owner
hash = 'chat:'..chat_id..':owner'
db:hset(hash, owner_id, nick)
--default settings
hash = 'chat:'..chat_id..':settings'
--disabled for users:yes / disabled for users:no
db:hset(hash, 'Rules', 'no')
db:hset(hash, 'About', 'no')