allow test channel, make database a class for easier use

This commit is contained in:
Philipp 2022-10-03 19:35:06 +02:00
parent b78b029855
commit 8b07505672
Signed by: Philipp
GPG key ID: 9EBD8439AFBAB750
2 changed files with 37 additions and 37 deletions

View file

@ -1,15 +1,16 @@
import mysql.connector import mysql.connector
def create_connection(host, user, password, db): class Database:
return mysql.connector.connect( def __init__(self, host, user, password, db):
self.con = mysql.connector.connect(
host=host, host=host,
user=user, user=user,
password=password, password=password,
database=db, database=db,
) )
def init_test_database(db): def init_test_database(self):
cursor = db.cursor() cursor = self.con.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 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("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 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);")
@ -17,21 +18,20 @@ def init_test_database(db):
cursor.execute("ALTER TABLE sm_admins MODIFY id int(10) UNSIGNED NOT NULL AUTO_INCREMENT;") 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);") 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): def create_reserved_slot(self, discord_user, steam_id):
cursor = db.cursor() cursor = self.con.cursor()
try: try:
sql_sm = "INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity) VALUES (%s, %s, %s, %s, %s, %s)" 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") val_sm = ("steam", steam_id, "", "a", discord_user.username, "5")
cursor.execute(sql_sm, val_sm) cursor.execute(sql_sm, val_sm)
db.commit() self.con.commit()
id = cursor.lastrowid id = cursor.lastrowid
sql_discord = "INSERT INTO discord_link (admin_id, discord_id, steamid) VALUES (%s, %s, %s)" sql_discord = "INSERT INTO discord_link (admin_id, discord_id, steamid) VALUES (%s, %s, %s)"
val_discord = (id, str(discord_user.id), steam_id) val_discord = (id, str(discord_user.id), steam_id)
cursor.execute(sql_discord, val_discord) cursor.execute(sql_discord, val_discord)
db.commit() self.con.commit()
except mysql.connector.IntegrityError: except mysql.connector.IntegrityError:
raise Exception("Duplicate entrys") raise Exception("Duplicate entrys")

View file

@ -2,8 +2,8 @@ import os
import sys import sys
import signal import signal
import interactions import interactions
import database
from database import Database
from dotenv import load_dotenv from dotenv import load_dotenv
from steamid import SteamID from steamid import SteamID
from loguru import logger from loguru import logger
@ -23,7 +23,7 @@ class GracefulDeath:
for signum in catchSignals: for signum in catchSignals:
signal.signal(signum, self.handler) signal.signal(signum, self.handler)
def handler(self, signum, frame): def handler(self, signum):
self.lastSignal=signum self.lastSignal=signum
self.receivedSignal=True self.receivedSignal=True
if signum in [2, 3, 15]: if signum in [2, 3, 15]:
@ -40,10 +40,10 @@ def main(args):
bot = interactions.Client(token=token) bot = interactions.Client(token=token)
mysql = database.create_connection(host, user, password, db) con = Database(host, user, password, db)
try: try:
if args[0] == "init_database": if args[0] == "init_database":
database.init_test_database(mysql) con.init_test_database()
logger.info(f"First init of database... Please disable this afterwards with the flag init_dabase") logger.info(f"First init of database... Please disable this afterwards with the flag init_dabase")
except: except:
logger.info(f"Skipping database init...") logger.info(f"Skipping database init...")
@ -67,7 +67,7 @@ def main(args):
channel = ctx.channel.id channel = ctx.channel.id
user = ctx.user user = ctx.user
if channel != "433738529898627073": if channel != "433738529898627073" and channel != "441985560144379912":
await ctx.send(f"This command only works in the Channel <#433738529898627073>!", ephemeral=True) await ctx.send(f"This command only works in the Channel <#433738529898627073>!", ephemeral=True)
logger.debug(f"Command was used in the wrong channel. ({channel})") logger.debug(f"Command was used in the wrong channel. ({channel})")
return return
@ -85,7 +85,7 @@ def main(args):
return return
try: try:
database.create_reserved_slot(mysql, user, steam_id.steam2(1)) con.create_reserved_slot(user, steam_id.steam2(1))
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)}`!")
logger.debug(f"{user.id} with SteamID: {steam_id.steam2(1)} has just claimed a reserved slot.") logger.debug(f"{user.id} with SteamID: {steam_id.steam2(1)} has just claimed a reserved slot.")
except Exception as err: except Exception as err: