This repository has been archived by the owner on May 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
66 lines (51 loc) · 1.81 KB
/
database.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
import random
import pymongo
from settings import DBuser, DBpass, DBurl
from datetime import datetime
client = pymongo.MongoClient(f"mongodb+srv://{DBuser}:{DBpass}@{DBurl}")
db = client['ohsea']
registered = db['registered_users']
verification = db['pending_verifications']
async def addVerification(user: dict):
# generate random auth codes
auth_code = None
while True:
auth_code = random.randint(100000, 999999)
# break out if auth code isn't taken
if not await authCodeTaken(auth_code):
break
# set auth code
user['auth_code'] = auth_code
# set timestamp
user['created'] = datetime.now()
# add to database
verification.insert_one(user)
async def emailTaken(email: str):
# search databases for that email
search1 = verification.find_one({"email": email})
search2 = registered.find_one({"email": email})
# return bool if it was found or not
return search1 is not None or search2 is not None
async def authCodeTaken(auth_code: int):
# search database for that auth token
search = verification.find_one({"auth_code": auth_code})
# return bool if it was found or not
return search is not None
async def idTaken(id: int):
# search database for that auth token
search = registered.find_one({"_id": id})
# return bool if it was found or not
return search is not None
async def verify(id, auth_code):
# search database for that user from auth token
user = verification.find_one({"auth_code": auth_code})
# remove from verification database
verification.delete_one(user)
# remove auth code field
user.pop('auth_code')
# update timestamp
user['created'] = datetime.now()
# update _id to discord id
user['_id'] = id
# add to registered database
registered.insert_one(user)