modified: bot.py

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

67
bot.py
View File

@@ -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"): 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 = connect_to_database() connection = None
cursor = connection.cursor() cursor = None
winner_uuid = uuid.uuid4() try:
insert_query = """ connection = connect_to_database()
INSERT INTO giveaway_data (guild_id, uuid, platform, name, game_key, winner_dc_id) cursor = connection.cursor()
VALUES (%s, %s, %s, %s, %s, %s) winner_uuid = uuid.uuid4()
""" insert_query = """
data = (guild_id, str(winner_uuid), platform, name, game_key, winner_dc_id) INSERT INTO giveaway_data (guild_id, uuid, platform, name, game_key, winner_dc_id)
cursor.execute(insert_query, data) VALUES (%s, %s, %s, %s, %s, %s)
connection.commit() """
cursor.close() data = (guild_id, str(winner_uuid), platform, name, game_key, winner_dc_id)
connection.close() cursor.execute(insert_query, data)
return winner_uuid 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): 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}'")
user_data = load_user_data(winner.id, giveaway.guild_id) for i, winner in enumerate(winners):
# Erstelle einen eigenen Datenbankeintrag für diesen Gewinner mit eigener UUID try:
winner_uuid = save_winner_to_db(giveaway.guild_id, giveaway.platform, giveaway.title, winner.id) user_data = load_user_data(winner.id, giveaway.guild_id)
await winner.send(f"🎁 Congratulations! You won the giveaway '{giveaway.title}'!\n" # Erstelle einen eigenen Datenbankeintrag für diesen Gewinner mit eigener UUID
f"Please claim your prize using the following link: {GIVEAWAY_WEBSITE_URL}{giveaway.guild_id}/{winner_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: 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.")