Add a command to set the category size.

This commit is contained in:
2025-03-05 11:02:09 -05:00
parent 6e25d4e75a
commit d703c76704
+63 -3
View File
@@ -7,8 +7,9 @@ from sqlalchemy.orm import Session
from sqlmodel import col from sqlmodel import col
from database.db import engine from database.db import engine
from database.models import CategoryGroup, Guild from database.models import Category, CategoryGroup, Guild
from utilities.utils import MAX_LINE_LENGTH, autofill_groups from utilities.logger import agon_logger
from utilities.utils import MAX_LINE_LENGTH, autofill_categories, autofill_groups, send_highscore_message
class SetCog(commands.Cog, name="Set command."): class SetCog(commands.Cog, name="Set command."):
@@ -21,6 +22,50 @@ class SetCog(commands.Cog, name="Set command."):
async def set(self, interaction: nextcord.Interaction) -> None: async def set(self, interaction: nextcord.Interaction) -> None:
"""Parent slash command for setting settings. Never called.""" """Parent slash command for setting settings. Never called."""
@set.subcommand(description="")
async def category_size(
self,
interaction: nextcord.Interaction,
group_id: str = nextcord.SlashOption(
name="group", description="Category Group that Category is in.", required=True
),
category_id: str = nextcord.SlashOption(
name="category", description="Category to update size of.", required=True
),
size: int = nextcord.SlashOption(description="Number of entries to show for the category.", required=True),
) -> None:
"""
Command to update a category's size.
Args:
interaction (nextcord.Interaction): Interaction that invoked the command.
group_id (str): String representation of Group id to register channel to. Is SlashOption.
category_id (str): String representation of Category id to register channel to. Is SlashOption.
size (int): Size to set. Is SlashOption.
"""
stmt = select(Category).where(col(CategoryGroup.id == group_id)).where(col(Category.id) == int(category_id))
with Session(engine) as session:
category = session.scalar(stmt)
if category is None:
await interaction.response.send_message("Category or Group was not found.", ephemeral=True)
return
category.size = size
# session.add(category) test if this is needed. SQLAlchemy can do the update without adding.
session.commit()
# Use the relationship to get group category is in.
group = category.category_group
if group is None:
await interaction.response.send_message(
f"An error with {category.name} and group relationship. Contact developer.", ephemeral=True
)
agon_logger.error(f"{category.name}:{category.id} group relationship was None.")
return
await send_highscore_message(session, interaction.guild, group)
await interaction.response.send_message(f"{category.name} size has been updated to {size}.", ephemeral=True)
@set.subcommand(description="Registers the channel for a Category Group's messages.") @set.subcommand(description="Registers the channel for a Category Group's messages.")
async def group_output_channel( async def group_output_channel(
self, self,
@@ -41,7 +86,7 @@ class SetCog(commands.Cog, name="Set command."):
Args: Args:
interaction (nextcord.Interaction): Interaction that invoked the command. interaction (nextcord.Interaction): Interaction that invoked the command.
group_id (str): String representation of Group id to register channel to. Is SlashOption. group_id (str): String representation of Group id to register channel to. Is SlashOption.
channel (nextcord.abc.GuildChannel, optional): Channel to register channel (nextcord.abc.GuildChannel, optional): Channel to register.
Is a SlashOption. Limited to text channels, Defaults to None. Is a SlashOption. Limited to text channels, Defaults to None.
""" """
channel_id = interaction.channel.id if not channel else channel.id channel_id = interaction.channel.id if not channel else channel.id
@@ -183,6 +228,21 @@ class SetCog(commands.Cog, name="Set command."):
session.commit() session.commit()
await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True)
@category_size.on_autocomplete("category_id")
async def category_autocomplete(self, interaction: nextcord.Interaction, category_id: str, group_id: str) -> None:
"""
Autocomplete function for category names.
Args:
interaction (nextcord.Interaction): Interaction that invoked the command.
category_id (str): String to be autocompleted with Category name.
group_id (str): Group to find Category from.
"""
await interaction.response.send_autocomplete(
autofill_categories(interaction.guild_id, int(group_id), category_id)
)
@category_size.on_autocomplete("group_id")
@group_output_channel.on_autocomplete("group_id") @group_output_channel.on_autocomplete("group_id")
@pad_length.on_autocomplete("group_id") @pad_length.on_autocomplete("group_id")
@username_length.on_autocomplete("group_id") @username_length.on_autocomplete("group_id")