Update post highscores command to slash command.

This commit is contained in:
alwayscompiling
2022-08-08 21:51:06 -04:00
parent 3fd9ff46a3
commit 57244bcbfe
+30 -19
View File
@@ -13,35 +13,44 @@ from utilities.utils import send_highscore_message # pylint: disable=import-err
class PostHighscores(commands.Cog, name="Post Highscores"): class PostHighscores(commands.Cog, name="Post Highscores"):
"""Posts 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 self.bot = bot
@commands.command(name="post-scores") @nextcord.slash_command(
@commands.has_permissions(administrator=True) description="Triggers post/update of highscores to registered highscore channel.",
async def post_highscores(self, ctx: commands.Context): 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. 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}") 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 = "Registered Highscores channel does not exist or was never registered. "
error_response += 'Register with "?register" command.' error_response += 'Use "/register" to register channels.'
assert channel is not None, await ctx.send(error_response) assert channel is not None, await interaction.response(error_response, ephemeral=True)
if len(highscores_data.get_guild(ctx.guild.id).tables) < 1: # Error checking for tables existing
await channel.send( # TODO fill out response string once functionality created.
"No tables have been created for this guild. Please create some tables." error_response = "No tables have been created for this guild. Please create some tables."
) assert (
return 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) message: nextcord.Message = await send_highscore_message(channel, guild, table)
await message.edit(view=SubmissionCreateButton(self.bot)) await message.edit(view=SubmissionCreateButton(self.bot))
@@ -49,6 +58,8 @@ class PostHighscores(commands.Cog, name="Post Highscores"):
# messages have been sent. # messages have been sent.
highscores_data.save_guild_data(guild) 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. # This function will be called when this extension is loaded.
# It is necessary to add these functions to the bot. # It is necessary to add these functions to the bot.