diff --git a/birthday/birthday.py b/birthday/birthday.py index 650f4ee..f531e9f 100644 --- a/birthday/birthday.py +++ b/birthday/birthday.py @@ -9,61 +9,70 @@ import random # Define context menu command outside the class @app_commands.context_menu(name="Give Birthday Role") async def birthday_context_menu(interaction: discord.Interaction, member: discord.Member): - cog = interaction.client.get_cog("Birthday") - if not cog: - await interaction.response.send_message("Birthday cog is not loaded.", ephemeral=True) - return - - # Check if the user has permission to use this command - allowed_roles = await cog.config.guild(interaction.guild).allowed_roles() - if not any(role.id in allowed_roles for role in interaction.user.roles): - return await interaction.response.send_message("You don't have permission to use this command.", ephemeral=True) - - birthday_role_id = await cog.config.guild(interaction.guild).birthday_role() - if not birthday_role_id: - return await interaction.response.send_message("The birthday role hasn't been set. An admin needs to set it using `/setrole`.", ephemeral=True) - - birthday_role = interaction.guild.get_role(birthday_role_id) - if not birthday_role: - return await interaction.response.send_message("The birthday role doesn't exist anymore. Please ask an admin to set it again.", ephemeral=True) - - # Assign the role, ignoring hierarchy try: - await member.add_roles(birthday_role, reason="Birthday role") - except discord.Forbidden: - return await interaction.response.send_message("I don't have permission to assign that role.", ephemeral=True) + # Defer the response immediately to prevent timeout + await interaction.response.defer(ephemeral=True) + + cog = interaction.client.get_cog("Birthday") + if not cog: + await interaction.followup.send("Birthday cog is not loaded.", ephemeral=True) + return - # Generate birthday message with random cakes (or pie) - cakes = random.randint(0, 5) - if cakes == 0: - message = f"🎉 Happy Birthday, {member.mention}! Sorry, out of cake today! Here's pie instead: 🥧" - else: - message = f"🎉 Happy Birthday, {member.mention}! Here's your cake{'s' if cakes > 1 else ''}: " + "🎂" * cakes + # Check if the user has permission to use this command + allowed_roles = await cog.config.guild(interaction.guild).allowed_roles() + if not any(role.id in allowed_roles for role in interaction.user.roles): + return await interaction.followup.send("You don't have permission to use this command.", ephemeral=True) - # Get the birthday announcement channel - birthday_channel_id = await cog.config.guild(interaction.guild).birthday_channel() - if birthday_channel_id: - channel = interaction.client.get_channel(birthday_channel_id) - if not channel: # If the set channel doesn't exist anymore + birthday_role_id = await cog.config.guild(interaction.guild).birthday_role() + if not birthday_role_id: + return await interaction.followup.send("The birthday role hasn't been set. An admin needs to set it using `/setrole`.", ephemeral=True) + + birthday_role = interaction.guild.get_role(birthday_role_id) + if not birthday_role: + return await interaction.followup.send("The birthday role doesn't exist anymore. Please ask an admin to set it again.", ephemeral=True) + + # Assign the role, ignoring hierarchy + try: + await member.add_roles(birthday_role, reason="Birthday role") + except discord.Forbidden: + return await interaction.followup.send("I don't have permission to assign that role.", ephemeral=True) + + # Generate birthday message with random cakes (or pie) + cakes = random.randint(0, 5) + if cakes == 0: + message = f"🎉 Happy Birthday, {member.mention}! Sorry, out of cake today! Here's pie instead: 🥧" + else: + message = f"🎉 Happy Birthday, {member.mention}! Here's your cake{'s' if cakes > 1 else ''}: " + "🎂" * cakes + + # Get the birthday announcement channel + birthday_channel_id = await cog.config.guild(interaction.guild).birthday_channel() + if birthday_channel_id: + channel = interaction.client.get_channel(birthday_channel_id) + if not channel: # If the set channel doesn't exist anymore + channel = interaction.channel + else: channel = interaction.channel - else: - channel = interaction.channel - await channel.send(message) - await interaction.response.send_message("Birthday role assigned!", ephemeral=True) + await channel.send(message) + await interaction.followup.send("Birthday role assigned!", ephemeral=True) - # Schedule role removal - timezone = await cog.config.guild(interaction.guild).timezone() - try: - tz = ZoneInfo(timezone) - except ZoneInfoNotFoundError: - await interaction.followup.send("Warning: Invalid timezone set. Defaulting to UTC.", ephemeral=True) - tz = ZoneInfo("UTC") + # Schedule role removal + timezone = await cog.config.guild(interaction.guild).timezone() + try: + tz = ZoneInfo(timezone) + except ZoneInfoNotFoundError: + await interaction.followup.send("Warning: Invalid timezone set. Defaulting to UTC.", ephemeral=True) + tz = ZoneInfo("UTC") - now = datetime.now(tz) - midnight = datetime.combine(now.date() + timedelta(days=1), time.min).replace(tzinfo=tz) + now = datetime.now(tz) + midnight = datetime.combine(now.date() + timedelta(days=1), time.min).replace(tzinfo=tz) - await cog.schedule_birthday_role_removal(interaction.guild, member, birthday_role, midnight) + await cog.schedule_birthday_role_removal(interaction.guild, member, birthday_role, midnight) + except Exception as e: + try: + await interaction.followup.send(f"An error occurred: {str(e)}", ephemeral=True) + except: + pass class Birthday(commands.Cog): """A cog to assign a birthday role until midnight in a specified timezone.""" diff --git a/videoarchiver/commands.py b/videoarchiver/commands.py index 0178667..db5bd76 100644 --- a/videoarchiver/commands.py +++ b/videoarchiver/commands.py @@ -197,7 +197,7 @@ class VideoArchiverCommands(commands.Cog): # Verify sites are valid with yt_dlp.YoutubeDL() as ydl: - valid_sites = set(ie.IE_NAME.lower() for ie in ydl._ies) + valid_sites = set(ie.IE_NAME.lower() for ie in ydl._ies if hasattr(ie, 'IE_NAME')) invalid_sites = [s for s in site_list if s not in valid_sites] if invalid_sites: await ctx.send( @@ -217,7 +217,8 @@ class VideoArchiverCommands(commands.Cog): embed = discord.Embed(title="Video Sites Configuration", color=discord.Color.blue()) with yt_dlp.YoutubeDL() as ydl: - all_sites = sorted(ie.IE_NAME for ie in ydl._ies if ie.IE_NAME is not None) + # Filter out any extractors that don't have IE_NAME attribute + all_sites = sorted(ie.IE_NAME for ie in ydl._ies if hasattr(ie, 'IE_NAME') and ie.IE_NAME is not None) # Split sites into chunks for Discord's field value limit chunk_size = 20