modified: bot.py
This commit is contained in:
55
bot.py
55
bot.py
@@ -162,18 +162,18 @@ CREATE TABLE IF NOT EXISTS user_data (
|
|||||||
db_cursor.execute(create_table_query)
|
db_cursor.execute(create_table_query)
|
||||||
db_connection.commit()
|
db_connection.commit()
|
||||||
|
|
||||||
def insert_user_data(user_id, permission, points, ban, askmultus, filter_value, chat_history):
|
def insert_user_data(user_id, permission=0, points=0, ban=0, askmultus=0, filter_value=0, chat_history=[]):
|
||||||
insert_query = """
|
insert_query = """
|
||||||
INSERT INTO user_data (user_id, permission, points, ban, askmultus, filter_value, rank, chat_history)
|
INSERT INTO user_data (user_id, permission, points, ban, askmultus, filter_value, rank, chat_history)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
|
||||||
"""
|
"""
|
||||||
# Serialize the chat_history list to a JSON string
|
# Serialize the chat_history list to a JSON string
|
||||||
serialized_chat_history = json.dumps(chat_history)
|
serialized_chat_history = json.dumps(chat_history)
|
||||||
data = (user_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history) # Setze den Rang initial auf 0
|
data = (user_id, permission, points, ban, askmultus, filter_value, 0, serialized_chat_history) # Rank is set to 0 initially
|
||||||
try:
|
try:
|
||||||
db_cursor.execute(insert_query, data)
|
db_cursor.execute(insert_query, data)
|
||||||
db_connection.commit()
|
db_connection.commit()
|
||||||
print("User data inserted successfully.")
|
print(f"User {user_id} inserted successfully.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error inserting user data: {e}")
|
print(f"Error inserting user data: {e}")
|
||||||
db_connection.rollback()
|
db_connection.rollback()
|
||||||
@@ -210,10 +210,12 @@ def load_user_data_from_mysql(user_id):
|
|||||||
cursor.execute(select_query, (user_id,))
|
cursor.execute(select_query, (user_id,))
|
||||||
result = cursor.fetchone()
|
result = cursor.fetchone()
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
close_database_connection(connection)
|
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
|
# Close the connection when we're done
|
||||||
|
cursor.close()
|
||||||
|
close_database_connection(connection)
|
||||||
|
|
||||||
|
# Return the user data as a dictionary
|
||||||
return {
|
return {
|
||||||
"user_id": result[0],
|
"user_id": result[0],
|
||||||
"permission": result[1],
|
"permission": result[1],
|
||||||
@@ -225,32 +227,22 @@ def load_user_data_from_mysql(user_id):
|
|||||||
"chat_history": json.loads(result[7]) if result[7] else []
|
"chat_history": json.loads(result[7]) if result[7] else []
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
return None
|
# 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)
|
||||||
|
|
||||||
def save_user_data_to_mysql(user_data):
|
# Rückgabe des neu eingefügten Benutzers
|
||||||
update_query = """
|
return {
|
||||||
UPDATE user_data
|
"user_id": user_id,
|
||||||
SET permission = %s,
|
"permission": 0, # Standard-Permission
|
||||||
points = %s,
|
"points": 0, # Standard-Punkte
|
||||||
ban = %s,
|
"ban": 0, # Kein Ban
|
||||||
askmultus = %s,
|
"askmultus": 0, # Keine Multus-Anfragen
|
||||||
filter_value = %s,
|
"filter_value": 0,# Kein Filter
|
||||||
rank = %s,
|
"rank": 0, # Standardrang
|
||||||
chat_history = %s
|
"chat_history": []# Leerer Chatverlauf
|
||||||
WHERE user_id = %s
|
}
|
||||||
"""
|
|
||||||
data = (
|
|
||||||
user_data["permission"],
|
|
||||||
user_data["points"],
|
|
||||||
user_data["ban"],
|
|
||||||
user_data["askmultus"],
|
|
||||||
user_data["filter_value"],
|
|
||||||
user_data["rank"],
|
|
||||||
json.dumps(user_data["chat_history"]),
|
|
||||||
user_data["user_id"]
|
|
||||||
)
|
|
||||||
db_cursor.execute(update_query, data)
|
|
||||||
db_connection.commit()
|
|
||||||
|
|
||||||
|
|
||||||
cached_user_data = {}
|
cached_user_data = {}
|
||||||
@@ -275,6 +267,7 @@ def load_user_data(user_id):
|
|||||||
if user_id in cached_user_data:
|
if user_id in cached_user_data:
|
||||||
return cached_user_data[user_id]
|
return cached_user_data[user_id]
|
||||||
|
|
||||||
|
# Daten aus der Datenbank laden oder einfügen
|
||||||
user_data = load_user_data_from_mysql(user_id)
|
user_data = load_user_data_from_mysql(user_id)
|
||||||
asyncio.ensure_future(cache_user_data(user_id, user_data))
|
asyncio.ensure_future(cache_user_data(user_id, user_data))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user