Files
agon/cogs/commands/message_management.py
T
2022-11-24 17:05:45 -05:00

138 lines
5.7 KiB
Python

"""
Commands for managing/displaying the highscores messages.
"""
from typing import Optional
import nextcord
from nextcord.ext import commands
from cogs.submission.submission_views import SubmissionCreateButton # pylint: disable=import-error
from highscores import highscores_data # pylint: disable=import-error
from utilities.utils import autofill_tables, send_highscore_message # pylint: disable=import-error
class MessageManagementCog(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)
@nextcord.slash_command(
description="Manually refresh a table. Use when changing settings or data manually changed.",
default_member_permissions=nextcord.Permissions(administrator=True),
)
async def refresh_table(
self,
interaction: nextcord.Interaction,
table_name: Optional[str] = nextcord.SlashOption(description="Name of table.", required=True),
) -> None:
"""
Refreshes a table given by user.
Args:
interaction (nextcord.Interaction): Interaction that invoked the command.
table (Optional[str]): Table name to refresh. Not optional. Is SlashOption.
"""
guild = highscores_data.get_guild(interaction.guild_id)
table = guild.get_table(table_name)
channel = interaction.guild.get_channel(guild.highscore_channel)
message: nextcord.Message = await send_highscore_message(channel, guild, table)
await message.edit(view=SubmissionCreateButton(self.bot))
await interaction.response.send_message(f"Updated table {table_name}!", ephemeral=True)
@refresh_table.on_autocomplete("table_name")
async def refresh_table_autofill(self, interaction: nextcord.Interaction, table_name: str):
"""
Collects list of table names and compares to input table name for autocomplete assistance.
Args:
interaction (nextcord.Interaction): Interaction with command.
table_name (str): String to run autocomplete compared to.
"""
await interaction.response.send_autocomplete(autofill_tables(interaction.guild_id, table_name))