feat: Add autocomplete for role category names and enhance role category addition logic

This commit is contained in:
Slipstream 2025-05-30 18:33:11 -06:00
parent 05433c0bb8
commit d73efb9735
Signed by: slipstream
GPG Key ID: 13E498CE010AC6FD

View File

@ -372,6 +372,33 @@ class RoleSelectorCog(commands.Cog):
max_selectable="Maximum number of roles a user can select from this category (default: 1).",
preset_id="Optional ID of a global preset to base this category on."
)
async def autocomplete_category_name(self, interaction: discord.Interaction, current: str) -> List[app_commands.Choice[str]]:
choices = []
# Add existing guild category names
if interaction.guild:
guild_configs = db.get_guild_role_category_configs(str(interaction.guild_id))
for config in guild_configs:
if config.name.lower().startswith(current.lower()):
choices.append(app_commands.Choice(name=config.name, value=config.name))
# Add global preset names
presets = db.get_all_role_category_presets()
for preset in presets:
if preset.name.lower().startswith(current.lower()):
choices.append(app_commands.Choice(name=f"Preset: {preset.name}", value=preset.name)) # Use preset name as value
# Limit to 25 choices as per Discord API limits
return choices[:25]
@roleselect_group.command(name="addcategory", description="Adds a new role category for selection.")
@app_commands.checks.has_permissions(manage_guild=True)
@app_commands.describe(
name="The name for the new category.",
description="A description for this role category.",
max_selectable="Maximum number of roles a user can select from this category (default: 1).",
preset_id="Optional ID of a global preset to base this category on."
)
@app_commands.autocomplete(name=autocomplete_category_name)
async def roleselect_addcategory(self, interaction: discord.Interaction, name: str, description: str, max_selectable: Optional[int] = 1, preset_id: Optional[str] = None):
if not interaction.guild:
await interaction.response.send_message("This command can only be used in a server.", ephemeral=True)
@ -380,9 +407,19 @@ class RoleSelectorCog(commands.Cog):
# Ensure max_selectable has a valid default if None is passed by Discord for optional int
current_max_selectable = max_selectable if max_selectable is not None else 1
# Check if the provided 'name' is actually a preset name from autocomplete
# If it starts with "Preset: ", extract the actual preset name
actual_name = name
if name.startswith("Preset: "):
actual_name = name[len("Preset: "):]
# If preset_id was not explicitly provided, set it to the ID of the selected preset
if not preset_id:
selected_preset = discord.utils.find(lambda p: p.name == actual_name, db.get_all_role_category_presets())
if selected_preset:
preset_id = selected_preset.id
if self._get_guild_category_config(interaction.guild_id, name) and not preset_id: # Allow adding preset even if name conflicts, preset name will be used
await interaction.response.send_message(f"A custom role category named '{name}' already exists.", ephemeral=True)
if self._get_guild_category_config(interaction.guild_id, actual_name) and not preset_id: # Allow adding preset even if name conflicts, preset name will be used
await interaction.response.send_message(f"A custom role category named '{actual_name}' already exists.", ephemeral=True)
return
roles_to_add: List[GuildRole] = []
@ -524,7 +561,6 @@ class RoleSelectorCog(commands.Cog):
await interaction.response.send_message(embed=embed, ephemeral=False) # Make it visible
@roleselect_group.command(name="listpresets", description="Lists all available global role category presets.")
@app_commands.check(is_owner_check) # Presets are global, so owner only
async def roleselect_listpresets(self, interaction: discord.Interaction):
presets = db.get_all_role_category_presets()
if not presets: