""" Commands for refreshing highscore messages. """ import nextcord from nextcord.ext import commands from sqlalchemy import select from sqlalchemy.orm import Session # pylint: disable=import-error from cogs.submission.submission_views import SubmissionCreateButton from database_setup.sql_data_classes import CategoryGroup, engine from utilities.utils import autofill_groups, send_highscore_message class RefreshCog(commands.Cog, name="Refresh Cog"): """ Class holds commands for refreshing. """ def __init__(self, bot: commands.Bot) -> None: """ Default constructor for RefreshCog class Args: bot (commands.Bot): Bot object. Required to register submission button view. """ self._bot = bot @nextcord.slash_command(default_member_permissions=nextcord.Permissions(administrator=True)) async def refresh(self, interaction: nextcord.Interaction): """ Parent slash command for refreshing things. Never called. """ @refresh.subcommand(description="Manually refresh a table. Use when changing settings or data manually changed.") async def group( self, interaction: nextcord.Interaction, group_id: str = nextcord.SlashOption(name="group", description="Name of Group.", required=True), ) -> None: """ Refreshes a table given by user. Args: interaction (nextcord.Interaction): Interaction that invoked the command. group_id (str): Str representation of Group id to refresh. Is SlashOption. """ with Session(engine) as session: stmt = ( select(CategoryGroup) .where(CategoryGroup.GuildId == interaction.guild_id) .where(CategoryGroup.GroupId == int(group_id)) ) group = session.scalar(stmt) message: nextcord.Message = await send_highscore_message(session, interaction.guild, group) await message.edit(view=SubmissionCreateButton(self._bot)) await interaction.response.send_message(f"Updated table {group.GroupName}!", ephemeral=True) @refresh.subcommand() async def all(self, interaction: nextcord.Interaction): """ Subcommand parent for refreshing all things. Never called. """ @all.subcommand(description="Triggers post/update of highscores to registered highscore channels.") async def groups(self, interaction: nextcord.Interaction) -> None: """ A command which posts the highscore in the registered channel. Sends warning if no channel has been registered. Either creates new messages for each table if one doesn't exist or edits the existing message. Args: interaction (nextcord.Interaction): Interaction that invoked the command. """ with Session(engine) as session: stmt = select(CategoryGroup).where(CategoryGroup.GuildId == interaction.guild_id) # awaiting response because posting scores takes a while await interaction.response.defer(ephemeral=True) for group in session.scalars(stmt): message: nextcord.Message = await send_highscore_message(session, interaction.guild, group) await message.edit(view=SubmissionCreateButton(self._bot)) await interaction.send(f"Posted all groups in {interaction.guild.name}'s highscores.", ephemeral=True) @group.on_autocomplete("group_id") async def group_autocomplete(self, interaction: nextcord.Interaction, group_id: str): """ Collects list of table names and compares to input table name for autocomplete assistance. Args: interaction (nextcord.Interaction): Interaction with command. group_id (str): String to run autocomplete compared to. """ await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, group_id))