modified: bot.py
This commit is contained in:
101
bot.py
101
bot.py
@@ -180,7 +180,9 @@ def get_database_cursor():
|
|||||||
|
|
||||||
pool = mysql.connector.pooling.MySQLConnectionPool(
|
pool = mysql.connector.pooling.MySQLConnectionPool(
|
||||||
pool_name="mypool",
|
pool_name="mypool",
|
||||||
pool_size=10,
|
pool_size=30, # Erhöht von 10 auf 30
|
||||||
|
pool_reset_session=True,
|
||||||
|
autocommit=True,
|
||||||
host=DB_HOST,
|
host=DB_HOST,
|
||||||
port=DB_PORT,
|
port=DB_PORT,
|
||||||
user=DB_USER,
|
user=DB_USER,
|
||||||
@@ -189,21 +191,29 @@ pool = mysql.connector.pooling.MySQLConnectionPool(
|
|||||||
)
|
)
|
||||||
|
|
||||||
def connect_to_database():
|
def connect_to_database():
|
||||||
return pool.get_connection()
|
"""Holt eine Verbindung aus dem Pool"""
|
||||||
|
try:
|
||||||
|
connection = pool.get_connection()
|
||||||
|
return connection
|
||||||
|
except mysql.connector.PoolError as e:
|
||||||
|
logger.error(f"Pool error: {e}")
|
||||||
|
raise e
|
||||||
|
|
||||||
def close_database_connection(connection):
|
def close_database_connection(connection):
|
||||||
|
"""Gibt eine Verbindung an den Pool zurück"""
|
||||||
|
if connection and connection.is_connected():
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
def load_user_data_from_mysql(user_id, guild_id):
|
def load_user_data_from_mysql(user_id, guild_id):
|
||||||
|
connection = None
|
||||||
|
cursor = None
|
||||||
|
try:
|
||||||
connection = connect_to_database()
|
connection = connect_to_database()
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
select_query = "SELECT * FROM user_data WHERE user_id = %s AND guild_id = %s"
|
select_query = "SELECT * FROM user_data WHERE user_id = %s AND guild_id = %s"
|
||||||
cursor.execute(select_query, (user_id, guild_id))
|
cursor.execute(select_query, (user_id, guild_id))
|
||||||
result = cursor.fetchone()
|
result = cursor.fetchone()
|
||||||
|
|
||||||
cursor.close()
|
|
||||||
close_database_connection(connection)
|
|
||||||
|
|
||||||
if result:
|
if result:
|
||||||
user_data = {
|
user_data = {
|
||||||
"user_id": result[0],
|
"user_id": result[0],
|
||||||
@@ -251,6 +261,29 @@ def load_user_data_from_mysql(user_id, guild_id):
|
|||||||
)
|
)
|
||||||
|
|
||||||
return user_data
|
return user_data
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error loading user data from MySQL: {e}")
|
||||||
|
# Return default user data in case of error
|
||||||
|
return {
|
||||||
|
"user_id": user_id,
|
||||||
|
"guild_id": guild_id,
|
||||||
|
"permission": 0,
|
||||||
|
"points": 0,
|
||||||
|
"ban": 0,
|
||||||
|
"askmultus": 0,
|
||||||
|
"filter_value": 0,
|
||||||
|
"rank": 0,
|
||||||
|
"chat_history": [],
|
||||||
|
"asknotes_history": [],
|
||||||
|
"xp": 0,
|
||||||
|
"level": 1,
|
||||||
|
"nickname": ""
|
||||||
|
}
|
||||||
|
finally:
|
||||||
|
if cursor:
|
||||||
|
cursor.close()
|
||||||
|
if connection:
|
||||||
|
close_database_connection(connection)
|
||||||
|
|
||||||
cached_user_data = {}
|
cached_user_data = {}
|
||||||
pending_deletion = {}
|
pending_deletion = {}
|
||||||
@@ -281,17 +314,28 @@ def load_user_data(user_id, guild_id):
|
|||||||
return user_data
|
return user_data
|
||||||
|
|
||||||
def get_global_permission(user_id):
|
def get_global_permission(user_id):
|
||||||
|
connection = None
|
||||||
|
cursor = None
|
||||||
|
try:
|
||||||
connection = connect_to_database()
|
connection = connect_to_database()
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
select_query = "SELECT global_permission FROM bot_data WHERE user_id = %s"
|
select_query = "SELECT global_permission FROM bot_data WHERE user_id = %s"
|
||||||
cursor.execute(select_query, (user_id,))
|
cursor.execute(select_query, (user_id,))
|
||||||
result = cursor.fetchone()
|
result = cursor.fetchone()
|
||||||
cursor.close()
|
|
||||||
connection.close()
|
|
||||||
|
|
||||||
return result[0] if result else None
|
return result[0] if result else None
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error getting global permission: {e}")
|
||||||
|
return None
|
||||||
|
finally:
|
||||||
|
if cursor:
|
||||||
|
cursor.close()
|
||||||
|
if connection:
|
||||||
|
close_database_connection(connection)
|
||||||
|
|
||||||
def save_global_permission(user_id, permission_level):
|
def save_global_permission(user_id, permission_level):
|
||||||
|
connection = None
|
||||||
|
cursor = None
|
||||||
|
try:
|
||||||
connection = connect_to_database()
|
connection = connect_to_database()
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
insert_query = """
|
insert_query = """
|
||||||
@@ -301,8 +345,17 @@ def save_global_permission(user_id, permission_level):
|
|||||||
"""
|
"""
|
||||||
cursor.execute(insert_query, (user_id, permission_level, permission_level))
|
cursor.execute(insert_query, (user_id, permission_level, permission_level))
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
logger.info(f"Successfully saved global permission for user {user_id}: {permission_level}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error saving global permission: {e}")
|
||||||
|
if connection:
|
||||||
|
connection.rollback()
|
||||||
|
raise e
|
||||||
|
finally:
|
||||||
|
if cursor:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
connection.close()
|
if connection:
|
||||||
|
close_database_connection(connection)
|
||||||
|
|
||||||
#-----------------------------------------------------------------------------------------------------------
|
#-----------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
@@ -342,6 +395,9 @@ async def update_all_users(batch_size=20, pause_duration=1):
|
|||||||
await asyncio.sleep(pause_duration)
|
await asyncio.sleep(pause_duration)
|
||||||
|
|
||||||
def save_giveaway_to_db(guild_id, platform, name, prize_uuid, game_key):
|
def save_giveaway_to_db(guild_id, platform, name, prize_uuid, game_key):
|
||||||
|
connection = None
|
||||||
|
cursor = None
|
||||||
|
try:
|
||||||
connection = connect_to_database()
|
connection = connect_to_database()
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
insert_query = """
|
insert_query = """
|
||||||
@@ -351,8 +407,17 @@ def save_giveaway_to_db(guild_id, platform, name, prize_uuid, game_key):
|
|||||||
data = (guild_id, str(prize_uuid), platform, name, game_key)
|
data = (guild_id, str(prize_uuid), platform, name, game_key)
|
||||||
cursor.execute(insert_query, data)
|
cursor.execute(insert_query, data)
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
logger.info(f"Successfully saved giveaway to database: UUID={prize_uuid}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error saving giveaway to database: {e}")
|
||||||
|
if connection:
|
||||||
|
connection.rollback()
|
||||||
|
raise e
|
||||||
|
finally:
|
||||||
|
if cursor:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
connection.close()
|
if connection:
|
||||||
|
close_database_connection(connection)
|
||||||
|
|
||||||
def save_winner_to_db(guild_id, platform, name, winner_dc_id, game_key="PREDEFINED_GAME_KEY"):
|
def save_winner_to_db(guild_id, platform, name, winner_dc_id, game_key="PREDEFINED_GAME_KEY"):
|
||||||
"""Erstellt einen eigenen Datenbankeintrag für jeden Gewinner mit eigener UUID"""
|
"""Erstellt einen eigenen Datenbankeintrag für jeden Gewinner mit eigener UUID"""
|
||||||
@@ -380,9 +445,12 @@ def save_winner_to_db(guild_id, platform, name, winner_dc_id, game_key="PREDEFIN
|
|||||||
if cursor:
|
if cursor:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
if connection:
|
if connection:
|
||||||
connection.close()
|
close_database_connection(connection)
|
||||||
|
|
||||||
def update_winner_in_db(guild_id, prize_uuid, winner_dc_id):
|
def update_winner_in_db(guild_id, prize_uuid, winner_dc_id):
|
||||||
|
connection = None
|
||||||
|
cursor = None
|
||||||
|
try:
|
||||||
connection = connect_to_database()
|
connection = connect_to_database()
|
||||||
cursor = connection.cursor()
|
cursor = connection.cursor()
|
||||||
update_query = """
|
update_query = """
|
||||||
@@ -391,8 +459,17 @@ def update_winner_in_db(guild_id, prize_uuid, winner_dc_id):
|
|||||||
data = (winner_dc_id, str(prize_uuid), guild_id)
|
data = (winner_dc_id, str(prize_uuid), guild_id)
|
||||||
cursor.execute(update_query, data)
|
cursor.execute(update_query, data)
|
||||||
connection.commit()
|
connection.commit()
|
||||||
|
logger.info(f"Successfully updated winner in database: UUID={prize_uuid}, winner_dc_id={winner_dc_id}")
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"Error updating winner in database: {e}")
|
||||||
|
if connection:
|
||||||
|
connection.rollback()
|
||||||
|
raise e
|
||||||
|
finally:
|
||||||
|
if cursor:
|
||||||
cursor.close()
|
cursor.close()
|
||||||
connection.close()
|
if connection:
|
||||||
|
close_database_connection(connection)
|
||||||
|
|
||||||
class Giveaway:
|
class Giveaway:
|
||||||
def __init__(self, ctx, platform, prize, num_winners, title, subtitle, duration, end_time):
|
def __init__(self, ctx, platform, prize, num_winners, title, subtitle, duration, end_time):
|
||||||
|
|||||||
Reference in New Issue
Block a user