From 60a2cb1f4f0ebaf134040622288eb33aa3a3e681 Mon Sep 17 00:00:00 2001 From: Valerie Date: Fri, 13 Jun 2025 20:02:18 -0400 Subject: [PATCH] Enhance error handling and improve message ID storage in ControlPanel and WaitingRoom This update refines the error handling in the ControlPanel and WaitingRoom classes, ensuring that exceptions are logged and handled gracefully. Additionally, it improves the storage of message IDs for both control panel and waiting room messages, enhancing the overall reliability and functionality of the AutoRoom feature. --- autoroom/autoroom.py | 5 ++- autoroom/control_panel.py | 94 ++++++++++++++++++++++++--------------- autoroom/waiting_room.py | 60 ++++++++++++++++--------- 3 files changed, 99 insertions(+), 60 deletions(-) diff --git a/autoroom/autoroom.py b/autoroom/autoroom.py index 98cb49f..3e3eadd 100644 --- a/autoroom/autoroom.py +++ b/autoroom/autoroom.py @@ -68,8 +68,9 @@ class AutoRoom( "owner": None, "associated_text_channel": None, "denied": [], - "message_id": None, - "waiting_room_message_id": None, + "message_id": None, # For control panel message ID + "control_panel_message_id": None, # Alternative name for control panel message ID + "waiting_room_message_id": None, # For waiting room message ID } extra_channel_name_change_delay = 4 diff --git a/autoroom/control_panel.py b/autoroom/control_panel.py index 57bbbd1..491d574 100644 --- a/autoroom/control_panel.py +++ b/autoroom/control_panel.py @@ -197,42 +197,62 @@ class ControlPanel: async def create_control_panel(self, autoroom: discord.VoiceChannel) -> None: """Create the control panel embed in the voice channel's text chat.""" - autoroom_info = await self.cog.get_autoroom_info(autoroom) - if not autoroom_info: + try: + autoroom_info = await self.cog.get_autoroom_info(autoroom) + if not autoroom_info: + return + + owner = autoroom.guild.get_member(autoroom_info["owner"]) + if not owner: + return + + # Get the associated text channel using the voice channel's ID + text_channel = None + for channel in autoroom.guild.text_channels: + if channel.id == autoroom.id: + text_channel = channel + break + + if not text_channel: + # If no matching text channel found, try to get the default text channel + text_channel = autoroom.guild.system_channel + if not text_channel: + return + + # Check if we have permission to send messages + if not text_channel.permissions_for(autoroom.guild.me).send_messages: + return + + # Create the embed + embed = discord.Embed( + title="AutoRoom Control Panel", + description=f"Control panel for {autoroom.mention}\nOwner: {owner.mention}", + color=discord.Color.blue() + ) + + # Add current status + status = "Public" if autoroom.permissions_for(autoroom.guild.default_role).connect else "Private" + embed.add_field(name="Status", value=status, inline=True) + + # Add member count + embed.add_field(name="Members", value=str(len(autoroom.members)), inline=True) + + # Create view with buttons + view = ControlPanelView(self.cog, autoroom.id) + + # Send the embed to the text channel + message = await text_channel.send(embed=embed, view=view) + + # Store the message ID for reference (using both field names for compatibility) + await self.config.channel(autoroom).message_id.set(message.id) + await self.config.channel(autoroom).control_panel_message_id.set(message.id) + except discord.NotFound: + # Channel was deleted or we lost access return - - owner = autoroom.guild.get_member(autoroom_info["owner"]) - if not owner: + except discord.Forbidden: + # We don't have permission to send messages return - - # Get the associated text channel using the voice channel's ID - text_channel = None - for channel in autoroom.guild.text_channels: - if channel.id == autoroom.id: - text_channel = channel - break - - if not text_channel: - return - - # Create the embed - embed = discord.Embed( - title="AutoRoom Control Panel", - description=f"Control panel for {autoroom.mention}\nOwner: {owner.mention}", - color=discord.Color.blue() - ) - - # Add current status - status = "Public" if autoroom.permissions_for(autoroom.guild.default_role).connect else "Private" - embed.add_field(name="Status", value=status, inline=True) - - # Add member count - embed.add_field(name="Members", value=str(len(autoroom.members)), inline=True) - - # Create view with buttons - view = ControlPanelView(self.cog, autoroom.id) - - # Send the embed to the text channel - message = await text_channel.send(embed=embed, view=view) - # Store the message ID for reference - await self.config.channel(autoroom).message_id.set(message.id) \ No newline at end of file + except Exception as e: + # Log any other errors + print(f"Error creating control panel: {e}") + return \ No newline at end of file diff --git a/autoroom/waiting_room.py b/autoroom/waiting_room.py index 2c1128f..430cbe8 100644 --- a/autoroom/waiting_room.py +++ b/autoroom/waiting_room.py @@ -137,29 +137,47 @@ class WaitingRoom: async def handle_waiting_user(self, member: discord.Member, autoroom: discord.VoiceChannel) -> None: """Handle a user joining the waiting room.""" - autoroom_info = await self.cog.get_autoroom_info(autoroom) - if not autoroom_info: + try: + autoroom_info = await self.cog.get_autoroom_info(autoroom) + if not autoroom_info: + return + + # Get the voice channel's text chat using the voice channel's ID + text_channel = None + for channel in autoroom.guild.text_channels: + if channel.id == autoroom.id: + text_channel = channel + break + + if not text_channel: + # If no matching text channel found, try to get the default text channel + text_channel = autoroom.guild.system_channel + if not text_channel: + return + + # Check if we have permission to send messages + if not text_channel.permissions_for(autoroom.guild.me).send_messages: + return + + # Create waiting message with buttons + view = WaitingRoomView(self.cog) + message = await text_channel.send( + f"User: {member.mention} is waiting to join the AutoRoom.", + view=view + ) + + # Save message ID + await self.config.channel(autoroom).waiting_room_message_id.set(message.id) + except discord.NotFound: + # Channel was deleted or we lost access return - - # Get the voice channel's text chat using the voice channel's ID - text_channel = None - for channel in autoroom.guild.text_channels: - if channel.id == autoroom.id: - text_channel = channel - break - - if not text_channel: + except discord.Forbidden: + # We don't have permission to send messages + return + except Exception as e: + # Log any other errors + print(f"Error handling waiting user: {e}") return - - # Create waiting message with buttons - view = WaitingRoomView(self.cog) - message = await text_channel.send( - f"User: {member.mention} is waiting to join the AutoRoom.", - view=view - ) - - # Save message ID - await self.config.channel(autoroom).waiting_room_message_id.set(message.id) async def cleanup_waiting_room(self, autoroom_source: discord.VoiceChannel) -> None: """Clean up the waiting room when disabling it."""