Refactor ControlPanelView to use voice channel ID directly
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run

This update modifies the ControlPanelView class to accept a voice channel ID during initialization, streamlining the retrieval of voice channel information. The changes enhance the button interactions by directly referencing the voice channel ID, improving the overall functionality and reliability of the AutoRoom feature.
This commit is contained in:
Valerie 2025-06-13 19:38:04 -04:00
parent 84410f9b20
commit 05db4e0d39

View file

@ -10,18 +10,16 @@ from redbot.core.bot import Red
class ControlPanelView(ui.View):
"""View for AutoRoom control panel buttons."""
def __init__(self, cog: Any):
def __init__(self, cog: Any, voice_channel_id: int):
super().__init__(timeout=None)
self.cog = cog
self.voice_channel_id = voice_channel_id
@discord.ui.button(label="🔓 Public", style=discord.ButtonStyle.green, custom_id="autoroom_public")
async def public_button(self, interaction: discord.Interaction, button: discord.ui.Button):
"""Make the AutoRoom public."""
if not interaction.message:
return
# Get the voice channel from the message
voice_channel = interaction.guild.get_channel(interaction.message.reference.channel_id)
# Get the voice channel
voice_channel = interaction.guild.get_channel(self.voice_channel_id)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
@ -46,11 +44,8 @@ class ControlPanelView(ui.View):
@discord.ui.button(label="🔒 Locked", style=discord.ButtonStyle.grey, custom_id="autoroom_locked")
async def locked_button(self, interaction: discord.Interaction, button: discord.ui.Button):
"""Make the AutoRoom locked."""
if not interaction.message:
return
# Get the voice channel from the message
voice_channel = interaction.guild.get_channel(interaction.message.reference.channel_id)
# Get the voice channel
voice_channel = interaction.guild.get_channel(self.voice_channel_id)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
@ -75,11 +70,8 @@ class ControlPanelView(ui.View):
@discord.ui.button(label="🔐 Private", style=discord.ButtonStyle.red, custom_id="autoroom_private")
async def private_button(self, interaction: discord.Interaction, button: discord.ui.Button):
"""Make the AutoRoom private."""
if not interaction.message:
return
# Get the voice channel from the message
voice_channel = interaction.guild.get_channel(interaction.message.reference.channel_id)
# Get the voice channel
voice_channel = interaction.guild.get_channel(self.voice_channel_id)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
@ -104,11 +96,8 @@ class ControlPanelView(ui.View):
@discord.ui.button(label="👥 Add User", style=discord.ButtonStyle.blurple, custom_id="autoroom_add_user")
async def add_user_button(self, interaction: discord.Interaction, button: discord.ui.Button):
"""Add a user to the AutoRoom."""
if not interaction.message:
return
# Get the voice channel from the message
voice_channel = interaction.guild.get_channel(interaction.message.reference.channel_id)
# Get the voice channel
voice_channel = interaction.guild.get_channel(self.voice_channel_id)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
@ -125,17 +114,14 @@ class ControlPanelView(ui.View):
return
# Create a modal for user selection
modal = UserSelectModal(self.cog, "allow")
modal = UserSelectModal(self.cog, "allow", self.voice_channel_id)
await interaction.response.send_modal(modal)
@discord.ui.button(label="🚫 Remove User", style=discord.ButtonStyle.danger, custom_id="autoroom_remove_user")
async def remove_user_button(self, interaction: discord.Interaction, button: discord.ui.Button):
"""Remove a user from the AutoRoom."""
if not interaction.message:
return
# Get the voice channel from the message
voice_channel = interaction.guild.get_channel(interaction.message.reference.channel_id)
# Get the voice channel
voice_channel = interaction.guild.get_channel(self.voice_channel_id)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
@ -152,16 +138,17 @@ class ControlPanelView(ui.View):
return
# Create a modal for user selection
modal = UserSelectModal(self.cog, "deny")
modal = UserSelectModal(self.cog, "deny", self.voice_channel_id)
await interaction.response.send_modal(modal)
class UserSelectModal(ui.Modal, title="Select User"):
"""Modal for selecting a user to add/remove."""
def __init__(self, cog: Any, action: str):
def __init__(self, cog: Any, action: str, voice_channel_id: int):
super().__init__()
self.cog = cog
self.action = action
self.voice_channel_id = voice_channel_id
self.user_id = ui.TextInput(
label="User ID or @mention",
placeholder="Enter user ID or @mention",
@ -188,8 +175,8 @@ class UserSelectModal(ui.Modal, title="Select User"):
await interaction.response.send_message("User not found in this server.", ephemeral=True)
return
# Get the voice channel from the message
voice_channel = interaction.guild.get_channel(interaction.message.reference.channel_id)
# Get the voice channel
voice_channel = interaction.guild.get_channel(self.voice_channel_id)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
@ -233,7 +220,7 @@ class ControlPanel:
embed.add_field(name="Members", value=str(len(autoroom.members)), inline=True)
# Create view with buttons
view = ControlPanelView(self.cog)
view = ControlPanelView(self.cog, autoroom.id)
# Send the embed
message = await autoroom.send(embed=embed, view=view)