diff --git a/bot.py b/bot.py index 06f63f8..60b08c5 100644 --- a/bot.py +++ b/bot.py @@ -166,44 +166,27 @@ CREATE TABLE IF NOT EXISTS user_data ( db_cursor.execute(create_table_query) db_connection.commit() -def insert_user_data(user_id, guild_id, permission, points, ban, askmultus, filter_value, chat_history, xp=0, level=1): - """Fügt neue Benutzerdaten in die Datenbank ein.""" - # Zuerst prüfen, ob die Daten bereits existieren - if user_data_exists(user_id, guild_id): - print(f"Data for user_id {user_id} and guild_id {guild_id} already exists. Skipping insertion.") - return - +def insert_user_data(user_id, guild_id, permission, points, ban, askmultus, filter_value, chat_history): insert_query = """ INSERT INTO user_data (user_id, guild_id, permission, points, ban, askmultus, filter_value, rank, chat_history, xp, level) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """ serialized_chat_history = json.dumps(chat_history) - data = (user_id, guild_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history, xp, level) - + data = (user_id, guild_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history, 0, 1) # Initialisiere XP auf 0 und Level auf 1 try: db_cursor.execute(insert_query, data) db_connection.commit() - print(f"User data for user_id {user_id} and guild_id {guild_id} inserted successfully.") + print("User data inserted successfully.") except Exception as e: print(f"Error inserting user data: {e}") db_connection.rollback() -def user_data_exists(user_id, guild_id): - """Überprüft, ob bereits Daten für die Kombination aus user_id und guild_id existieren.""" - connection = connect_to_database() - cursor = connection.cursor() - select_query = "SELECT 1 FROM user_data WHERE user_id = %s AND guild_id = %s" - cursor.execute(select_query, (user_id, guild_id)) - result = cursor.fetchone() - cursor.close() - close_database_connection(connection) - return result is not None - def update_user_data(user_id, guild_id, field, value): - global db_connection, db_cursor # Verbindung global nutzen + global db_connection, db_cursor # global-Deklaration muss vor dem Zugriff erfolgen try: update_query = f"UPDATE user_data SET {field} = %s WHERE user_id = %s AND guild_id = %s" + # Überprüfen, ob das Feld 'chat_history' aktualisiert wird if field == 'chat_history': serialized_chat_history = json.dumps(value) db_cursor.execute(update_query, (serialized_chat_history, user_id, guild_id)) @@ -214,16 +197,17 @@ def update_user_data(user_id, guild_id, field, value): except mysql.connector.Error as err: logger.error(f"Database error: {err}") - # Überprüfen, ob die Verbindung verloren gegangen ist und neu aufbauen - if not db_connection.is_connected(): + if db_connection.is_connected(): db_cursor.close() db_connection.close() - db_connection = connect_to_database() - db_cursor = db_connection.cursor() - # Erneut versuchen, die Daten zu aktualisieren - db_cursor.execute(update_query, (value, user_id, guild_id)) - db_connection.commit() + # Verbindung neu aufbauen + db_connection = connect_to_database() + db_cursor = db_connection.cursor() + + # Wiederhole die Abfrage nach dem erneuten Verbinden + update_user_data(user_id, guild_id, field, value) + def connect_to_database(): return mysql.connector.connect( @@ -248,6 +232,7 @@ def load_user_data_from_mysql(user_id, guild_id): close_database_connection(connection) if result: + # Wenn das Level und die XP nicht vorhanden sind, initialisieren user_data = { "user_id": result[0], "guild_id": result[1], @@ -258,24 +243,27 @@ def load_user_data_from_mysql(user_id, guild_id): "filter_value": result[6], "rank": result[7], "chat_history": json.loads(result[8]) if result[8] else [], + "asknotes_history": json.loads(result[9]) if result[9] else [], "xp": result[10], "level": result[11] } else: - # Falls keine Benutzerdaten vorhanden sind, neue Daten anlegen + # Falls keine Benutzerdaten gefunden werden, initialisiere sie neu user_data = { "user_id": user_id, "guild_id": guild_id, - "permission": 0, - "points": 0, - "ban": 0, - "askmultus": 0, - "filter_value": 0, - "rank": 0, - "chat_history": [], - "xp": 0, - "level": 1 + "permission": 0, # Standardberechtigung + "points": 0, # Standardpunkte + "ban": 0, # Standardbannstatus + "askmultus": 0, # Standardwert für askmultus + "filter_value": 0, # Standardwert für Filter + "rank": 0, # Standardrang + "chat_history": [], # Leerer Chatverlauf + "asknotes_history": [], # Leerer Chatverlauf + "xp": 0, # Standard-XP + "level": 1 # Standardlevel } + insert_user_data( user_data["user_id"], user_data["guild_id"], @@ -285,6 +273,7 @@ def load_user_data_from_mysql(user_id, guild_id): user_data["askmultus"], user_data["filter_value"], user_data["chat_history"], + user_data["asknotes_history"], user_data["xp"], user_data["level"] ) @@ -313,7 +302,7 @@ def load_user_data(user_id, guild_id): if (user_id, guild_id) in cached_user_data: return cached_user_data[(user_id, guild_id)] - # Daten aus der Datenbank laden oder neu anlegen + # Daten aus der Datenbank laden oder einfügen user_data = load_user_data_from_mysql(user_id, guild_id) asyncio.ensure_future(cache_user_data(user_id, guild_id, user_data)) @@ -462,7 +451,6 @@ async def on_interaction(interaction): # Logge Interaktionen, die nicht den erwarteten Typ haben logger.error(f"Unbekannte Interaktion: {interaction.type}, Daten: {interaction.data}") - def read_introduction(): try: with open("introduction.txt", "r", encoding="utf-8") as file: