Ruby-Cogs/autoroom/control_panel.py
Valerie 84410f9b20
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run
Refactor ControlPanelView to utilize voice channel references
This update modifies the ControlPanelView class to retrieve voice channel information from interaction messages, ensuring accurate AutoRoom management. It also updates the context handling for allow/deny actions, improving the overall functionality and user experience of the AutoRoom feature.
2025-06-13 19:35:00 -04:00

241 lines
No EOL
10 KiB
Python

"""Control panel functionality for AutoRoom cog."""
from typing import Any, Optional
import discord
from discord import ui
from redbot.core import Config
from redbot.core.bot import Red
class ControlPanelView(ui.View):
"""View for AutoRoom control panel buttons."""
def __init__(self, cog: Any):
super().__init__(timeout=None)
self.cog = cog
@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)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
# Get the AutoRoom info
autoroom_info = await self.cog.get_autoroom_info(voice_channel)
if not autoroom_info:
await interaction.response.send_message("This is not an AutoRoom.", ephemeral=True)
return
# Check if the interaction user is the owner
if interaction.user.id != autoroom_info["owner"]:
await interaction.response.send_message("Only the AutoRoom owner can use these buttons.", ephemeral=True)
return
# Make the AutoRoom public
ctx = await self.cog.bot.get_context(interaction.message)
ctx.author = interaction.user
await self.cog._process_allow_deny(ctx, "allow")
await interaction.response.send_message("AutoRoom is now public.", ephemeral=True)
@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)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
# Get the AutoRoom info
autoroom_info = await self.cog.get_autoroom_info(voice_channel)
if not autoroom_info:
await interaction.response.send_message("This is not an AutoRoom.", ephemeral=True)
return
# Check if the interaction user is the owner
if interaction.user.id != autoroom_info["owner"]:
await interaction.response.send_message("Only the AutoRoom owner can use these buttons.", ephemeral=True)
return
# Make the AutoRoom locked
ctx = await self.cog.bot.get_context(interaction.message)
ctx.author = interaction.user
await self.cog._process_allow_deny(ctx, "lock")
await interaction.response.send_message("AutoRoom is now locked.", ephemeral=True)
@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)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
# Get the AutoRoom info
autoroom_info = await self.cog.get_autoroom_info(voice_channel)
if not autoroom_info:
await interaction.response.send_message("This is not an AutoRoom.", ephemeral=True)
return
# Check if the interaction user is the owner
if interaction.user.id != autoroom_info["owner"]:
await interaction.response.send_message("Only the AutoRoom owner can use these buttons.", ephemeral=True)
return
# Make the AutoRoom private
ctx = await self.cog.bot.get_context(interaction.message)
ctx.author = interaction.user
await self.cog._process_allow_deny(ctx, "deny")
await interaction.response.send_message("AutoRoom is now private.", ephemeral=True)
@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)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
# Get the AutoRoom info
autoroom_info = await self.cog.get_autoroom_info(voice_channel)
if not autoroom_info:
await interaction.response.send_message("This is not an AutoRoom.", ephemeral=True)
return
# Check if the interaction user is the owner
if interaction.user.id != autoroom_info["owner"]:
await interaction.response.send_message("Only the AutoRoom owner can use these buttons.", ephemeral=True)
return
# Create a modal for user selection
modal = UserSelectModal(self.cog, "allow")
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)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
# Get the AutoRoom info
autoroom_info = await self.cog.get_autoroom_info(voice_channel)
if not autoroom_info:
await interaction.response.send_message("This is not an AutoRoom.", ephemeral=True)
return
# Check if the interaction user is the owner
if interaction.user.id != autoroom_info["owner"]:
await interaction.response.send_message("Only the AutoRoom owner can use these buttons.", ephemeral=True)
return
# Create a modal for user selection
modal = UserSelectModal(self.cog, "deny")
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):
super().__init__()
self.cog = cog
self.action = action
self.user_id = ui.TextInput(
label="User ID or @mention",
placeholder="Enter user ID or @mention",
required=True
)
self.add_item(self.user_id)
async def on_submit(self, interaction: discord.Interaction):
"""Handle the modal submission."""
user_input = self.user_id.value.strip()
# Try to get user from mention
if user_input.startswith("<@") and user_input.endswith(">"):
user_id = int(user_input[2:-1])
else:
try:
user_id = int(user_input)
except ValueError:
await interaction.response.send_message("Invalid user ID or mention.", ephemeral=True)
return
user = interaction.guild.get_member(user_id)
if not 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)
if not isinstance(voice_channel, discord.VoiceChannel):
await interaction.response.send_message("Could not find the voice channel.", ephemeral=True)
return
# Process the allow/deny action
ctx = await self.cog.bot.get_context(interaction.message)
ctx.author = interaction.user
await self.cog._process_allow_deny(ctx, self.action, member_or_role=user)
await interaction.response.send_message(f"User {user.mention} has been {'allowed' if self.action == 'allow' else 'denied'} access.", ephemeral=True)
class ControlPanel:
"""Handles control panel functionality."""
def __init__(self, cog: Any):
self.cog = cog
self.bot: Red = cog.bot
self.config: Config = cog.config
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:
return
owner = autoroom.guild.get_member(autoroom_info["owner"])
if not owner:
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)
# Send the embed
message = await autoroom.send(embed=embed, view=view)
# Store the message ID for reference
await self.config.channel(autoroom).control_panel_message_id.set(message.id)