Enhance error handling and improve message ID storage in ControlPanel and WaitingRoom
Some checks failed
Run pre-commit / Run pre-commit (push) Has been cancelled

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.
This commit is contained in:
Valerie 2025-06-13 20:02:18 -04:00
parent 628f2dae90
commit 60a2cb1f4f
3 changed files with 99 additions and 60 deletions

View file

@ -68,8 +68,9 @@ class AutoRoom(
"owner": None, "owner": None,
"associated_text_channel": None, "associated_text_channel": None,
"denied": [], "denied": [],
"message_id": None, "message_id": None, # For control panel message ID
"waiting_room_message_id": None, "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 extra_channel_name_change_delay = 4

View file

@ -197,42 +197,62 @@ class ControlPanel:
async def create_control_panel(self, autoroom: discord.VoiceChannel) -> None: async def create_control_panel(self, autoroom: discord.VoiceChannel) -> None:
"""Create the control panel embed in the voice channel's text chat.""" """Create the control panel embed in the voice channel's text chat."""
autoroom_info = await self.cog.get_autoroom_info(autoroom) try:
if not autoroom_info: 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 return
except discord.Forbidden:
owner = autoroom.guild.get_member(autoroom_info["owner"]) # We don't have permission to send messages
if not owner:
return return
except Exception as e:
# Get the associated text channel using the voice channel's ID # Log any other errors
text_channel = None print(f"Error creating control panel: {e}")
for channel in autoroom.guild.text_channels:
if channel.id == autoroom.id:
text_channel = channel
break
if not text_channel:
return 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)

View file

@ -137,29 +137,47 @@ class WaitingRoom:
async def handle_waiting_user(self, member: discord.Member, autoroom: discord.VoiceChannel) -> None: async def handle_waiting_user(self, member: discord.Member, autoroom: discord.VoiceChannel) -> None:
"""Handle a user joining the waiting room.""" """Handle a user joining the waiting room."""
autoroom_info = await self.cog.get_autoroom_info(autoroom) try:
if not autoroom_info: 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 return
except discord.Forbidden:
# Get the voice channel's text chat using the voice channel's ID # We don't have permission to send messages
text_channel = None return
for channel in autoroom.guild.text_channels: except Exception as e:
if channel.id == autoroom.id: # Log any other errors
text_channel = channel print(f"Error handling waiting user: {e}")
break
if not text_channel:
return 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: async def cleanup_waiting_room(self, autoroom_source: discord.VoiceChannel) -> None:
"""Clean up the waiting room when disabling it.""" """Clean up the waiting room when disabling it."""