refactor, hacky duplication check
This commit is contained in:
parent
36a89ac640
commit
da1c289830
2 changed files with 79 additions and 62 deletions
37
src/database.py
Normal file
37
src/database.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
import mysql.connector
|
||||||
|
|
||||||
|
def create_connection(host, user, password, db):
|
||||||
|
return mysql.connector.connect(
|
||||||
|
host=host,
|
||||||
|
user=user,
|
||||||
|
password=password,
|
||||||
|
database=db,
|
||||||
|
)
|
||||||
|
|
||||||
|
def init_test_database(db):
|
||||||
|
cursor = db.cursor()
|
||||||
|
cursor.execute("CREATE TABLE discord_link ( admin_id int(10) UNSIGNED NOT NULL, discord_id bigint(20) UNSIGNED NOT NULL, steamid varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL);")
|
||||||
|
cursor.execute("CREATE TABLE sm_admins ( id int(10) UNSIGNED NOT NULL, authtype enum('steam', 'name', 'ip') COLLATE utf8mb4_unicode_ci NOT NULL, identity varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, password varchar(65) COLLATE utf8mb4_unicode_ci DEFAULT NULL, flags varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, name varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, immunity int(10) UNSIGNED NOT NULL);")
|
||||||
|
cursor.execute("ALTER TABLE discord_link ADD PRIMARY KEY (admin_id), ADD UNIQUE KEY admin_id (admin_id), ADD UNIQUE KEY discord_id (discord_id), ADD UNIQUE KEY steamid (steamid);")
|
||||||
|
cursor.execute("ALTER TABLE sm_admins ADD PRIMARY KEY (id), ADD UNIQUE KEY identity (identity), ADD UNIQUE KEY id (id);")
|
||||||
|
cursor.execute("ALTER TABLE sm_admins MODIFY id int(10) UNSIGNED NOT NULL AUTO_INCREMENT;")
|
||||||
|
cursor.execute("ALTER TABLE discord_link ADD CONSTRAINT discord_link_ibfk_1 FOREIGN KEY (admin_id) REFERENCES sm_admins (id);")
|
||||||
|
|
||||||
|
def create_reserved_slot(db, discord_user, steam_id):
|
||||||
|
cursor = db.cursor()
|
||||||
|
|
||||||
|
try:
|
||||||
|
sql_sm = "INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity) VALUES (%s, %s, %s, %s, %s, %s)"
|
||||||
|
val_sm = ("steam", steam_id, "", "a", discord_user.username, "5")
|
||||||
|
cursor.execute(sql_sm, val_sm)
|
||||||
|
db.commit()
|
||||||
|
|
||||||
|
id = cursor.lastrowid
|
||||||
|
|
||||||
|
sql_discord = "INSERT INTO discord_link (admin_id, discord_id, steamid) VALUES (%s, %s, %s)"
|
||||||
|
val_discord = (id, str(discord_user.id), steam_id)
|
||||||
|
cursor.execute(sql_discord, val_discord)
|
||||||
|
db.commit()
|
||||||
|
except mysql.connector.IntegrityError:
|
||||||
|
raise Exception("Duplicate entrys")
|
||||||
|
|
70
src/main.py
70
src/main.py
|
@ -1,43 +1,28 @@
|
||||||
import os
|
import os
|
||||||
import interactions
|
import interactions
|
||||||
import mysql.connector
|
import database
|
||||||
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from steamid import SteamID
|
from steamid import SteamID
|
||||||
|
|
||||||
load_dotenv()
|
def main():
|
||||||
token = os.getenv('DISCORD_TOKEN')
|
load_dotenv()
|
||||||
host = os.getenv('MYSQL_HOST')
|
token = os.getenv('DISCORD_TOKEN')
|
||||||
user = os.getenv('MYSQL_USER')
|
host = os.getenv('MYSQL_HOST')
|
||||||
password = os.getenv('MYSQL_PASSWORD')
|
user = os.getenv('MYSQL_USER')
|
||||||
db = os.getenv('MYSQL_DATABASE')
|
password = os.getenv('MYSQL_PASSWORD')
|
||||||
|
db = os.getenv('MYSQL_DATABASE')
|
||||||
|
|
||||||
bot = interactions.Client(token=token)
|
bot = interactions.Client(token=token)
|
||||||
|
|
||||||
db = mysql.connector.connect(
|
mysql = database.create_connection(host, user, password, db)
|
||||||
host=host,
|
database.init_test_database(mysql)
|
||||||
user="root",
|
|
||||||
password=password,
|
|
||||||
database=db,
|
|
||||||
)
|
|
||||||
|
|
||||||
cursor = db.cursor()
|
@bot.command(
|
||||||
|
|
||||||
def create_tables():
|
|
||||||
cursor.execute("CREATE TABLE discord_link ( admin_id int(10) UNSIGNED NOT NULL, discord_id bigint(20) UNSIGNED NOT NULL, steamid varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL);")
|
|
||||||
cursor.execute("CREATE TABLE sm_admins ( id int(10) UNSIGNED NOT NULL, authtype enum('steam', 'name', 'ip') COLLATE utf8mb4_unicode_ci NOT NULL, identity varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, password varchar(65) COLLATE utf8mb4_unicode_ci DEFAULT NULL, flags varchar(30) COLLATE utf8mb4_unicode_ci NOT NULL, name varchar(65) COLLATE utf8mb4_unicode_ci NOT NULL, immunity int(10) UNSIGNED NOT NULL);")
|
|
||||||
cursor.execute("ALTER TABLE discord_link ADD PRIMARY KEY (admin_id), ADD UNIQUE KEY admin_id (admin_id), ADD UNIQUE KEY discord_id (discord_id), ADD UNIQUE KEY steamid (steamid);")
|
|
||||||
cursor.execute("ALTER TABLE sm_admins ADD PRIMARY KEY (id), ADD UNIQUE KEY identity (identity), ADD UNIQUE KEY id (id);")
|
|
||||||
cursor.execute("ALTER TABLE sm_admins MODIFY id int(10) UNSIGNED NOT NULL AUTO_INCREMENT;")
|
|
||||||
cursor.execute("ALTER TABLE discord_link ADD CONSTRAINT discord_link_ibfk_1 FOREIGN KEY (admin_id) REFERENCES sm_admins (id);")
|
|
||||||
|
|
||||||
create_tables()
|
|
||||||
|
|
||||||
@bot.command(
|
|
||||||
name="reserve_slot",
|
name="reserve_slot",
|
||||||
description="Claim your reserved slot on the DM Server",
|
description="Claim your reserved slot on the DM Server",
|
||||||
scope=433600034983116810,
|
scope=433600034983116810,
|
||||||
options= [
|
options=[
|
||||||
interactions.Option(
|
interactions.Option(
|
||||||
name="steamid",
|
name="steamid",
|
||||||
description="Please provide your SteamID, from https://steamid.xyz/",
|
description="Please provide your SteamID, from https://steamid.xyz/",
|
||||||
|
@ -47,29 +32,24 @@ create_tables()
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
async def reserve_slot(ctx: interactions.CommandContext, steamid: str):
|
async def reserve_slot(ctx: interactions.CommandContext, steamid: str):
|
||||||
steam_id = SteamID(steamid)
|
steam_id = SteamID(steamid)
|
||||||
user = ctx.user
|
user = ctx.user
|
||||||
if steam_id.isValid() == True:
|
if steam_id.isValid() == True:
|
||||||
try:
|
try:
|
||||||
sql_sm = "INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity) VALUES (%s, %s, %s, %s, %s, %s)"
|
database.create_reserved_slot(mysql, user, steam_id.steam2(1))
|
||||||
val_sm = ("steam", steam_id.steam2(1), "", "a", user.username, "5")
|
|
||||||
cursor.execute(sql_sm, val_sm)
|
|
||||||
db.commit()
|
|
||||||
|
|
||||||
id = cursor.lastrowid
|
|
||||||
|
|
||||||
sql_discord = "INSERT INTO discord_link (admin_id, discord_id, steamid) VALUES (%s, %s, %s)"
|
|
||||||
val_discord = (id, str(user.id), steam_id.steam2(1))
|
|
||||||
cursor.execute(sql_discord, val_discord)
|
|
||||||
db.commit()
|
|
||||||
await ctx.send(f"You just claimed a reserved slot for: `{steam_id.steam2(1)}`!")
|
await ctx.send(f"You just claimed a reserved slot for: `{steam_id.steam2(1)}`!")
|
||||||
except:
|
except Exception as err:
|
||||||
await ctx.send(f"Something failed with the database, please try again later.")
|
if str(err) == "Duplicate entrys":
|
||||||
|
await ctx.send(f"You have already claimed a reserved slot!")
|
||||||
else:
|
else:
|
||||||
await ctx.send(f"You failed to give a valid SteamID as it seems, please head to <https://steamid.xyz/> and check.")
|
await ctx.send(f"Something failed with the database, please try again later.")
|
||||||
|
else:
|
||||||
|
await ctx.send(f"You failed to give a valid SteamID as it seems, please head to <https://steamid.xyz/ and check.")
|
||||||
|
|
||||||
bot.start()
|
bot.start()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
|
|
Loading…
Reference in a new issue