modified: bot.py

This commit is contained in:
SimolZimol
2025-10-09 01:50:53 +02:00
parent 04dd33dc03
commit 9d1b502609

83
bot.py
View File

@@ -781,14 +781,11 @@ async def handle_expired_process(process_uuid, process_type, data):
async def handle_expired_giveaway(process_uuid, data): async def handle_expired_giveaway(process_uuid, data):
"""Handles expired giveaway processes""" """Handles expired giveaway processes"""
try: try:
giveaway_id = data.get("giveaway_id")
guild_id = data.get("guild_id") guild_id = data.get("guild_id")
channel_id = data.get("channel_id") channel_id = data.get("channel_id")
if not giveaway_id: # Use process_uuid as giveaway_id since that's our new system
logger.error(f"No giveaway_id found in process data for {process_uuid}") giveaway_id = str(process_uuid)
update_process_status(process_uuid, "failed")
return
# Try to get giveaway from memory first, then from database # Try to get giveaway from memory first, then from database
giveaway = None giveaway = None
@@ -1342,6 +1339,40 @@ def save_winner_to_db(guild_id, platform, name, winner_dc_id, game_key="PREDEFIN
if connection: if connection:
close_database_connection(connection) close_database_connection(connection)
async def process_sponsor_mention(ctx, sponsor: str):
"""Process sponsor mention to extract display name"""
try:
# Check if it's a user mention <@123456> or <@!123456>
mention_pattern = r'<@!?(\d+)>'
match = re.search(mention_pattern, sponsor)
if match:
user_id = int(match.group(1))
try:
# Try to get user from guild first (includes display name)
user = ctx.guild.get_member(user_id)
if user:
return user.display_name
# Fallback to fetching user globally
user = await client.fetch_user(user_id)
if user:
return user.name
# If all fails, return formatted user ID
return f"User#{user_id}"
except Exception as e:
logger.warning(f"Could not fetch user {user_id}: {e}")
return f"User#{user_id}"
else:
# Not a mention, return the text as-is
return sponsor
except Exception as e:
logger.error(f"Error processing sponsor mention: {e}")
return sponsor
async def extract_game_info(game_url: str): async def extract_game_info(game_url: str):
"""Extract game information from various game store URLs""" """Extract game information from various game store URLs"""
try: try:
@@ -1469,7 +1500,7 @@ def update_winner_in_db(guild_id, prize_uuid, winner_dc_id):
close_database_connection(connection) close_database_connection(connection)
class Giveaway: class Giveaway:
def __init__(self, ctx, platform, prize, num_winners, title, subtitle, duration, end_time, sponsor=None, game_url=None, game_info=None): def __init__(self, ctx, platform, prize, num_winners, title, subtitle, duration, end_time, sponsor=None, game_url=None, game_info=None, sponsor_display=None):
self.ctx = ctx self.ctx = ctx
self.guild_id = ctx.guild.id # Speichern der guild_id self.guild_id = ctx.guild.id # Speichern der guild_id
self.platform = platform self.platform = platform
@@ -1479,7 +1510,8 @@ class Giveaway:
self.subtitle = subtitle self.subtitle = subtitle
self.duration = duration self.duration = duration
self.end_time = end_time self.end_time = end_time
self.sponsor = sponsor # New sponsor field self.sponsor = sponsor # Original sponsor field (for embed fields)
self.sponsor_display = sponsor_display # Display name for footer
self.game_url = game_url # Game store URL self.game_url = game_url # Game store URL
self.game_info = game_info # Extracted game information self.game_info = game_info # Extracted game information
self.participants = [] self.participants = []
@@ -1506,6 +1538,7 @@ class Giveaway:
giveaway.title = data.get("title", "Unknown Giveaway") giveaway.title = data.get("title", "Unknown Giveaway")
giveaway.subtitle = data.get("subtitle", "") giveaway.subtitle = data.get("subtitle", "")
giveaway.sponsor = data.get("sponsor", None) # Restore sponsor giveaway.sponsor = data.get("sponsor", None) # Restore sponsor
giveaway.sponsor_display = data.get("sponsor_display", None) # Restore sponsor display
giveaway.game_url = data.get("game_url", None) # Restore game URL giveaway.game_url = data.get("game_url", None) # Restore game URL
giveaway.game_info = data.get("game_info", None) # Restore game info giveaway.game_info = data.get("game_info", None) # Restore game info
giveaway.duration = "restored" giveaway.duration = "restored"
@@ -1521,7 +1554,6 @@ class Giveaway:
"""Creates an entry in the active_processes table for this giveaway""" """Creates an entry in the active_processes table for this giveaway"""
try: try:
giveaway_data = { giveaway_data = {
"giveaway_id": len(giveaways) + 1, # Will be set properly when added to giveaways dict
"guild_id": self.guild_id, "guild_id": self.guild_id,
"channel_id": self.ctx.channel.id, "channel_id": self.ctx.channel.id,
"platform": self.platform, "platform": self.platform,
@@ -1530,6 +1562,7 @@ class Giveaway:
"title": self.title, "title": self.title,
"subtitle": self.subtitle, "subtitle": self.subtitle,
"sponsor": self.sponsor, # Include sponsor in data "sponsor": self.sponsor, # Include sponsor in data
"sponsor_display": self.sponsor_display, # Include sponsor display name
"game_url": self.game_url, # Include game URL "game_url": self.game_url, # Include game URL
"game_info": self.game_info, # Include game info "game_info": self.game_info, # Include game info
"winner_uuids": [str(uuid) for uuid in self.winner_uuids], "winner_uuids": [str(uuid) for uuid in self.winner_uuids],
@@ -1561,17 +1594,12 @@ class Giveaway:
logger.error(f"Error creating process entry for giveaway: {e}") logger.error(f"Error creating process entry for giveaway: {e}")
def update_process_data(self, giveaway_id): def update_process_data(self, giveaway_id):
"""Updates the process data with the actual giveaway ID""" """Updates the process data with the giveaway ID (now using process UUID)"""
try: try:
if self.process_uuid: # Since giveaway_id is now the process UUID, we don't need to update it
current_processes = get_active_processes() # This method is kept for compatibility but doesn't need to do anything
for process in current_processes: logger.info(f"Giveaway using process UUID as ID: {giveaway_id}")
if process["uuid"] == str(self.process_uuid): pass
data = process["data"] or {}
data["giveaway_id"] = giveaway_id
update_process_status(self.process_uuid, "active", data=data)
logger.info(f"Updated process data for giveaway {giveaway_id}")
break
except Exception as e: except Exception as e:
logger.error(f"Error updating process data: {e}") logger.error(f"Error updating process data: {e}")
@@ -1659,9 +1687,16 @@ async def startgiveaway(ctx, platform: str, prize: str, num_winners: int, title:
if game_url: if game_url:
game_info = await extract_game_info(game_url) game_info = await extract_game_info(game_url)
# Process sponsor mention to get display name
sponsor_display = None
if sponsor:
sponsor_display = await process_sponsor_mention(ctx, sponsor)
# Create new giveaway # Create new giveaway
giveaway = Giveaway(ctx, platform, prize, num_winners, title, subtitle, duration, end_time, sponsor, game_url, game_info) giveaway = Giveaway(ctx, platform, prize, num_winners, title, subtitle, duration, end_time, sponsor, game_url, game_info, sponsor_display)
giveaway_id = len(giveaways) + 1
# Use the process UUID as the giveaway ID (more reliable and unique)
giveaway_id = str(giveaway.process_uuid)
giveaways[giveaway_id] = giveaway giveaways[giveaway_id] = giveaway
# Update the process data with the actual giveaway ID # Update the process data with the actual giveaway ID
@@ -1709,9 +1744,11 @@ async def startgiveaway(ctx, platform: str, prize: str, num_winners: int, title:
# Sponsor information if provided # Sponsor information if provided
if sponsor: if sponsor:
embed.add_field(name="💝 Sponsored by", value=f"**{sponsor}**", inline=False) embed.add_field(name="💝 Sponsored by", value=f"**{sponsor}**", inline=False)
embed.set_footer(text=f"Giveaway ID: {giveaway_id} • Sponsored by {sponsor}") # Use sponsor_display for footer if available, otherwise fallback to sponsor
footer_sponsor = sponsor_display if sponsor_display else sponsor
embed.set_footer(text=f"Giveaway ID: {giveaway_id[:8]}... • Sponsored by {footer_sponsor}")
else: else:
embed.set_footer(text=f"Giveaway ID: {giveaway_id} • Good luck!") embed.set_footer(text=f"Giveaway ID: {giveaway_id[:8]}... • Good luck!")
# Set game image if available, otherwise use default # Set game image if available, otherwise use default
if game_info and game_info.get('image_url'): if game_info and game_info.get('image_url'):
@@ -1870,7 +1907,7 @@ async def on_interaction(interaction):
# Nur Button-Interaktionen für Giveaways verarbeiten # Nur Button-Interaktionen für Giveaways verarbeiten
if interaction.type == discord.InteractionType.component and "custom_id" in interaction.data: if interaction.type == discord.InteractionType.component and "custom_id" in interaction.data:
if interaction.data["custom_id"].startswith("giveaway_"): if interaction.data["custom_id"].startswith("giveaway_"):
giveaway_id = int(interaction.data["custom_id"].split("_")[1]) giveaway_id = interaction.data["custom_id"].split("_", 1)[1] # Get everything after "giveaway_"
giveaway = giveaways.get(giveaway_id) giveaway = giveaways.get(giveaway_id)
if giveaway: if giveaway: