forked from bliind/role-inventory-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
86 lines (75 loc) · 2.07 KB
/
db.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
import sqlite3
import uuid
def open_db():
return sqlite3.connect('db.db')
def execute(*args):
conn = open_db()
cur = conn.cursor()
cur.execute(*args)
if args[0].lower().startswith('select'):
results = cur.fetchall()
else:
conn.commit()
conn.close()
try:
return results
except: pass
def add_user(user_id: int, roles: str):
try:
execute('INSERT INTO inventory (id, user_id, roles) VALUES (?, ?, ?)', (str(uuid.uuid4()), user_id, roles))
return True
except Exception as e:
print('Failed to add user!')
print(e)
print(user_id)
print(roles)
return False
def update_user(user_id, roles):
try:
execute('UPDATE inventory SET roles = ? WHERE user_id = ?', (roles, user_id))
return True
except Exception as e:
print('Failed to update user!')
print(e)
print(user_id)
print(roles)
return False
def check_user(user_id):
if execute('SELECT * FROM inventory WHERE user_id = ?', (user_id,)):
return True
return False
def get_roles(user_id):
conn = open_db()
cur = conn.cursor()
cur.execute('SELECT roles FROM inventory WHERE user_id = ?', (user_id,))
try:
roles = cur.fetchone()[0]
conn.close()
return roles.split(',')
except Exception as e:
conn.close()
return []
def save_slot_pull(user_id, datestamp):
try:
execute('INSERT INTO gamba (id, user_id, datestamp) VALUES (?, ?, ?)', (str(uuid.uuid4()), user_id, datestamp))
return True
except Exception as e:
print('Failed to add gamba!')
print(e)
print(user_id)
print(datestamp)
return False
def get_last_slot_pull(user_id):
conn = open_db()
cur = conn.cursor()
cur.execute('SELECT * FROM gamba WHERE user_id = ?', (user_id,))
row = cur.fetchone()
conn.close()
try:
return {
"id": row[0],
"user_id": row[1],
"datestamp": row[2]
}
except:
return None