modified: bot.py
This commit is contained in:
32
bot.py
32
bot.py
@@ -170,17 +170,15 @@ def insert_user_data(user_id, guild_id, permission, points, ban, askmultus, filt
|
|||||||
insert_query = """
|
insert_query = """
|
||||||
INSERT INTO user_data (user_id, guild_id, permission, points, ban, askmultus, filter_value, rank, chat_history, xp, level)
|
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)
|
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
||||||
ON DUPLICATE KEY UPDATE
|
|
||||||
permission = VALUES(permission), points = VALUES(points), ban = VALUES(ban), askmultus = VALUES(askmultus), filter_value = VALUES(filter_value), rank = VALUES(rank), chat_history = VALUES(chat_history), xp = VALUES(xp), level = VALUES(level)
|
|
||||||
"""
|
"""
|
||||||
serialized_chat_history = json.dumps(chat_history)
|
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, xp, level)
|
||||||
try:
|
try:
|
||||||
db_cursor.execute(insert_query, data)
|
db_cursor.execute(insert_query, data)
|
||||||
db_connection.commit()
|
db_connection.commit()
|
||||||
print("User data inserted or updated successfully.")
|
print("User data inserted successfully.")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error inserting or updating user data: {e}")
|
print(f"Error inserting user data: {e}")
|
||||||
db_connection.rollback()
|
db_connection.rollback()
|
||||||
|
|
||||||
def update_user_data(user_id, guild_id, field, value):
|
def update_user_data(user_id, guild_id, field, value):
|
||||||
@@ -234,6 +232,7 @@ def load_user_data_from_mysql(user_id, guild_id):
|
|||||||
close_database_connection(connection)
|
close_database_connection(connection)
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
|
# Wenn das Level und die XP nicht vorhanden sind, initialisieren
|
||||||
user_data = {
|
user_data = {
|
||||||
"user_id": result[0],
|
"user_id": result[0],
|
||||||
"guild_id": result[1],
|
"guild_id": result[1],
|
||||||
@@ -249,21 +248,23 @@ def load_user_data_from_mysql(user_id, guild_id):
|
|||||||
"level": result[11]
|
"level": result[11]
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
|
# Wenn keine Benutzerdaten gefunden werden, neue Daten anlegen
|
||||||
print(f"No data found for user {user_id} in guild {guild_id}. Inserting new data.")
|
print(f"No data found for user {user_id} in guild {guild_id}. Inserting new data.")
|
||||||
user_data = {
|
user_data = {
|
||||||
"user_id": user_id,
|
"user_id": user_id,
|
||||||
"guild_id": guild_id,
|
"guild_id": guild_id,
|
||||||
"permission": 0,
|
"permission": 0, # Standardberechtigung
|
||||||
"points": 0,
|
"points": 0, # Standardpunkte
|
||||||
"ban": 0,
|
"ban": 0, # Standardbannstatus
|
||||||
"askmultus": 0,
|
"askmultus": 0, # Standardwert für askmultus
|
||||||
"filter_value": 0,
|
"filter_value": 0, # Standardwert für Filter
|
||||||
"rank": 0,
|
"rank": 0, # Standardrang
|
||||||
"chat_history": [],
|
"chat_history": [], # Leerer Chatverlauf
|
||||||
"asknotes_history": [],
|
"asknotes_history": [], # Leerer Chatverlauf
|
||||||
"xp": 0,
|
"xp": 0, # Standard-XP
|
||||||
"level": 1
|
"level": 1 # Standardlevel
|
||||||
}
|
}
|
||||||
|
|
||||||
insert_user_data(
|
insert_user_data(
|
||||||
user_data["user_id"],
|
user_data["user_id"],
|
||||||
user_data["guild_id"],
|
user_data["guild_id"],
|
||||||
@@ -301,11 +302,13 @@ def load_user_data(user_id, guild_id):
|
|||||||
if (user_id, guild_id) in cached_user_data:
|
if (user_id, guild_id) in cached_user_data:
|
||||||
return cached_user_data[(user_id, guild_id)]
|
return cached_user_data[(user_id, guild_id)]
|
||||||
|
|
||||||
|
# Daten aus der Datenbank laden oder neu anlegen
|
||||||
user_data = load_user_data_from_mysql(user_id, guild_id)
|
user_data = load_user_data_from_mysql(user_id, guild_id)
|
||||||
asyncio.ensure_future(cache_user_data(user_id, guild_id, user_data))
|
asyncio.ensure_future(cache_user_data(user_id, guild_id, user_data))
|
||||||
|
|
||||||
return user_data
|
return user_data
|
||||||
|
|
||||||
|
|
||||||
#-----------------------------------------------------------------------------------------------------------
|
#-----------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
def save_giveaway_to_db(platform, name, prize_uuid, game_key, winner_dc_id=None):
|
def save_giveaway_to_db(platform, name, prize_uuid, game_key, winner_dc_id=None):
|
||||||
@@ -449,6 +452,7 @@ async def on_interaction(interaction):
|
|||||||
# Logge Interaktionen, die nicht den erwarteten Typ haben
|
# Logge Interaktionen, die nicht den erwarteten Typ haben
|
||||||
logger.error(f"Unbekannte Interaktion: {interaction.type}, Daten: {interaction.data}")
|
logger.error(f"Unbekannte Interaktion: {interaction.type}, Daten: {interaction.data}")
|
||||||
|
|
||||||
|
|
||||||
def read_introduction():
|
def read_introduction():
|
||||||
try:
|
try:
|
||||||
with open("introduction.txt", "r", encoding="utf-8") as file:
|
with open("introduction.txt", "r", encoding="utf-8") as file:
|
||||||
|
|||||||
Reference in New Issue
Block a user