""" Commands for managing the Guild's settings. """ from typing import Optional import nextcord from nextcord.ext import commands from sqlalchemy import select # pylint: disable=import-error from highscores import CategoryGroup, Guild, session from utilities.utils import autofill_groups class SettingsManagementCog(commands.Cog, name="Settings Management"): """ Class holds commands for managing Guild's settings. """ def __init__(self) -> None: """ Default constructor for SettingsManagement class. Does nothing. """ @nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True)) async def register(self, interaction: nextcord.Interaction): """ Parent slash command, never called. """ @register.subcommand(description="Registers the channel for a Category Group's messages.") async def group_output_channel( self, interaction: nextcord.Interaction, category_group: nextcord.SlashOption(description="Category Group to assign to channel.", required=True), channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( description="Channel to register", required=False, channel_types=[nextcord.ChannelType.text], default=None, ), ) -> None: """ Command function to register a CategoryGroup's output channel. Args: interaction (nextcord.Interaction): Interaction that invoked the command channel (nextcord.abc.GuildChannel, optional): Channel to register Is a SlashOption. Limited to text channels, Defaults to None. """ if not channel: channel = interaction.channel channel_id = channel.id stmt = ( select(CategoryGroup) .where(CategoryGroup.GuildId == interaction.guild_id) .where(CategoryGroup.GroupId == category_group) ) group = session.scalar(stmt) group.ChannelId = channel_id session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) @register.subcommand(description="Registers the channel for submission messages.") async def submission( self, interaction: nextcord.Interaction, channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( description="Channel to register", required=False, channel_types=[nextcord.ChannelType.text], default=None, ), ) -> None: """ Command function to register the submission message channel. Args: interaction (nextcord.Interaction): Interaction that invoked the command channel Optional[nextcord.abc.GuildChannel], optional): Channel to register Is a SlashOption. Limited to text channels, Defaults to None. """ if not channel: channel = interaction.channel channel_id = channel.id stmt = select(Guild).where(Guild.GuildId == interaction.guild_id) guild = session.scalar(stmt) guild.SubmissionChannel = channel_id session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) @register.subcommand(description="Registers the channel for verification messages.") async def verify( self, interaction: nextcord.Interaction, channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption( description="Channel to register", required=False, channel_types=[nextcord.ChannelType.text], default=None, ), ) -> None: """ Command function to register the verification message channel. Args: interaction (nextcord.Interaction): Interaction that invoked the command channel (Optional[nextcord.abc.GuildChannel], optional): Channel to register Is a SlashOption. Limited to text channels, Defaults to None. """ if not channel: channel = interaction.channel channel_id = channel.id stmt = select(Guild).where(Guild.GuildId == interaction.guild_id) guild = session.scalar(stmt) guild.VerificationChannel = channel_id session.commit() await interaction.send(f"Registered <#{channel_id}>.", ephemeral=True) @nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True)) async def set(self, interaction: nextcord.Interaction): """ Parent slash command for setting settings. Never called. """ @set.subcommand( description="Set character length for a category. Can cut off username/score. Affects all categories." ) async def category_pad_length( self, interaction: nextcord.Interaction, category_group: nextcord.SlashOption(description="Category Group to assign to channel.", required=True), length: Optional[int] = nextcord.SlashOption(description="Number of characters.", required=True, default=26), ) -> None: """ Changes guild's category pad length. Args: interaction (nextcord.Interaction): Interaction that invoked the command length (Optional[int]): Number of characters. Is SlashOption. Defaults to 26. """ stmt = ( select(CategoryGroup) .where(CategoryGroup.GuildId == interaction.guild_id) .where(CategoryGroup.GroupId == category_group) ) group = session.scalar(stmt) group.PadLength = length session.commit() await interaction.response.send_message( f"Category pad length set to {length} for {group.GroupName}", ephemeral=True ) @group_output_channel.on_autocomplete("category_group") @category_pad_length.on_autocomplete("category_group") async def group_id_autocomplete(self, interaction: nextcord.Interaction, category_group: str): """ Sends results from autofill_groups back to group_output_channel for autocompletion of categoru_group. Args: interaction (nextcord.Interaction): Interaction with command. category_group (str): String to run autocompletion against. """ await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, category_group)) @set.subcommand(description="Set username length. Limits all submissions to this length.") async def username_length( self, interaction: nextcord.Interaction, length: Optional[int] = nextcord.SlashOption(description="Number of characters.", required=True, default=12), ) -> None: """ Changes guild's username length limit. Args: interaction (nextcord.Interaction): Interaction that invoked the command length (Optional[int]): Number of charactesr. Is SlashOption. Defaults to 12. """ stmt = select(Guild).where(Guild.GuildId == interaction.guild_id) guild = session.scalar(stmt) guild.UsernameLength = length session.commit() await interaction.response.send_message(f"Username length set to {length}", ephemeral=True)