102 lines
4.2 KiB
Python
102 lines
4.2 KiB
Python
"""
|
|
Commands for managing/displaying the highscores messages.
|
|
"""
|
|
|
|
from nextcord.ext import commands
|
|
import nextcord
|
|
|
|
from cogs.submission.submission_views import SubmissionCreateButton # pylint: disable=import-error
|
|
from highscores import highscores_data # pylint: disable=import-error
|
|
from utilities.utils import send_highscore_message # pylint: disable=import-error
|
|
|
|
|
|
class MessageManagement(commands.Cog, name="Message Management"):
|
|
"""
|
|
Class holds commands for managing the messages that display highscores.
|
|
"""
|
|
|
|
def __init__(self, bot: commands.Bot) -> None:
|
|
"""
|
|
Default constructor for MessageManagement class
|
|
|
|
Args:
|
|
bot (commands.Bot): Bot object. Required to register submission button view.
|
|
"""
|
|
self.bot = bot
|
|
|
|
@nextcord.slash_command(
|
|
description="Clears highscores channel of all table messages. Does not change data.",
|
|
default_member_permissions=nextcord.Permissions(administrator=True),
|
|
)
|
|
async def delete_table_messages(self, interaction: nextcord.Interaction) -> None:
|
|
"""
|
|
Deletes all table messages from highscores channel. Does not change data.
|
|
Does not delete highscores channel id.
|
|
|
|
Args:
|
|
interaction (nextcord.Interaction): Interaction that invoked the command
|
|
"""
|
|
guild = highscores_data.get_guild(interaction.guild_id)
|
|
|
|
highscores_channel: nextcord.TextChannel = interaction.guild.get_channel(
|
|
guild.highscore_channel
|
|
)
|
|
|
|
await interaction.response.defer(ephemeral=True)
|
|
|
|
for table in guild.tables:
|
|
try:
|
|
table_message: nextcord.Message = await highscores_channel.fetch_message(
|
|
table.message_id
|
|
)
|
|
await table_message.delete()
|
|
except nextcord.NotFound:
|
|
# silently ignore that messages are not found. They got deleted already.
|
|
pass
|
|
|
|
await interaction.send("Table messages deleted.", ephemeral=True)
|
|
|
|
@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 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
|
|
"""
|
|
guild = highscores_data.get_guild(interaction.guild_id)
|
|
print(f"Printing out highscores information in 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 += 'Use "/register" to register channels.'
|
|
assert channel is not None, await interaction.response(error_response, ephemeral=True)
|
|
|
|
# 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.send(error_response, ephemeral=True)
|
|
|
|
# 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))
|
|
|
|
# have to save data here in case message id was changed. rather save here after all
|
|
# messages have been sent.
|
|
highscores_data.save_guild_data(guild)
|
|
|
|
await interaction.send(f"Posted all tables to <#{channel.id}>.", ephemeral=True)
|