Skip to content

Commit

Permalink
fix: giveaways
Browse files Browse the repository at this point in the history
  • Loading branch information
Destinea committed Dec 21, 2024
1 parent a2349ac commit a7ec768
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
28 changes: 17 additions & 11 deletions background/logs/giveaway.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pendulum


class GiveawayEvents(commands.Cog):
class GiveawayEvents(commands.Cog, name="Giveaway Events"):
def __init__(self, bot):
self.bot = bot
giveaway_ee.on('giveaway_start', self.on_giveaway_start)
Expand All @@ -21,12 +21,12 @@ async def get_user_roles(self, guild_id, user_id: str) -> list:
Get the roles of a user in a guild.
Args:
user_id (str): L'ID de l'utilisateur.
user_id (str): The user ID to fetch roles for.
Returns:
list: List of role IDs the user has.
"""
guild = self.bot.get_guild(guild_id)
guild = await self.bot.getch_guild(guild_id)
if guild is None:
return []

Expand All @@ -46,9 +46,10 @@ async def on_giveaway_start(self, data):
Handle the 'giveaway_start' event and add a "Participate" button with an image.
"""
# Extract giveaway data
print(data)
giveaway = data['giveaway']
print(giveaway)
server_id = giveaway.get('server_id', 'unknown')
if server_id not in self.bot.OUR_GUILDS:
return
channel_id = giveaway['channel_id']
prize = giveaway['prize']
mentions = giveaway.get('mentions', [])
Expand Down Expand Up @@ -87,7 +88,8 @@ async def on_giveaway_start(self, data):
mention_text = " ".join([f"<@&{mention}>" for mention in mentions]) if mentions else ""

# Send the embed to the channel
channel = self.bot.get_channel(int(channel_id))
channel = await self.bot.getch_channel(int(channel_id))

if channel:
# Combine mention_text and text_above_embed, ensuring proper spacing
content = f"{mention_text}\n{text_above_embed}".strip() if mention_text or text_above_embed else None
Expand All @@ -109,6 +111,9 @@ async def on_giveaway_update(self, data):
"""
# Extract giveaway data
giveaway = data['giveaway']
server_id = giveaway.get('server_id', 'unknown')
if server_id not in self.bot.OUR_GUILDS:
return
channel_id = giveaway['channel_id']
giveaway_id = giveaway.get('_id', 'unknown')
prize = giveaway['prize']
Expand Down Expand Up @@ -143,7 +148,7 @@ async def on_giveaway_update(self, data):
content = f"{mention_text}\n{text_above_embed}".strip() if mention_text or text_above_embed else None

# Fetch the target channel and message
channel = self.bot.get_channel(int(channel_id))
channel = await self.bot.getch_channel(int(channel_id))
if not channel:
print(f"Channel with ID {channel_id} not found.")
return
Expand All @@ -164,6 +169,9 @@ async def on_giveaway_end(self, data):
"""
# Extract giveaway data
giveaway = data['giveaway']
server_id = giveaway.get('server_id', 'unknown')
if server_id not in self.bot.OUR_GUILDS:
return
participants = giveaway.get('entries', [])
winner_count = giveaway.get('winners', 0)
channel_id = giveaway['channel_id']
Expand All @@ -186,8 +194,6 @@ async def on_giveaway_end(self, data):
weight = max(applicable_boosts) if applicable_boosts else 1
weights.append(weight)

print(weights)

# Determine winners
winners = []
if participants and winner_count > 0:
Expand All @@ -209,7 +215,8 @@ async def on_giveaway_end(self, data):
embed.set_image(url=image_url)

# Get the target channel
channel = self.bot.get_channel(int(channel_id))
channel = await self.bot.getch_channel(int(channel_id))

if channel:
# Combine mention_text and text_on_end
content = f"{mention_text}\n{text_on_end}".strip() if mention_text or text_on_end else None
Expand All @@ -223,7 +230,6 @@ async def on_button_click(self, interaction: disnake.MessageInteraction):
Listener for giveaway participation buttons.
"""
if interaction.component.custom_id.startswith("giveaway_"):
await interaction.response.defer(ephemeral=True)
giveaway_id = interaction.component.custom_id.split("_")[1]

# Fetch giveaway from the database
Expand Down
2 changes: 1 addition & 1 deletion commands/giveaway/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ async def giveaway_reroll(

# Send the announcement in the giveaway's channel
channel_id = giveaway["channel_id"]
channel = self.bot.get_channel(channel_id)
channel = await self.bot.getch_channel(int(channel_id))

if not channel:
await ctx.send("❌ Could not find the channel associated with the giveaway.", ephemeral=True)
Expand Down

0 comments on commit a7ec768

Please sign in to comment.