modified: bot.py

This commit is contained in:
SimolZimol
2025-08-16 10:28:50 +02:00
parent b484a552b7
commit 87f959e596

35
bot.py
View File

@@ -356,6 +356,9 @@ 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"): 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"""
connection = None
cursor = None
try:
connection = connect_to_database() connection = connect_to_database()
cursor = connection.cursor() cursor = connection.cursor()
winner_uuid = uuid.uuid4() winner_uuid = uuid.uuid4()
@@ -366,9 +369,18 @@ def save_winner_to_db(guild_id, platform, name, winner_dc_id, game_key="PREDEFIN
data = (guild_id, str(winner_uuid), platform, name, game_key, winner_dc_id) data = (guild_id, str(winner_uuid), platform, name, game_key, winner_dc_id)
cursor.execute(insert_query, data) cursor.execute(insert_query, data)
connection.commit() connection.commit()
cursor.close() logger.info(f"Successfully saved winner to database: UUID={winner_uuid}, winner_dc_id={winner_dc_id}")
connection.close()
return winner_uuid 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): def update_winner_in_db(guild_id, prize_uuid, winner_dc_id):
connection = connect_to_database() connection = connect_to_database()
@@ -410,7 +422,16 @@ class Giveaway:
return datetime.now() >= self.end_time return datetime.now() >= self.end_time
def pick_winners(self): 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() @client.hybrid_command()
async def startgiveaway(ctx, platform: str, prize: str, num_winners: int, title: str, subtitle: str, duration: str): 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}") 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 # Notify and update the winners - Erstelle für jeden Gewinner einen eigenen DB-Eintrag
for winner in winners: 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) user_data = load_user_data(winner.id, giveaway.guild_id)
# Erstelle einen eigenen Datenbankeintrag für diesen Gewinner mit eigener UUID # 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) 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" 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}") 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: else:
await giveaway.ctx.send(f"The giveaway '{giveaway.title}' has ended, but there were no participants.") await giveaway.ctx.send(f"The giveaway '{giveaway.title}' has ended, but there were no participants.")