From 57244bcbfe15c758830b72f0edbd07ca93be216a Mon Sep 17 00:00:00 2001 From: alwayscompiling Date: Mon, 8 Aug 2022 21:51:06 -0400 Subject: [PATCH] Update post highscores command to slash command. --- cogs/post_highscores/__init__.py | 49 +++++++++++++++++++------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/cogs/post_highscores/__init__.py b/cogs/post_highscores/__init__.py index 8aa929a..b6b39bd 100644 --- a/cogs/post_highscores/__init__.py +++ b/cogs/post_highscores/__init__.py @@ -13,35 +13,44 @@ from utilities.utils import send_highscore_message # pylint: disable=import-err class PostHighscores(commands.Cog, name="Post Highscores"): """Posts Highscores.""" - def __init__(self, bot: commands.Bot): + def __init__(self, bot: commands.Bot) -> None: + """ + Default constructor for PostHighscores class + + Args: + bot (commands.Bot): Bot object. Required to register submission button view. + """ self.bot = bot - @commands.command(name="post-scores") - @commands.has_permissions(administrator=True) - async def post_highscores(self, ctx: commands.Context): + @nextcord.slash_command( + description="Triggers post/update of highscores to registered highscore channel.", + default_member_permissions=nextcord.Permissions(administrator=True), + ) + async def post_highscore_tables(self, interaction: nextcord.Interaction) -> None: """ - A command which posts the highscore in the designated channel. + A command which posts the highscore in the registered channel. Sends warning if no channel has been registered. - Usage: - ``` - ?post-scores - ``` """ - guild = highscores_data.get_guild(ctx.guild.id) + guild = highscores_data.get_guild(interaction.guild_id) print(f"Printing out highscores information in channel {guild.highscore_channel}") - channel = ctx.guild.get_channel(guild.highscore_channel) + channel = interaction.guild.get_channel(guild.highscore_channel) + # Error checking for registered highscore channel error_response = "Registered Highscores channel does not exist or was never registered. " - error_response += 'Register with "?register" command.' - assert channel is not None, await ctx.send(error_response) + error_response += 'Use "/register" to register channels.' + assert channel is not None, await interaction.response(error_response, ephemeral=True) - if len(highscores_data.get_guild(ctx.guild.id).tables) < 1: - await channel.send( - "No tables have been created for this guild. Please create some tables." - ) - return + # Error checking for tables existing + # TODO fill out response string once functionality created. + error_response = "No tables have been created for this guild. Please create some tables." + assert ( + len(highscores_data.get_guild(interaction.guild_id).tables) > 0 + ), await interaction.response(error_response, ephemeral=True) - for table in highscores_data.get_guild(ctx.guild.id).tables: + # awaiting response because posting scores takes a while + await interaction.response.defer(ephemeral=True) + + for table in highscores_data.get_guild(interaction.guild_id).tables: message: nextcord.Message = await send_highscore_message(channel, guild, table) await message.edit(view=SubmissionCreateButton(self.bot)) @@ -49,6 +58,8 @@ class PostHighscores(commands.Cog, name="Post Highscores"): # messages have been sent. highscores_data.save_guild_data(guild) + await interaction.followup.send(f"Posted all tables to <#{channel.id}>.", ephemeral=True) + # This function will be called when this extension is loaded. # It is necessary to add these functions to the bot.