-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_manager.py
1840 lines (1388 loc) · 54.3 KB
/
db_manager.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
"""
A large collection of functions which allow you to interact with the database.
Many of the functions are specific-use and serve as helpers for other functions.
There's a whole lot of janky logic in here to allow f-strings and prevent SQL injection.
"""
import sqlite3
import discord
import web.flask.item_db_manager as item_db
import time
import datetime
import re
# The path to the main SQLite database file
db_path = '/path/to/database.db'
class DBManager:
"""
A Class with common database functions.
"""
def __init__(self, db_name):
self.db_name = db_name
self.conn = sqlite3.connect(self.db_name)
self.cursor = self.conn.cursor()
def create_table(self, table_name, columns):
self.cursor.execute(
f"CREATE TABLE IF NOT EXISTS {table_name} ({columns})")
self.conn.commit()
def insert(self, table_name, values):
# Use placeholders and a tuple to pass values to the query
# to prevent SQL injection
self.cursor.execute(
f"INSERT INTO {table_name} VALUES (?, ?)", values)
self.conn.commit()
def select(self, table_name, columns, condition=None):
if condition:
# Use placeholders and a tuple to pass values to the query
# to prevent SQL injection
self.cursor.execute(
f"SELECT {columns} FROM {table_name} WHERE {condition}", condition)
else:
self.cursor.execute(f"SELECT {columns} FROM {table_name}")
return self.cursor.fetchall()
def update(self, table_name, column, value, condition):
# Use placeholders and a tuple to pass values to the query
# to prevent SQL injection
self.cursor.execute(
f"UPDATE {table_name} SET {column} = ? WHERE {condition}", value)
self.conn.commit()
def delete(self, table_name, condition):
# Use placeholders and a tuple to pass values to the query
# to prevent SQL injection
self.cursor.execute(
f"DELETE FROM {table_name} WHERE {condition}", condition)
self.conn.commit()
def close(self):
self.conn.close()
def daterange(start_date: datetime.date, end_date: datetime.date) -> datetime.date:
"""
A generator which allows you to iterate through a range of dates.
Accepts two arguments:
- start_date: A datetime.date object representing the start date.
- end_date: A datetime.date object representing the end date.
"""
for n in range(int((end_date - start_date).days)):
yield start_date + datetime.timedelta(n)
def parse_epoch_to_datetime(epoch: int) -> str:
"""
A function which converts an epoch timestamp to a datetime object.
Accepts one argument, an integer representing the epoch timestamp.
"""
return datetime.datetime.fromtimestamp(epoch).strftime('%m-%d-%Y %I:%M:%S%p')
def add_user(user: discord.User) -> None:
"""
Adds a user to the database.
Accepts one argument, a discord.User object.
"""
db = DBManager(db_path)
db.insert('DISCORD_USERS',
f'"{user.name}", "{user.id}", "{user.name}", "Member", 0, 0, "NORMAL", 0, 0, {user.discriminator}, {None}, {None}')
db.close()
def user_exists(user: discord.User) -> bool:
"""
A function to check if a user exists in the database.
Accepts one argument, a discord.User object.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', '*', f'USER_ID = "{user.id}"')
db.close()
if result:
return True
else:
return False
def change_permission(user: str, permission: str) -> None:
"""
A function to change a user's permission level in the database.
Accepts two arguments:
- user: A string representing at least part of the user's username.
- permission: A string representing the desired permission level.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'PERMISSION_LEVEL',
f'"{permission}"', f'USERNAME LIKE "%{user}%"')
db.close()
def fetch_username(user: str) -> str:
"""
A function which allows you to quickly fetch a user's username from a short query.
Accepts one argument, a string representing at least part of the user's username.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'USERNAME',
f'USERNAME LIKE "%{user}%"')
db.close()
return result[0]
def fetch_username_by_id(user_id: str) -> str:
"""
A function which allows you to fetch a user's username by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'USERNAME',
f'USER_ID == "{user_id}"')
db.close()
return result[0][0]
def fetch_permission(user: discord.User) -> str:
"""
A function which allows you to fetch the permission level of a user.
Accepts one argument, a discord.User object.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'PERMISSION_LEVEL',
f'USER_ID == "{user.id}"')
db.close()
return result[0]
def fetch_coin_balance(user: discord.User) -> int:
"""
A function which allows you to fetch the coin balance of a user.
Accepts one argument, a discord.User object.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'COINS',
f'USER_ID == "{user.id}"')
db.close()
return result[0]
def fetch_balance_by_username(username: str) -> int:
"""
A function which allows you to fetch the coin balance of a user by their username.
Accepts one argument, a string representing at least part of the user's username.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'COINS',
f'USERNAME == "{username}"')
db.close()
return result[0]
def fetch_users_sorted_by_balance():
"""
A function which allows you to fetch all users sorted by their coin balance.
Accepts no arguments.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', '*',
f'COINS IS NOT NULL ORDER BY COINS DESC')
db.close()
return result
def fetch_id_by_snowflake(snowflake: str) -> str:
"""
A function which allows you to fetch the user ID of a user by their snowflake.
Accepts one argument, a string representing the user's snowflake.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'USER_ID',
f'USERNAME == "{snowflake.split("#")[0]}" AND DISCRIMINATOR == "{snowflake.split("#")[1]}"')
db.close()
return result[0][0]
def update_coin_balance(id, amount):
"""
A function which allows you to update the coin balance of a user.
Accepts two arguments:
- id: A string representing the user's ID.
- amount: An integer representing the amount to add to the user's balance.
Can be used to add or subtract coins. To subtract coins, pass a negative integer.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'COINS',
f'COINS + {amount}', f'USER_ID == "{id}"')
db.close()
def set_coin_balance(id, amount):
"""
A function which allows you to set the coin balance of a user to a specific number.
Accepts two arguments:
- id: A string representing the user's ID.
- amount: An integer representing the amount to set the user's balance to.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'COINS',
f'{amount}', f'USER_ID == "{id}"')
db.close()
def set_balance_all(amount):
"""
A function which allows you to set the coin balance of all users to a specific number.
Accepts one argument, an integer representing the amount to set the user's balance to.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'COINS',
f'{amount}', f'USER_ID != "0"')
db.close()
def fetch_winnings(user):
"""
A function which allows you to fetch the winnings of a user.
Accepts one argument, a string representing the user's username.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'COINS_WON',
f'USERNAME == "{user}"')
db.close()
return result[0]
def update_winnings(user, amount):
"""
A function which allows you to update the winnings of a user.
Accepts two arguments:
- user: A string representing the user's username.
- amount: An integer representing the amount to add to the user's winnings.
Can be used to add or subtract winnings. To subtract winnings, pass a negative integer.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'COINS_WON',
f'COINS_WON + {amount}', f'USER_ID == "{user.id}"')
db.close()
def fetch_users():
"""
A function which allows you to fetch all users from the database.
Accepts no arguments.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', '*')
db.close()
return result
def add_coins_to_all(amount):
"""
A function which allows you to add coins to all users.
Accepts one argument, an integer representing the amount to add to all users.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'COINS',
f'COINS + {amount}', f'ACCOUNT_TYPE == "NORMAL"')
db.close()
def fetch_normal_users():
"""
A function which allows you to fetch all normal users from the database.
Accepts no arguments.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', '*', f'ACCOUNT_TYPE == "NORMAL"')
db.close()
return result
fetch_normal_users()
def is_mobile(user):
"""
A function which allows you to check if a user is using a mobile device.
Accepts one argument, a discord.User object.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'ACCOUNT_TYPE',
f'USER_ID == "{user.id}"')
db.close()
if result[0] == 'MOBILE':
return True
else:
return False
def fetch_theft_profit(user):
"""
A function which allows you to fetch the theft profit of a user.
Accepts one argument, a discord.User object.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'THEFT_PROFIT',
f'USER_ID == "{user.id}"')
db.close()
return result[0]
def fetch_theft_loss(user):
"""
A function which allows you to fetch the theft loss of a user.
Accepts one argument, a discord.User object.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'THEFT_LOSS',
f'USER_ID == "{user.id}"')
db.close()
return result[0]
def fetch_balance_by_id(id):
"""
A function which allows you to fetch the coin balance of a user by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'COINS',
f'USER_ID == "{id}"')
db.close()
return result[0][0]
def add_theft_profit(user, amount):
"""
A function which allows you to add to a user's theft profit.
Accepts two arguments:
- user: A discord.User object.
- amount: An integer representing the amount to add to the user's theft profit.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'THEFT_PROFIT',
f'THEFT_PROFIT + {amount}', f'USER_ID == "{user.id}"')
db.close()
def add_theft_loss(user, amount):
"""
A function which allows you to add to a user's theft loss.
Accepts two arguments:
- user: A discord.User object.
- amount: An integer representing the amount to add to the user's theft loss.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'THEFT_LOSS',
f'THEFT_LOSS + {amount}', f'USER_ID == "{user.id}"')
db.close()
def set_daily_payout_by_id(id, amount):
"""
A function which allows you to set the daily payout of a user by their ID.
Accepts two arguments:
- id: A string representing the user's ID.
- amount: An integer representing the amount to set the user's daily payout to.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'DAILY_PAYOUT',
f'{amount}', f'USER_ID == "{id}"')
db.close()
def update_daily_payout_by_id(id, amount):
"""
A function which allows you to update the daily payout of a user by their ID.
Accepts two arguments:
- id: A string representing the user's ID.
- amount: An integer representing the amount to add to the user's daily payout.
Can be used to add or subtract daily payout. To subtract daily payout, pass a negative integer.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'DAILY_PAYOUT',
f'DAILY_PAYOUT + {amount}', f'USER_ID == "{id}"')
db.close()
def fetch_daily_payout_by_id(id):
"""
A function which allows you to fetch the daily payout of a user by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'DAILY_PAYOUT',
f'USER_ID == "{id}"')
db.close()
return result[0][0]
def fetch_usernames():
"""
A function which allows you to fetch all usernames from the database.
Accepts no arguments.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'USERNAME')
db.close()
usernames = []
for username in result:
usernames.append(username[0])
return usernames
def update_username(user, username):
"""
A function which allows you to update a user's username.
Accepts two arguments:
- user: A discord.User object.
- username: A string representing the new username.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'USERNAME',
f'"{username}"', f'USER_ID == "{user.id}"')
db.update('DISCORD_USERS', 'DISCRIMINATOR',
f'"{user.discriminator}"', f'USER_ID == "{user.id}"')
db.close()
def update_balance_by_id(id, amount):
"""
A function which allows you to update the coin balance of a user by their ID.
Accepts two arguments:
- id: A string representing the user's ID.
- amount: An integer representing the amount to add to the user's balance.
Can be used to add or subtract coins. To subtract coins, pass a negative integer.
"""
db = DBManager(db_path)
db.update('DISCORD_USERS', 'COINS',
f'COINS + {amount}', f'USER_ID == "{id}"')
db.close()
def fetch_all_balances():
"""
A function which allows you to fetch all user balances.
Accepts no arguments.
Returns user names and balances.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'USERNAME, COINS, ACCOUNT_TYPE')
db.close()
return result
def fetch_all_winnings():
"""
A function which allows you to fetch all user winnings.
Accepts no arguments.
Returns user names and winnings.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'USERNAME, COINS_WON, ACCOUNT_TYPE')
db.close()
return result
def fetch_user_by_full_username(username, discriminator):
"""
A function which allows you to fetch a user by their full username.
Accepts two arguments:
- username: A string representing the user's username.
- discriminator: A string representing the user's discriminator.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', '*',
f'USERNAME == "{username}" AND DISCRIMINATOR == "{discriminator}"')
db.close()
return result[0][0]
def fetch_user_id_by_full_username(username, discriminator):
"""
A function which allows you to fetch a user's ID by their full username.
Accepts two arguments:
- username: A string representing the user's username.
- discriminator: A string representing the user's discriminator.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'USER_ID',
f'USERNAME == "{username}" AND DISCRIMINATOR == "{discriminator}"')
db.close()
return result[0][0]
def fetch_winnings_by_id(id):
"""
A function which allows you to fetch the coin winnings of a user by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'COINS_WON',
f'USER_ID == "{id}"')
db.close()
return result[0][0]
def fetch_inventory_by_id(id) -> list:
"""
A function which allows you to fetch the inventory of a user by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'INVENTORY',
f'USER_ID == "{id}"')
db.close()
if not result[0][0]:
return None
return result[0][0]
def add_to_inventory_by_id(id, item):
"""
A function which allows you to add an item to a user's inventory by their ID.
Accepts two arguments:
- item: A string representing the item to add to the user's inventory.
- id: A string representing the user's ID.
"""
item_id = item[0]
current_inventory = fetch_inventory_by_id(id)
if not current_inventory:
current_inventory = []
elif len(current_inventory) == 1:
current_inventory = [current_inventory]
else:
current_inventory = current_inventory.split(',')
current_inventory.append(item_id)
new_inventory_str = ','.join(current_inventory)
db = DBManager(db_path)
db.update('DISCORD_USERS', 'INVENTORY',
f'"{new_inventory_str}"', f'USER_ID == "{id}"')
db.close()
def add_to_inventory_by_item_id(user_id, item_id):
"""
A function which allows you to add an item to a user's inventory by their ID.
Accepts two arguments:
- item_id: A string representing the item ID to add to the user's inventory.
- user_id: A string representing the user's ID.
"""
current_inventory = fetch_inventory_by_id(user_id)
if not current_inventory:
current_inventory = []
elif len(current_inventory) == 1:
current_inventory = [current_inventory]
else:
current_inventory = current_inventory.split(',')
current_inventory.append(item_id)
new_inventory_str = ','.join(current_inventory)
db = DBManager(db_path)
db.update('DISCORD_USERS', 'INVENTORY',
f'"{new_inventory_str}"', f'USER_ID == "{user_id}"')
db.close()
def remove_from_inventory_by_id(id, item_id):
"""
A function which allows you to remove an item from a user's inventory by their ID.
Accepts two arguments:
- item: A string representing the item to remove from the user's inventory.
- id: A string representing the user's ID.
"""
current_inventory = fetch_inventory_by_id(str(id))
if not current_inventory:
current_inventory = []
elif len(current_inventory) == 1:
current_inventory = [current_inventory]
else:
current_inventory = current_inventory.split(',')
current_inventory.remove(item_id)
new_inventory_str = ','.join(current_inventory)
db = DBManager(db_path)
db.update('DISCORD_USERS', 'INVENTORY',
f'"{new_inventory_str}"', f'USER_ID == "{id}"')
db.close()
def remove_x_items_from_inventory_by_id(user_id, item_id, number):
"""
A function which allows you to remove x items from a user's inventory by their ID.
Accepts three arguments:
- item: A string representing the item to remove from the user's inventory.
- id: A string representing the user's ID.
- number: An integer representing the number of items to remove.
"""
current_inventory = fetch_inventory_by_id(str(user_id))
if not current_inventory:
current_inventory = []
elif len(current_inventory) == 1:
current_inventory = [current_inventory]
else:
current_inventory = current_inventory.split(',')
for i in range(number):
current_inventory.remove(item_id)
new_inventory_str = ','.join(current_inventory)
db = DBManager(db_path)
db.update('DISCORD_USERS', 'INVENTORY',
f'"{new_inventory_str}"', f'USER_ID == "{user_id}"')
db.close()
def add_x_items_to_inventory_by_id(user_id, item_id, number):
"""
A function which allows you to add x items to a user's inventory by their ID.
Accepts three arguments:
- item: A string representing the item to add to the user's inventory.
- id: A string representing the user's ID.
- number: An integer representing the number of items to add.
"""
current_inventory = fetch_inventory_by_id(str(user_id))
if not current_inventory:
current_inventory = []
elif len(current_inventory) == 1:
current_inventory = [current_inventory]
else:
current_inventory = current_inventory.split(',')
for i in range(number):
current_inventory.append(item_id)
new_inventory_str = ','.join(current_inventory)
db = DBManager(db_path)
db.update('DISCORD_USERS', 'INVENTORY',
f'"{new_inventory_str}"', f'USER_ID == "{user_id}"')
db.close()
def add_to_inventory_by_item_id(user_id, item_id):
"""
A function which allows you to add an item to a user's inventory by their ID.
Accepts two arguments:
- item_id: A string representing the item ID to add to the user's inventory.
- user_id: A string representing the user's ID.
"""
item_id = str(item_id)
current_inventory = fetch_inventory_by_id(user_id)
if not current_inventory:
current_inventory = []
elif len(current_inventory) == 1:
current_inventory = [current_inventory]
else:
current_inventory = current_inventory.split(',')
current_inventory.append(item_id)
new_inventory_str = ','.join(current_inventory)
db = DBManager(db_path)
db.update('DISCORD_USERS', 'INVENTORY',
f'"{new_inventory_str}"', f'USER_ID == "{user_id}"')
db.close()
def fetch_inventory_quantity_by_user_id(user_id, item_id):
"""
A function which allows you to fetch the quantity of an item in a user's inventory by their ID.
Accepts two arguments:
- user_id: A string representing the user's ID.
- item_id: A string representing the item's ID.
"""
db = DBManager(db_path)
result = db.select('DISCORD_USERS', 'INVENTORY',
f'USER_ID == "{user_id}"')
db.close()
if not result[0][0]:
return 0
inventory = result[0][0].split(',')
return inventory.count(item_id)
def use_item(user_id, item_id):
"""
A function which allows you to use an item in a user's inventory by their ID.
Accepts two arguments:
- user_id: A string representing the user's ID.
- item_id: A string representing the item's ID.
"""
current_inventory = fetch_inventory_by_id(user_id)
if not current_inventory:
return
elif len(current_inventory) == 1:
current_inventory = [current_inventory]
else:
current_inventory = current_inventory.split(',')
current_inventory.remove(item_id)
new_inventory_str = ','.join(current_inventory)
db = DBManager(db_path)
db.update('DISCORD_USERS', 'INVENTORY',
f'"{new_inventory_str}"', f'USER_ID == "{user_id}"')
db.close()
def add_active_item(item_id, item_name, user_id, user_name, valid_until_epoch, uses_left, status):
"""
A function which allows you to add an active item to the database.
Accepts six arguments:
- item_id: A string representing the item's ID.
- item_name: A string representing the item's name.
- user_id: A string representing the user's ID.
- user_name: A string representing the user's name.
- valid_until_epoch: An integer representing the epoch time when the item will expire.
- uses_left: An integer representing the number of uses left for the item.
- status: A string representing the status of the item.
"""
db = DBManager(db_path)
db.insert('ACTIVE_ITEMS', (item_id, item_name, user_id,
user_name, valid_until_epoch, uses_left, status))
db.close()
def new_transaction(trans_type, recip_username, recip_id, recip_newbal, sender_username, sender_id, sender_newbal, amount, note):
"""
A function which allows you to create a new transaction.
Accepts eight arguments:
- type: A string representing the type of transaction.
- recip_username: A string representing the recipient's username.
- recip_id: A string representing the recipient's ID.
- recip_newbal: An integer representing the recipient's new balance.
- sender_username: A string representing the sender's username.
- sender_id: A string representing the sender's ID.
- sender_newbal: An integer representing the sender's new balance.
- amount: An integer representing the amount of coins transferred.
- note: A string representing the note attached to the transaction.
"""
db = DBManager(db_path)
db.insert('TRANSACTION_LOG',
f'{int(time.time()) - 21600}, "{trans_type}", "{recip_username}", "{recip_id}", {recip_newbal}, "{sender_username}", "{sender_id}", {sender_newbal}, {amount}, "{note}"')
db.close()
def new_gambling_entry(username: str, user_id: str, game_played: str, outcome: str, amount_won: int, bet_amount: int):
"""
A function which allows you to add a new gambling entry to the database.
Accepts six arguments:
- username: A string representing the user's username.
- user_id: A string representing the user's ID.
- game_played: A string representing the game played.
- outcome: A string representing the outcome of the game.
- amount_won: An integer representing the amount won.
- bet_amount: An integer representing the amount bet.
"""
new_transaction('GAMBLING', username, user_id, fetch_balance_by_id(
user_id), 'CASINO', '0', 0, amount_won, f'CASINO: {game_played} - {outcome}')
db = DBManager(db_path)
db.insert('GAMBLING_LOG',
f'{int(time.time() - 21600)}, "{username}", "{user_id}", "{game_played}", "{outcome}", {amount_won}, {bet_amount}')
db.close()
def most_frequent(List):
counter = 0
num = List[0]
for i in List:
curr_frequency = List.count(i)
if (curr_frequency > counter):
counter = curr_frequency
num = i
return num
def fetch_favorite_game_by_id(id):
"""
A function which allows you to fetch a user's favorite game by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('GAMBLING_LOG', 'GAME_PLAYED',
f'USER_ID == "{id}"')
db.close()
if not result:
return 'No Games Played'
else:
return (most_frequent(result)[0])
def fetch_number_of_games_played_by_id(id):
"""
A function which allows you to fetch the number of games played by a user by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('GAMBLING_LOG', 'GAME_PLAYED',
f'USER_ID == "{id}"')
db.close()
if not result:
return 0
return len(result)
def fetch_average_bet_by_id(id):
"""
A function which allows you to fetch the average bet of a user by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('GAMBLING_LOG', 'BET_AMOUNT',
f'USER_ID == "{id}"')
db.close()
if not result:
return 0
return int(sum([bet[0] for bet in result]) / len(result))
def fetch_average_winnings_by_id(id):
"""
A function which allows you to fetch the average win of a user by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('GAMBLING_LOG', 'AMOUNT_WON',
f'USER_ID == "{id}"')
db.close()
if not result:
return 0
return int(sum([win[0] for win in result]) / len(result))
def fetch_winrate_by_id(id):
"""
A function which allows you to fetch the winrate of a user by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('GAMBLING_LOG', 'OUTCOME',
f'USER_ID == "{id}"')
db.close()
wins = 0
if not result:
return 0
for game in result:
if game[0] == 'WIN':
wins += 1
return (wins / len(result)) * 100
def fetch_biggest_win_by_id(id):
"""
A function which allows you to fetch the biggest win of a user by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('GAMBLING_LOG', 'AMOUNT_WON',
f'USER_ID == "{id}"')
db.close()
if not result:
return 0
return max([win[0] for win in result])
def fetch_biggest_loss_by_id(id):
"""
A function which allows you to fetch the biggest loss of a user by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('GAMBLING_LOG', 'AMOUNT_WON',
f'USER_ID == "{id}"')
db.close()
if not result:
return 0
return min([bet[0] for bet in result])
def fetch_highest_balance_by_id(user_id):
"""
A function which allows you to fetch the highest balance of a user by their ID.
Accepts one argument, a string representing the user's ID.
"""
db = DBManager(db_path)
result = db.select('TRANSACTION_LOG', 'RECIPIENT_NEW_BALANCE',
f'RECIPIENT_ID == "{user_id}"')
db.close()