modified: bot.py
This commit is contained in:
59
bot.py
59
bot.py
@@ -162,22 +162,22 @@ CREATE TABLE IF NOT EXISTS user_data (
|
||||
db_cursor.execute(create_table_query)
|
||||
db_connection.commit()
|
||||
|
||||
def insert_user_data(user_id, permission=0, points=0, ban=0, askmultus=0, filter_value=0, chat_history=[]):
|
||||
def insert_user_data(user_id, permission, points, ban, askmultus, filter_value, chat_history):
|
||||
insert_query = """
|
||||
INSERT INTO user_data (user_id, permission, points, ban, askmultus, filter_value, rank, chat_history)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
||||
INSERT INTO user_data (user_id, permission, points, ban, askmultus, filter_value, rank, chat_history, xp, level)
|
||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||
"""
|
||||
# Serialize the chat_history list to a JSON string
|
||||
serialized_chat_history = json.dumps(chat_history)
|
||||
data = (user_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history) # Rank is set to 0 initially
|
||||
data = (user_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 {user_id} inserted successfully.")
|
||||
print("User data inserted successfully.")
|
||||
except Exception as e:
|
||||
print(f"Error inserting user data: {e}")
|
||||
db_connection.rollback()
|
||||
|
||||
|
||||
def update_user_data(user_id, field, value):
|
||||
update_query = f"UPDATE user_data SET {field} = %s WHERE user_id = %s"
|
||||
|
||||
@@ -210,13 +210,12 @@ def load_user_data_from_mysql(user_id):
|
||||
cursor.execute(select_query, (user_id,))
|
||||
result = cursor.fetchone()
|
||||
|
||||
if result:
|
||||
# Close the connection when we're done
|
||||
cursor.close()
|
||||
close_database_connection(connection)
|
||||
|
||||
# Return the user data as a dictionary
|
||||
return {
|
||||
if result:
|
||||
# Überprüfe, ob die Felder 'level' und 'xp' fehlen, und initialisiere sie gegebenenfalls
|
||||
user_data = {
|
||||
"user_id": result[0],
|
||||
"permission": result[1],
|
||||
"points": result[2],
|
||||
@@ -224,26 +223,36 @@ def load_user_data_from_mysql(user_id):
|
||||
"askmultus": result[4],
|
||||
"filter_value": result[5],
|
||||
"rank": result[6],
|
||||
"chat_history": json.loads(result[7]) if result[7] else []
|
||||
"chat_history": json.loads(result[7]) if result[7] else [],
|
||||
"xp": result[8] if len(result) > 8 else 0, # Setze XP auf 0, wenn es fehlt
|
||||
"level": result[9] if len(result) > 9 else 1 # Setze Level auf 1, wenn es fehlt
|
||||
}
|
||||
else:
|
||||
# Benutzer existiert noch nicht, also neuen Datensatz hinzufügen
|
||||
insert_user_data(user_id) # Hier wird ein neuer Benutzer erstellt
|
||||
cursor.close()
|
||||
close_database_connection(connection)
|
||||
|
||||
# Rückgabe des neu eingefügten Benutzers
|
||||
return {
|
||||
# Benutzer existiert noch nicht, initialisiere die Standarddaten
|
||||
user_data = {
|
||||
"user_id": user_id,
|
||||
"permission": 0, # Standard-Permission
|
||||
"points": 0, # Standard-Punkte
|
||||
"ban": 0, # Kein Ban
|
||||
"askmultus": 0, # Keine Multus-Anfragen
|
||||
"filter_value": 0,# Kein Filter
|
||||
"rank": 0, # Standardrang
|
||||
"chat_history": []# Leerer Chatverlauf
|
||||
"permission": 0,
|
||||
"points": 0,
|
||||
"ban": 0,
|
||||
"askmultus": 0,
|
||||
"filter_value": 0,
|
||||
"rank": 0,
|
||||
"chat_history": [],
|
||||
"xp": 0, # Standardmäßig 0 XP
|
||||
"level": 1 # Standardmäßig Level 1
|
||||
}
|
||||
# Füge den Benutzer in die Datenbank ein
|
||||
insert_user_data(
|
||||
user_data["user_id"],
|
||||
user_data["permission"],
|
||||
user_data["points"],
|
||||
user_data["ban"],
|
||||
user_data["askmultus"],
|
||||
user_data["filter_value"],
|
||||
user_data["chat_history"]
|
||||
)
|
||||
|
||||
return user_data
|
||||
|
||||
cached_user_data = {}
|
||||
pending_deletion = {}
|
||||
|
||||
Reference in New Issue
Block a user