-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdb_handler.py
87 lines (70 loc) · 2.48 KB
/
db_handler.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
import sqlite3
from sqlite3 import Error, IntegrityError
def db_connection():
conn = sqlite3.connect('orianna_bot.db')
c = conn.cursor()
return conn, c
def create_user(discord_id, author, summoner_name, summoner_id, account_id, puu_id):
conn, c = db_connection()
params = (discord_id, author, summoner_name, summoner_id, account_id, puu_id)
try:
with conn:
c.execute("INSERT INTO users VALUES (?, ?, ?, ?, ?, ?)", params)
return True
except IntegrityError:
return False
def remove_user(discord_id):
conn, c = db_connection()
try:
with conn:
c.execute("SELECT summoner_name FROM users WHERE discord_id = ?", (discord_id,))
summoner_name = c.fetchone()[0]
c.execute("DELETE FROM users WHERE discord_id = ?", (discord_id,))
print("{} removed from user database".format(discord_id))
return True, summoner_name
except TypeError:
return False, None
def get_summoner_name(discord_id):
# If user exists in database, return summoner name
# If user does not exist, return False
conn, c = db_connection()
try:
with conn:
c.execute("SELECT summoner_name FROM users WHERE discord_id = ?", (discord_id,))
summoner_name = c.fetchone()[0]
return summoner_name
except TypeError:
return False
def get_summoner_id(discord_id):
# If user exists in database, return summoner id
# If user does not exist, return False
conn, c = db_connection()
try:
with conn:
c.execute("SELECT summoner_id FROM users WHERE discord_id = ?", (discord_id,))
summoner_id = c.fetchone()[0]
return summoner_id
except TypeError:
return False
def get_account_id(discord_id):
# If user exists in database, return account id
# If user does not exist, return False
conn, c = db_connection()
try:
with conn:
c.execute("SELECT account_id FROM users WHERE discord_id = ?", (discord_id,))
account_id = c.fetchone()[0]
return account_id
except TypeError:
return False
def get_puu_id(discord_id):
# If user exists in database, return puu id
# If user does not exist, return False
conn, c = db_connection()
try:
with conn:
c.execute("SELECT puu_id FROM users WHERE discord_id = ?", (discord_id,))
puu_id = c.fetchone()[0]
return puu_id
except TypeError:
return False