-
Notifications
You must be signed in to change notification settings - Fork 0
/
databaseFunctions.py
133 lines (96 loc) · 3.66 KB
/
databaseFunctions.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
import sqlite3
def connect_to_database():
conn = sqlite3.connect("queuebot.db")
conn.row_factory = sqlite3.Row
return conn
def get_text_id(guildId):
# connects to database and initializes cursor
conn = connect_to_database()
c = conn.cursor()
# fetches text and voice channel id
c.execute("SELECT text_channel_id FROM preferences WHERE guild_id = ?", [guildId])
row = c.fetchone()
if not row:
conn.close()
return None
conn.close()
return row["text_channel_id"]
def get_voice_id(guildId):
# connects to database and initializes cursor
conn = connect_to_database()
c = conn.cursor()
# fetches text and voice channel id
c.execute("SELECT voice_channel_id FROM preferences WHERE guild_id = ?", [guildId])
row = c.fetchone()
if not row:
conn.close()
return None
conn.close()
return row["voice_channel_id"]
def get_bot_prefix(guildId):
conn = connect_to_database()
c = conn.cursor()
c.execute("SELECT bot_prefix FROM preferences WHERE guild_id = ?", [guildId])
botPrefix = c.fetchone()["bot_prefix"]
conn.close()
return botPrefix
def get_queue_save_time(guildId):
conn = connect_to_database()
c = conn.cursor()
c.execute("SELECT save_time FROM preferences WHERE guild_id = ?", [guildId])
queueSaveTime = c.fetchone()["save_time"]
conn.close
return queueSaveTime
def set_voice_id(guildId, voiceId):
conn = connect_to_database()
c = conn.cursor()
c.execute("UPDATE preferences SET voice_channel_id = ? WHERE guild_id = ?", [voiceId, guildId])
conn.commit()
conn.close()
def set_queue_save_time_id(guildId, queueSaveTime):
conn = connect_to_database()
c = conn.cursor()
c.execute("UPDATE preferences SET save_time = ? WHERE guild_id = ?", [queueSaveTime, guildId])
conn.commit()
conn.close()
def set_text_id(guildId, textId):
conn = connect_to_database()
c = conn.cursor()
c.execute("UPDATE preferences SET text_channel_id = ? WHERE guild_id = ?", [textId, guildId])
conn.commit()
conn.close()
def set_bot_prefix(guildId, botPrefix):
conn = connect_to_database()
c = conn.cursor()
c.execute("UPDATE preferences SET bot_prefix = ? WHERE guild_id = ?", [botPrefix, guildId])
conn.commit()
conn.close()
def get_queue_times(guildId, memberId, queueCountToSelect):
conn = connect_to_database()
c = conn.cursor()
c.execute(
"SELECT timestamp, time_in_queue FROM queueinfo WHERE guild_id = ? AND member_id = ? ORDER BY timestamp DESC",
[guildId, memberId])
rowList = c.fetchmany(queueCountToSelect)
conn.close()
return rowList
def write_member_info(guildId, memberId, unixTime, timeSpentInQueue):
conn = connect_to_database()
c = conn.cursor()
c.execute("INSERT INTO queueinfo (guild_id, member_id, timestamp, time_in_queue) VALUES (?, ?, ?, ?)",
[guildId, memberId, unixTime, timeSpentInQueue])
conn.commit()
conn.close()
def create_default_preferences_if_not_in_db(guildId):
conn = connect_to_database()
c = conn.cursor()
c.execute("SELECT guild_id FROM preferences")
rowList = c.fetchall()
# If the guild id already has a preferences entry then it won't create preferences, and so will return, if it does
# not return then the function will create default prefs
for row in rowList:
if row["guild_id"] == guildId:
return
c.execute("INSERT INTO preferences (guild_id) VALUES (?)", [guildId])
conn.commit()
conn.close()