Some changes
This commit is contained in:
100
data/DatabaseConnection.py
Executable file
100
data/DatabaseConnection.py
Executable file
@@ -0,0 +1,100 @@
|
||||
import mysql.connector
|
||||
|
||||
class WrongMinecraftName(Exception):
|
||||
pass
|
||||
class SQLInsertError(Exception):
|
||||
pass
|
||||
class UnLinkError(Exception):
|
||||
pass
|
||||
class PlayerNotLinked(Exception):
|
||||
pass
|
||||
|
||||
class DatabaseConnection:
|
||||
# databases -> worldcraft and worldcraft_discord
|
||||
def __init__(self, database="worldcraft_discord"):
|
||||
if database == "worldcraft_discord":
|
||||
self.mydb = mysql.connector.connect(
|
||||
host="192.168.1.251",
|
||||
user="worldcraft_discord",
|
||||
password="aquev5vcwhLwTdRt",
|
||||
database=database
|
||||
)
|
||||
elif database == "s13_worldcraft":
|
||||
self.mydb = mysql.connector.connect(
|
||||
host="192.168.1.251",
|
||||
user="u13_f2zpWZpIKY",
|
||||
password="@fZQ6Uu3+U^WH1i2JNemgTC7",
|
||||
database=database
|
||||
)
|
||||
|
||||
self.cursor = self.mydb.cursor()
|
||||
|
||||
def get_db(self):
|
||||
return self.mydb
|
||||
|
||||
def get_cursor(self):
|
||||
return self.cursor
|
||||
|
||||
def close(self):
|
||||
self.mydb.close()
|
||||
|
||||
# this will save the uuid of the player and discord id
|
||||
class PlayerDBLinker:
|
||||
def __init__(self):
|
||||
self.tablename = "playerlinks"
|
||||
self.discorddbconn = DatabaseConnection(database="worldcraft_discord")
|
||||
self.serverdbconn = DatabaseConnection(database="s13_worldcraft")
|
||||
# check if table exists
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("create table IF NOT EXISTS {} (id int NOT NULL PRIMARY KEY AUTO_INCREMENT, minecraftUUID varchar(36) NOT NULL, discordid TINYTEXT NOT NULL)".format(self.tablename))
|
||||
|
||||
def checkPlayerLink(self, minecraftUUID, discordid):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("select count(*) from {} where minecraftUUID='{}' AND discordid='{}'".format(self.tablename, minecraftUUID, discordid))
|
||||
res = cursor.fetchone()
|
||||
return res[0] == 1
|
||||
|
||||
def minecraftUUIDused(self, minecraftUUID):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("select count(*) from {} where minecraftUUID='{}'".format(self.tablename, minecraftUUID))
|
||||
res = cursor.fetchone()
|
||||
return res[0] >= 1
|
||||
|
||||
def discordidused(self, discordid):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
cursor.execute("select count(*) from {} where discordid='{}'".format(self.tablename, discordid))
|
||||
res = cursor.fetchone()
|
||||
return res[0] >= 1
|
||||
|
||||
def linkPlayer(self, minecraftUUID, discordid):
|
||||
cursor = self.discorddbconn.get_cursor()
|
||||
sql = "insert into {} (minecraftUUID, discordid) VALUES (%s, %s)".format(self.tablename)
|
||||
val = (minecraftUUID, discordid)
|
||||
|
||||
try:
|
||||
cursor.execute(sql, val)
|
||||
self.discorddbconn.get_db().commit()
|
||||
except:
|
||||
raise SQLInsertError()
|
||||
|
||||
|
||||
def unlinkPlayer(self, minecraftname, discordid):
|
||||
# get uuid from server database -> check if in linkerdatabase
|
||||
serverdb_cursor = self.serverdbconn.get_cursor()
|
||||
discorddb_cursor = self.discorddbconn.get_cursor()
|
||||
sql = "SELECT playerUUID FROM playerprofiles WHERE playerName = '{}' LIMIT 1".format(minecraftname)
|
||||
serverdb_cursor.execute(sql)
|
||||
res = serverdb_cursor.fetchone()
|
||||
if res == None:
|
||||
raise WrongMinecraftName()
|
||||
playeruuid = res[0]
|
||||
if self.checkPlayerLink(playeruuid, discordid):
|
||||
sql2 = f"DELETE FROM {self.tablename} WHERE minecraftUUID = '{playeruuid}' AND discordid = '{discordid}'"
|
||||
discorddb_cursor.execute(sql2)
|
||||
res2 = self.discorddbconn.get_db().commit()
|
||||
else:
|
||||
raise PlayerNotLinked()
|
||||
|
||||
def closeconnections(self):
|
||||
self.discorddbconn.close()
|
||||
self.serverdbconn.close()
|
||||
Reference in New Issue
Block a user