Ruby-Cogs/autoroom/control_panel.py
Valerie fa6cd01476
Some checks are pending
Run pre-commit / Run pre-commit (push) Waiting to run
Add ControlPanel integration and refactor autoroom creation logic
This update introduces the ControlPanel class to the AutoRoom cog, enhancing the autoroom creation process. The autoroom source configuration has been streamlined by renaming variables for clarity. Additionally, the logic for creating associated text channels has been improved, ensuring better handling of permissions and channel creation. The waiting room functionality has also been adjusted to correctly reference the voice channel's text chat.
2025-06-13 19:29:45 -04:00

199 lines
No EOL
8 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 AutoRoom info
autoroom_info = await self.cog.get_autoroom_info(interaction.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
await self.cog._process_allow_deny(interaction, "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 AutoRoom info
autoroom_info = await self.cog.get_autoroom_info(interaction.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
await self.cog._process_allow_deny(interaction, "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 AutoRoom info
autoroom_info = await self.cog.get_autoroom_info(interaction.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
await self.cog._process_allow_deny(interaction, "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 AutoRoom info
autoroom_info = await self.cog.get_autoroom_info(interaction.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 AutoRoom info
autoroom_info = await self.cog.get_autoroom_info(interaction.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
# Process the allow/deny action
await self.cog._process_allow_deny(interaction, 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
# Get the voice channel's text chat
text_channel = autoroom.guild.get_channel(autoroom.id)
if not text_channel:
return
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
await text_channel.send(embed=embed, view=view)