-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathuser_db.py
More file actions
60 lines (53 loc) · 1.79 KB
/
user_db.py
File metadata and controls
60 lines (53 loc) · 1.79 KB
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
from database import Database
class UserDB(Database):
TABLE_NAME = "users"
TABLE_SCHEMA = f"""CREATE TABLE {TABLE_NAME}(
discord char(18),
id int
)"""
BOT_ID = "1209240468605509634"
def __init__(self, path: str):
super().__init__(path)
#
#
# Get a user's Discord ID from their unique ID.
#
# If the unique ID has not been registered,
# this function returns the bot's Discord ID.
#
# Returns:
# Corresponding Discord ID
#
#
def get_discord_id(self, unique_id: int) -> str:
command = f"""SELECT * FROM {self.TABLE_NAME} WHERE id = ?"""
self.cursor.execute(command, (unique_id,))
result = self.cursor.fetchone()
return self.BOT_ID if not result else result[0]
#
#
# Add a new Discord ID to the database.
#
# If the Discord ID is already registered,
# this function returns the previous unique ID.
#
# Returns:
# Newly generated unique ID
#
#
def add_discord_id(self, discord_id: str) -> int:
# Check if the ID has already been registered
command = f"""SELECT id FROM {self.TABLE_NAME} WHERE discord = ?"""
self.cursor.execute(command, (discord_id,))
if result := self.cursor.fetchone():
return result[0]
# Get the next sequential ID
command = f"""SELECT COUNT (*) FROM {self.TABLE_NAME}"""
self.cursor.execute(command)
result = self.cursor.fetchone()
unique_id = 0 if not result else result[0]
# Create new entry
command = f"""INSERT INTO {self.TABLE_NAME} VALUES (?, ?)"""
self.cursor.execute(command, (discord_id, unique_id,))
self.connection.commit()
return unique_id