forked from gg2001/EmailBot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
bot_db.py
190 lines (164 loc) · 5.89 KB
/
bot_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
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
import sqlite3
import csv
import os
class EmailBotDB:
"""Handles SQLite database operations for EmailBot.
Uses three tables
users: (userid, guildid, email, code, verified)
A table for all user info within a guild.
guilds: (guildid, onjoin, role)
A table for all guilds a bot is in.
authenticated_emails: (email)
An automatically populated table containing
the emails of users that are allowed to join
the server.
Attributes:
conn: A connection to the database
c: A cursor for the previously mentions connection
"""
def __init__(self):
""" Initializes DB and creates tables """
self.conn = sqlite3.connect('bot.db')
self.c = self.conn.cursor()
# Create users table if one doesn't exist
self.c.execute(
"""
CREATE TABLE IF NOT EXISTS users(
userid INT,
guildid INT,
email TEXT,
code INT,
verified INT
);
"""
)
# Create guides table if one doesn't exist
self.c.execute(
"""
CREATE TABLE IF NOT EXISTS guilds(
guildid INT PRIMARY KEY,
onjoin INT,
role TEXT
);
"""
)
# Create authenticated emails table if one doesn't exist
self.c.execute(
"""
CREATE TABLE IF NOT EXISTS authenticated_emails (
email TEXT
);
"""
)
self.conn.commit()
self.populate_emails_table(os.environ.get('AUTH_EMAILS_FILEPATH'))
def get_guild(self, guildid):
""" Returns guild with specific ID """
return self.c.execute(
"SELECT * FROM guilds WHERE guildid=?",
(guildid,)
).fetchone()
def new_guild(self, guildid, onjoin=0, role="verified"):
""" Inserts new guild into the DB """
self.c.execute(
"INSERT INTO guilds VALUES (?, ?, ?)",
(guildid, onjoin, role)
)
self.conn.commit()
def get_user_guild(self, guildid, userid):
""" Returns user info within a specific guild """
return self.c.execute(
"SELECT * FROM users WHERE guildid=? AND userid=?",
(guildid, userid)
).fetchone()
def get_users_guilds(self, userid):
""" Returns user info (within all guilds) """
return self.c.execute(
"SELECT * FROM users WHERE userid=?",
(userid,)
).fetchall()
def get_emails_guilds(self, guildid, email):
""" Returns user info given email and guild """
return self.c.execute(
"SELECT * FROM users WHERE guildid=? AND email=? AND verified=1",
(guildid, email)
).fetchall()
def get_users_codes(self, userid, code):
""" Returns user info if given code matches """
return self.c.execute(
"SELECT * FROM users WHERE userid=? AND code=?",
(userid, code)
).fetchall()
def verify_message(self, guildname):
""" Display verify message """
return "To verify yourself on {}, **reply here with your email address**."\
.format(guildname)
def new_user(self, userid, guildid, email="", code=0, verified=0):
""" Inserts new user into the DB """
self.c.execute(
"INSERT INTO users VALUES (?, ?, ?, ?, ?)",
(userid, guildid, email, code, verified)
)
self.conn.commit()
def verify_user(self, userid, guildid):
""" Verifies user in the DB """
self.c.execute(
"UPDATE users SET verified=1 WHERE userid=? AND guildid=?",
(userid, guildid)
)
self.conn.commit()
def change_role(self, role, guildid):
""" Change role of verified user in a guild """
self.c.execute(
"UPDATE guilds SET role=? WHERE guildid=?",
(role, guildid)
)
self.conn.commit()
def enable_onjoin(self, guildid):
""" Enables verification on join """
self.c.execute(
"UPDATE guilds SET onjoin=? WHERE guildid=?",
(1, guildid)
)
self.conn.commit()
def disable_onjoin(self, guildid):
""" Disables verification on join """
self.c.execute(
"UPDATE guilds SET onjoin=? WHERE guildid=?",
(0, guildid)
)
self.conn.commit()
def insert_code(self, code, userid, guildid):
""" Inserts the random code for a user in the DB """
self.c.execute(
"UPDATE users SET code=? WHERE userid=? AND guildid=?",
(code, userid, guildid)
)
self.conn.commit()
def insert_email(self, email, userid, guildid):
""" Inserts email for a user in the DB """
self.c.execute(
"UPDATE users SET email=? WHERE userid=? AND guildid=?",
(email, userid, guildid)
)
self.conn.commit()
def email_check(self, email):
""" Checks that email exists in authenticated_emails """
found_email = self.c.execute(
"SELECT email FROM authenticated_emails WHERE email=?",
(email,)
).fetchone()
return found_email != None
def add_authenticated_email(self, email):
""" Inserts an email into the emails table """
self.c.execute(
"INSERT INTO authenticated_emails VALUES (?)",
(email, )
)
self.conn.commit()
def populate_emails_table(self, emails_filepath):
""" Populates authenticated email table from a file """
emails_file = open(emails_filepath, encoding='utf-8-sig')
auth_emails = csv.DictReader(emails_file, delimiter=',')
for row in auth_emails:
self.add_authenticated_email(row['email'])