-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_management.py
1508 lines (1392 loc) · 52.2 KB
/
db_management.py
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
import datetime
import json
import logging
import os
import traceback
import typing
import peewee
import pyrogram
from playhouse.sqliteq import SqliteQueueDatabase
DB = None
local_config = None
with open(file="config.json", encoding="utf-8") as f:
local_config = json.load(fp=f)
if not local_config:
logging.log(logging.FATAL, "Missing config.json")
exit()
DB = SqliteQueueDatabase(
database=local_config["database"],
pragmas=[
("foreign_keys", 1),
("journal_mode", "wal"),
("wal_checkpoint", "truncate"),
("ignore_check_constraints", 0),
("synchronous", "normal"),
],
)
class Users(peewee.Model):
id = peewee.IntegerField(
primary_key=True, constraints=[peewee.Check(constraint="id > 0")]
)
first_name = peewee.CharField(null=False)
last_name = peewee.CharField(default=None, null=True)
username = peewee.CharField(default=None, null=True)
is_bot = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_bot BETWEEN 0 AND 1")],
)
timestamp = peewee.DateTimeField(default=datetime.datetime.utcnow, null=False)
class Meta:
database = DB
class UserSettings(peewee.Model):
user = peewee.ForeignKeyField(
primary_key=True,
model=Users,
backref="settings",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
# 2 CHARACTERS
language = peewee.CharField(default="en", null=False)
shitstorm_counter = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="shitstorm_counter >= 0")],
)
# date of global ban
global_ban_date = peewee.DateField(default=datetime.date.min, null=False)
# expiration date of global ban
global_ban_expiration = peewee.DateField(default=datetime.date.min, null=False)
global_ban_counter = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="global_ban_counter >= 0")],
)
private_flood_counter = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="private_flood_counter >= 0")],
)
# datetime of block in private with bot
block_datetime = peewee.DateTimeField(default=datetime.datetime.min, null=False)
# expiration date of block in private with bot
block_expiration = peewee.DateTimeField(default=datetime.datetime.min, null=False)
block_counter = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="block_counter >= 0")],
)
# how the user is called by others, used to alert the user < 128 CHARACTERS
nickname = peewee.CharField(default=None, null=True)
# POSSIBLE VALUES #
# 0 = No #
# 1 = Only my groups#
# 2 = Yes #
# does the user want to be alerted when tagged (username/nickname)?
wants_tag_alerts = peewee.IntegerField(
default=False,
null=False,
constraints=[peewee.Check(constraint="wants_tag_alerts BETWEEN 0 AND 2")],
)
# did the user start the bot?
has_blocked_bot = peewee.BooleanField(
default=True,
null=False,
constraints=[peewee.Check(constraint="has_blocked_bot BETWEEN 0 AND 1")],
)
# can the user use advanced keyboards?
advanced_mode = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="advanced_mode BETWEEN 0 AND 1")],
)
class Meta:
database = DB
class Networks(peewee.Model):
link = peewee.CharField(primary_key=True)
name = peewee.CharField(unique=True, null=False)
# who owns the network
owner = peewee.ForeignKeyField(
model=Users, backref="networks_owned", on_update="CASCADE", null=False
)
# is the network public?
is_public = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_public BETWEEN 0 AND 1")],
)
class Meta:
database = DB
class Chats(peewee.Model):
id = peewee.IntegerField(
primary_key=True, constraints=[peewee.Check(constraint="id < 0")]
)
title = peewee.CharField(null=False)
username = peewee.CharField(default=None, null=True)
is_channel = peewee.BooleanField(null=False)
timestamp = peewee.DateTimeField(default=datetime.datetime.utcnow, null=False)
class Meta:
database = DB
class ChatSettings(peewee.Model):
chat = peewee.ForeignKeyField(
primary_key=True,
model=Chats,
backref="settings",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
# who owns the chat
owner = peewee.ForeignKeyField(
model=Users, backref="chats_owned", on_update="CASCADE", null=True
)
# 2 CHARACTERS
language = peewee.CharField(default="en", null=False)
# link of the chat < 512 CHARACTERS
link = peewee.CharField(default=None, null=True)
# number of members necessary before sending welcome message BETWEEN 0 AND 10
welcome_members = peewee.IntegerField(default=0, null=False)
# is bot enabled on chat?
is_bot_on = peewee.BooleanField(
default=True,
null=False,
constraints=[peewee.Check(constraint="is_bot_on BETWEEN 0 AND 1")],
)
# should bot stay in this chat?
is_banned = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_banned BETWEEN 0 AND 1")],
)
# are settings locked by the owner?
are_locked = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="are_locked BETWEEN 0 AND 1")],
)
# where to send logs
log_channel = peewee.IntegerField(default=None, null=True)
# should inform user about tags (with username and nickname if set) in chat?
has_pin_markers = peewee.BooleanField(
default=True,
null=False,
constraints=[peewee.Check(constraint="has_pin_markers BETWEEN 0 AND 1")],
)
# POSSIBLE VALUES #
# 0 = No #
# 1 = Only my groups/in group #
# 2 = Yes #
# should inform user about tags (with username and nickname if set) in chat?
has_tag_alerts = peewee.IntegerField(
default=False,
null=False,
constraints=[peewee.Check(constraint="has_tag_alerts BETWEEN 0 AND 2")],
)
# can users access the link freely?
is_link_public = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="is_link_public BETWEEN 0 AND 2")],
)
# maximum number of messages per user in X seconds BETWEEN 3 AND 10
max_flood = peewee.IntegerField(default=5, null=False)
# time interval in which messages increment flood counter BETWEEN 3 AND 10
max_flood_time = peewee.IntegerField(default=8, null=False)
# maximum number of warns a user can receive BETWEEN 1 AND 10
max_warns = peewee.IntegerField(
default=3, null=False, constraints=[peewee.Check(constraint="max_warns > 0")]
)
# measured in seconds, 24h default BETWEEN 30 AND 31622400 but it's better to take some seconds so BETWEEN X AND Y
max_temp_restrict = peewee.IntegerField(
default=local_config["default_temp_restrict"], null=False
)
# measured in seconds, 24h default BETWEEN 30 AND 31622400 but it's better to take some seconds so BETWEEN X AND Y
max_temp_ban = peewee.IntegerField(
default=local_config["default_temp_ban"], null=False
)
# how many users can be added by someone at the same time BETWEEN 1 AND 20
max_invites = peewee.IntegerField(
default=5, null=False, constraints=[peewee.Check(constraint="max_invites >= 1")]
)
# region locks and mutes
# POSSIBLE VALUES #
# 0 = allowed #
# 1 = delete #
# 2 = warn #
# 3 = kick #
# 4 = temprestrict #
# 5 = restrict #
# 6 = tempban #
# 7 = ban #
# should inform chat about automatic actions? 0 = no
group_notices = peewee.IntegerField(
default=2,
null=False,
constraints=[
peewee.Check(constraint="group_notices == 0 OR group_notices >= 2")
],
)
# why times BETWEEN 0 AND 95?
# 2 variables => hours and minutes
# hours have 23 alternatives => 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23
# minutes have 4 alternatives => 00, 15, 30, 45 (alternatives 0, 1, 2, 3)
# hours * 4 + minutes (alternatives) < 96
# to retrieve the exact time the formula is: value / 4 = hour.minutes
# where minutes can be .00(0)(0), .25(15)(1), .50(30)(2), .75(45)(3)
# is the night mode enabled?
night_mode_punishment = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="night_mode_punishment >= 0")],
)
# what time should the night mode start? BETWEEN 0 AND 95
night_mode_from = peewee.IntegerField(
default=90, # 22:30
null=False,
constraints=[peewee.Check(constraint="night_mode_from BETWEEN 0 AND 95")],
)
# what time should the night mode stop? BETWEEN 0 AND 95
night_mode_to = peewee.IntegerField(
default=28, # 07:00
null=False,
constraints=[peewee.Check(constraint="night_mode_to BETWEEN 0 AND 95")],
)
# why times BETWEEN 0 AND 95?
# 2 variables => hours and minutes
# hours have 23 alternatives => 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23
# minutes have 4 alternatives => 00, 15, 30, 45 (alternatives 0, 1, 2, 3)
# hours * 4 + minutes (alternatives) < 96
# to retrieve the exact time the formula is: value / 4 = hour.minutes_alternatives
# where minutes can be .00(0)(0), .25(15)(1), .50(30)(2), .75(45)(3)
# is the slow mode enabled?
slow_mode_value = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="slow_mode_value >= 0")],
)
# what time should the slow mode start? BETWEEN 0 AND 95
slow_mode_from = peewee.IntegerField(
default=90, # 22:30
null=False,
constraints=[peewee.Check(constraint="slow_mode_from BETWEEN 0 AND 95")],
)
# what time should the slow mode stop? BETWEEN 0 AND 95
slow_mode_to = peewee.IntegerField(
default=28, # 07:00
null=False,
constraints=[peewee.Check(constraint="slow_mode_to BETWEEN 0 AND 95")],
)
# when max_warns is reached
max_warns_punishment = peewee.IntegerField(
default=7,
null=False,
constraints=[
peewee.Check(
constraint="max_warns_punishment == 0 OR max_warns_punishment >= 4"
)
],
)
# should show temporary punishments as possible punishments?
allow_temporary_punishments = peewee.BooleanField(
default=False,
null=False,
constraints=[
peewee.Check(constraint="allow_temporary_punishments BETWEEN 0 AND 1")
],
)
# should flood_process multiple audios/documents/photos/videos/ sent as an album?
allow_media_group = peewee.BooleanField(
default=True,
null=False,
constraints=[peewee.Check(constraint="allow_media_group BETWEEN 0 AND 1")],
)
allow_service_messages = peewee.BooleanField(
default=True,
null=False,
constraints=[peewee.Check(constraint="allow_service_messages BETWEEN 0 AND 1")],
)
# MUTES
# mute everything in the chat
anti_everything = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="anti_everything >= 0")],
)
# gifs
anti_animation = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="anti_animation >= 0")],
)
anti_audio = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="anti_audio >= 0")]
)
anti_contact = peewee.IntegerField(
default=1,
null=False,
constraints=[peewee.Check(constraint="anti_contact >= 0")],
)
# generic file
anti_document = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="anti_document >= 0")],
)
anti_game = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="anti_game >= 0")]
)
anti_location = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="anti_location >= 0")],
)
anti_photo = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="anti_photo >= 0")]
)
anti_sticker = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="anti_sticker >= 0")],
)
anti_text = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="anti_text >= 0")]
)
# type of location
anti_venue = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="anti_venue >= 0")]
)
anti_video = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="anti_video >= 0")]
)
anti_video_note = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="anti_video_note >= 0")],
)
anti_voice = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="anti_voice >= 0")]
)
# LOCKS
# add users/bots
add_punishment = peewee.IntegerField(
default=5,
null=False,
constraints=[
peewee.Check(constraint="add_punishment == 0 OR add_punishment >= 4")
],
)
# arabic characters
arabic_punishment = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="arabic_punishment >= 0")],
)
# punishment for bots in chat
bot_punishment = peewee.IntegerField(
default=3,
null=False,
constraints=[
peewee.Check(constraint="bot_punishment == 0 OR bot_punishment >= 3")
],
)
# censorship on words/medias
censorships_punishment = peewee.IntegerField(
default=1,
null=False,
constraints=[peewee.Check(constraint="censorships_punishment >= 0")],
)
# more than X messages in Y seconds
flood_punishment = peewee.IntegerField(
default=2,
null=False,
constraints=[
peewee.Check(constraint="flood_punishment == 0 OR flood_punishment >= 2")
],
)
# forwarded messages from channels
forward_punishment = peewee.IntegerField(
default=2,
null=False,
constraints=[peewee.Check(constraint="forward_punishment >= 0")],
)
globally_banned_punishment = peewee.IntegerField(
default=5,
null=False,
constraints=[
peewee.Check(
constraint="globally_banned_punishment == 0 OR globally_banned_punishment >= 4"
)
],
)
# people who join the chat
join_punishment = peewee.IntegerField(
default=0,
null=False,
constraints=[
peewee.Check(constraint="join_punishment == 0 OR join_punishment >= 4")
],
)
# links to chats and channels
link_spam_punishment = peewee.IntegerField(
default=2,
null=False,
constraints=[peewee.Check(constraint="link_spam_punishment >= 0")],
)
# RightToLeft characters
rtl_punishment = peewee.IntegerField(
default=1,
null=False,
constraints=[peewee.Check(constraint="rtl_punishment >= 0")],
)
# lots of equal messages
shitstorm_punishment = peewee.IntegerField(
default=7,
null=False,
constraints=[
peewee.Check(
constraint="shitstorm_punishment == 0 OR shitstorm_punishment >= 4"
)
],
)
# messages longer than 2048 OR messages/names with more than 40 non-printable characters
text_spam_punishment = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="text_spam_punishment >= 0")],
)
# endregion
# at 5 messages that the bot could not write it leaves the chat
forbidden_writing_counter = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="forbidden_writing_counter >= 0")],
)
class Meta:
database = DB
class ChatPreActionList(peewee.Model):
chat = peewee.ForeignKeyField(
model=ChatSettings,
backref="preactioned_users",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
user_id = peewee.IntegerField(
null=False, constraints=[peewee.Check(constraint="user_id > 0")]
)
action = peewee.CharField(null=False)
class Meta:
database = DB
primary_key = peewee.CompositeKey("chat", "user_id")
class ChatWhitelistedChats(peewee.Model):
chat = peewee.ForeignKeyField(
model=ChatSettings,
backref="whitelisted_chats",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
# whitelisted chat id, can be positive (resolved by link, will be addressed both in -id and -100id) or negative (-id or -100id)
whitelisted_chat = peewee.IntegerField(null=False)
class Meta:
database = DB
primary_key = peewee.CompositeKey("chat", "whitelisted_chat")
class ChatCensorships(peewee.Model):
chat = peewee.ForeignKeyField(
model=ChatSettings,
backref="censorships",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
# thing to be censored < 512 CHARACTERS
value = peewee.CharField(null=False)
# is thing a regex?
is_regex = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_regex BETWEEN 0 AND 1")],
)
# is thing a media?
is_media = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_media BETWEEN 0 AND 1")],
)
# if value is media then the original message is necessary in order to fetch again the file_ref once expired
# DEPRECATED SINCE PYROGRAM 1.1
original_chat_id = peewee.IntegerField(default=None, null=True)
original_message_id = peewee.IntegerField(default=None, null=True)
class Meta:
database = DB
primary_key = peewee.CompositeKey("chat", "value")
class ChatExtras(peewee.Model):
chat = peewee.ForeignKeyField(
model=ChatSettings,
backref="extras",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
# keyword found in text < 50 CHARACTERS
key = peewee.CharField(null=False)
# WELCOME_BUTTONS is composed in the following way
# text1 | link1 && text2 | link2
# text3 | link3
# | separates button_text from link
# && separates buttons
# \n separates rows
# thing with which the bot has to reply < 2048 CHARACTERS, if media then ###file_id$caption
value = peewee.CharField(null=False)
# is it goodbye/rules/welcome/welcome_buttons?
is_group_data = peewee.BooleanField(null=False, default=False)
# is key a regex?
is_regex = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_regex BETWEEN 0 AND 1")],
)
# is value a media?
is_media = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_media BETWEEN 0 AND 1")],
)
# if value is media then the original message is necessary in order to fetch again the file_ref once expired
# DEPRECATED SINCE PYROGRAM 1.1
original_chat_id = peewee.IntegerField(default=None, null=True)
original_message_id = peewee.IntegerField(default=None, null=True)
class Meta:
database = DB
primary_key = peewee.CompositeKey("chat", "key", "value", "is_group_data")
class ChatAlternatives(peewee.Model):
chat = peewee.ForeignKeyField(
model=ChatSettings,
backref="alternative_commands",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
# original command that is used by bot < 50 CHARACTERS
original = peewee.CharField(
null=False, constraints=[peewee.Check(constraint="original != ''")]
)
# alternative command < 512 CHARACTERS
alternative = peewee.CharField(
null=False, constraints=[peewee.Check(constraint="alternative != ''")]
)
# is thing a media?
is_media = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_media BETWEEN 0 AND 1")],
)
# if value is media then the original message is necessary in order to fetch again the file_ref once expired
# DEPRECATED SINCE PYROGRAM 1.1
original_chat_id = peewee.IntegerField(default=None, null=True)
original_message_id = peewee.IntegerField(default=None, null=True)
class Meta:
database = DB
primary_key = peewee.CompositeKey("chat", "original", "alternative")
class RUserChat(peewee.Model):
user = peewee.ForeignKeyField(
model=Users,
backref="chats",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
chat = peewee.ForeignKeyField(
model=Chats,
backref="users",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
# rank of the user
# POSSIBLE VALUES #
# 0 = normal #
# 1 = privileged #
# 2 = junior moderator #
# 3 = senior moderator #
# 4 = owner #
rank = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="rank >= 0")]
)
# is member of group? (used for commands requiring a quite accurate member's list, not a reliable indicator on the long run)
is_member = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_member BETWEEN 0 AND 1")],
)
# is user administrator on telegram?
is_admin = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_admin BETWEEN 0 AND 1")],
)
# is user anonymous on telegram?
is_anonymous = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_anonymous BETWEEN 0 AND 1")],
)
# TELEGRAM PERMISSIONS
# Applicable to administrators only. True, if you are allowed to edit administrator privileges of the user.
can_be_edited = peewee.BooleanField(
default=False,
constraints=[peewee.Check(constraint="can_be_edited BETWEEN 0 AND 1")],
)
# Applicable to default chat permissions in private groups and administrators in public groups only. True, if the chat title, photo and other settings can be changed.
can_change_info = peewee.BooleanField(
default=False,
constraints=[peewee.Check(constraint="can_change_info BETWEEN 0 AND 1")],
)
# Applicable to administrators only. True, if the administrator can delete messages of other users.
can_delete_messages = peewee.BooleanField(
default=False,
constraints=[peewee.Check(constraint="can_delete_messages BETWEEN 0 AND 1")],
)
# Applicable to default chat permissions and administrators only. True, if new users can be invited to the chat.
can_invite_users = peewee.BooleanField(
default=False,
constraints=[peewee.Check(constraint="can_invite_users BETWEEN 0 AND 1")],
)
# Applicable to administrators only. True, if the administrator can access the chat event log, chat statistics, message statistics in channels, see channel members, see anonymous administrators in supergroups and ignore slow mode. Implied by any other administrator privilege.
can_manage_chat = peewee.BooleanField(
default=False,
constraints=[peewee.Check(constraint="can_manage_chat BETWEEN 0 AND 1")],
)
# Applicable to administrators only. True, if the administrator can manage video chats (also called group calls).
can_manage_video_chats = peewee.BooleanField(
default=False,
constraints=[peewee.Check(constraint="can_manage_video_chats BETWEEN 0 AND 1")],
)
# Applicable to default chat permissions in private groups and administrators in public groups only. True, if messages can be pinned, supergroups only.
can_pin_messages = peewee.BooleanField(
default=False,
constraints=[peewee.Check(constraint="can_pin_messages BETWEEN 0 AND 1")],
)
# Applicable to administrators only. True, if the administrator can add new administrators with a subset of his own privileges or demote administrators that he has promoted, directly or indirectly (promoted by administrators that were appointed by the user).
can_promote_members = peewee.BooleanField(
default=False,
constraints=[peewee.Check(constraint="can_promote_members BETWEEN 0 AND 1")],
)
# Applicable to administrators only. True, if the administrator can restrict, ban or unban chat members.
can_restrict_members = peewee.BooleanField(
default=False,
constraints=[peewee.Check(constraint="can_restrict_members BETWEEN 0 AND 1")],
)
# is user safe from locks/mutes checks in the chat?
is_whitelisted = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_whitelisted BETWEEN 0 AND 1")],
)
# is user safe from global ban check in the chat?
is_global_ban_whitelisted = peewee.BooleanField(
default=False,
null=False,
constraints=[
peewee.Check(constraint="is_global_ban_whitelisted BETWEEN 0 AND 1")
],
)
# user's number of warns in chat >= 0
warns = peewee.IntegerField(default=0, null=False)
# how many messages did the user write in the chat?
message_counter = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="message_counter >= 0")],
)
timestamp = peewee.DateTimeField(default=datetime.datetime.utcnow, null=False)
class Meta:
database = DB
primary_key = peewee.CompositeKey("user", "chat")
class RFlamedUserChat(peewee.Model):
flamed_user = peewee.ForeignKeyField(
model=Users,
backref="flamed_in",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
chat = peewee.ForeignKeyField(
model=Chats,
backref="flamed_users",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
counter = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="counter >= 0")],
)
max_flame = peewee.IntegerField(
default=10,
null=False,
constraints=[peewee.Check(constraint="max_flame >= 1")],
)
class Meta:
database = DB
primary_key = peewee.CompositeKey("flamed_user", "chat")
class RNetworkChat(peewee.Model):
network = peewee.ForeignKeyField(
model=Networks,
backref="chats",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
chat = peewee.ForeignKeyField(
model=ChatSettings,
backref="network",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
# are chat settings locked to network's ones?
are_settings_locked = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="are_settings_locked BETWEEN 0 AND 1")],
)
# are chat plugins locked to network's ones?
are_plugins_locked = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="are_plugins_locked BETWEEN 0 AND 1")],
)
class Meta:
database = DB
primary_key = peewee.CompositeKey("network", "chat")
class Plugins(peewee.Model):
name = peewee.CharField(primary_key=True)
# is plugin enabled globally?
is_enabled = peewee.BooleanField(
default=True,
null=False,
constraints=[peewee.Check(constraint="is_enabled BETWEEN 0 AND 1")],
)
# is plugin optional?
is_optional = peewee.BooleanField(
default=True,
null=False,
constraints=[peewee.Check(constraint="is_optional BETWEEN 0 AND 1")],
)
class Meta:
database = DB
class RChatPlugin(peewee.Model):
chat = peewee.ForeignKeyField(
model=ChatSettings,
backref="plugins",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
plugin = peewee.ForeignKeyField(
model=Plugins, on_delete="CASCADE", on_update="CASCADE", null=False
)
# is plugin just for rank^?
min_rank = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="min_rank >= 0")]
)
# is plugin enabled on chat?
is_enabled_on_chat = peewee.BooleanField(
default=True,
null=False,
constraints=[peewee.Check(constraint="is_enabled_on_chat BETWEEN 0 AND 1")],
)
class Meta:
database = DB
primary_key = peewee.CompositeKey("chat", "plugin")
class RuletaUsers(peewee.Model):
user = peewee.ForeignKeyField(
primary_key=True,
model=Users,
backref="ruleta_data",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
score = peewee.IntegerField(default=0, null=False)
attempts = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="attempts >= 0")]
)
deaths = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="deaths >= 0")]
)
duels = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="duels >= 0")]
)
victories = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="victories >= 0")]
)
defeats = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="defeats >= 0")]
)
surrenders = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="surrenders >= 0")]
)
streak = peewee.IntegerField(
default=0, null=False, constraints=[peewee.Check(constraint="streak >= 0")]
)
longest_streak = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="longest_streak >= 0")],
)
# is the user clickable in the leaderboards?
is_clickable = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_clickable BETWEEN 0 AND 1")],
)
# user alternative name?
alias = peewee.CharField(default=None, null=True)
class Meta:
database = DB
class RuletaChats(peewee.Model):
chat = peewee.ForeignKeyField(
primary_key=True,
model=ChatSettings,
backref="ruleta_settings",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
# normal cylinder BETWEEN 5 AND 10
cylinder = peewee.IntegerField(default=6, null=False)
# duel cylinder BETWEEN 5 AND 10
duel_cylinder = peewee.IntegerField(default=6, null=False)
# normal cylinder's bullets BETWEEN 1 AND cylinder
bullets = peewee.IntegerField(
default=1,
null=False,
constraints=[peewee.Check(constraint="bullets BETWEEN 1 AND cylinder")],
)
# duel cylinder's bullets BETWEEN 1 AND duel_cylinder
duel_bullets = peewee.IntegerField(
default=1,
null=False,
constraints=[
peewee.Check(constraint="duel_bullets BETWEEN 1 AND duel_cylinder")
],
)
class Meta:
database = DB
class RuletaDuels(peewee.Model):
challenger = peewee.ForeignKeyField(
model=RuletaUsers,
backref="challenger_of",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
challenged = peewee.ForeignKeyField(
model=RuletaUsers,
backref="challenged_by",
on_delete="CASCADE",
on_update="CASCADE",
null=False,
)
cylinder = peewee.IntegerField(default=6, null=False)
bullets = peewee.IntegerField(
default=1,
null=False,
constraints=[peewee.Check(constraint="bullets BETWEEN 1 AND cylinder")],
)
rounds = peewee.IntegerField(
default=0,
null=False,
constraints=[peewee.Check(constraint="rounds BETWEEN 0 AND cylinder")],
)
begin_date = peewee.DateTimeField(default=datetime.datetime.utcnow, null=False)
is_accepted = peewee.BooleanField(
default=False,
null=False,
constraints=[peewee.Check(constraint="is_accepted BETWEEN 0 AND 1")],
)
class Meta:
database = DB
primary_key = peewee.CompositeKey("challenger", "challenged")
constraints = [peewee.Check(constraint="challenger_id != challenged_id")]
class ResolvedObjects(peewee.Model):
id = peewee.IntegerField(primary_key=True, null=False)
username = peewee.CharField(default=None, null=True)
timestamp = peewee.DateTimeField(default=datetime.datetime.utcnow, null=False)
type = peewee.CharField(null=False)
class Meta: