From 87f959e596e828376e8bb89b228b47fef01b9cc6 Mon Sep 17 00:00:00 2001 From: SimolZimol <70102430+SimolZimol@users.noreply.github.com> Date: Sat, 16 Aug 2025 10:28:50 +0200 Subject: [PATCH] modified: bot.py --- bot.py | 67 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/bot.py b/bot.py index 8125742..dd61bf1 100644 --- a/bot.py +++ b/bot.py @@ -356,19 +356,31 @@ def save_giveaway_to_db(guild_id, platform, name, prize_uuid, 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""" - connection = connect_to_database() - cursor = connection.cursor() - winner_uuid = uuid.uuid4() - insert_query = """ - INSERT INTO giveaway_data (guild_id, uuid, platform, name, game_key, winner_dc_id) - VALUES (%s, %s, %s, %s, %s, %s) - """ - data = (guild_id, str(winner_uuid), platform, name, game_key, winner_dc_id) - cursor.execute(insert_query, data) - connection.commit() - cursor.close() - connection.close() - return winner_uuid + connection = None + cursor = None + try: + connection = connect_to_database() + cursor = connection.cursor() + winner_uuid = uuid.uuid4() + insert_query = """ + INSERT INTO giveaway_data (guild_id, uuid, platform, name, game_key, winner_dc_id) + VALUES (%s, %s, %s, %s, %s, %s) + """ + data = (guild_id, str(winner_uuid), platform, name, game_key, winner_dc_id) + cursor.execute(insert_query, data) + connection.commit() + logger.info(f"Successfully saved winner to database: UUID={winner_uuid}, winner_dc_id={winner_dc_id}") + return winner_uuid + except Exception as e: + logger.error(f"Error saving winner to database: {e}") + if connection: + connection.rollback() + raise e + finally: + if cursor: + cursor.close() + if connection: + connection.close() def update_winner_in_db(guild_id, prize_uuid, winner_dc_id): connection = connect_to_database() @@ -410,7 +422,16 @@ class Giveaway: return datetime.now() >= self.end_time def pick_winners(self): - return random.sample(self.participants, min(self.num_winners, len(self.participants))) + available_participants = len(self.participants) + winners_to_pick = min(self.num_winners, available_participants) + logger.info(f"Picking winners: requested={self.num_winners}, available_participants={available_participants}, winners_to_pick={winners_to_pick}") + + if winners_to_pick == 0: + return [] + + winners = random.sample(self.participants, winners_to_pick) + logger.info(f"Selected {len(winners)} winners: {[winner.name for winner in winners]}") + return winners @client.hybrid_command() async def startgiveaway(ctx, platform: str, prize: str, num_winners: int, title: str, subtitle: str, duration: str): @@ -600,12 +621,18 @@ async def check_giveaway(giveaway_id): await giveaway.ctx.send(f"🎉 Congratulations to the winners of the giveaway '{giveaway.title}'! The winners are: {winner_mentions}") # Notify and update the winners - Erstelle für jeden Gewinner einen eigenen DB-Eintrag - for winner in winners: - user_data = load_user_data(winner.id, giveaway.guild_id) - # Erstelle einen eigenen Datenbankeintrag für diesen Gewinner mit eigener UUID - winner_uuid = save_winner_to_db(giveaway.guild_id, giveaway.platform, giveaway.title, winner.id) - await winner.send(f"🎁 Congratulations! You won the giveaway '{giveaway.title}'!\n" - f"Please claim your prize using the following link: {GIVEAWAY_WEBSITE_URL}{giveaway.guild_id}/{winner_uuid}") + logger.info(f"Processing {len(winners)} winners for giveaway '{giveaway.title}'") + for i, winner in enumerate(winners): + try: + user_data = load_user_data(winner.id, giveaway.guild_id) + # Erstelle einen eigenen Datenbankeintrag für diesen Gewinner mit eigener UUID + logger.info(f"Creating database entry for winner {i+1}/{len(winners)}: {winner.name} (ID: {winner.id})") + winner_uuid = save_winner_to_db(giveaway.guild_id, giveaway.platform, giveaway.title, winner.id) + logger.info(f"Created winner entry with UUID: {winner_uuid}") + await winner.send(f"🎁 Congratulations! You won the giveaway '{giveaway.title}'!\n" + f"Please claim your prize using the following link: {GIVEAWAY_WEBSITE_URL}{giveaway.guild_id}/{winner_uuid}") + except Exception as e: + logger.error(f"Error processing winner {winner.name}: {e}") else: await giveaway.ctx.send(f"The giveaway '{giveaway.title}' has ended, but there were no participants.")