Updated message management to use new database.

This commit is contained in:
alwayscompiling
2022-12-01 17:07:50 -05:00
parent 10e8e5a446
commit 439ad539a8
2 changed files with 58 additions and 43 deletions
+52 -37
View File
@@ -6,10 +6,12 @@ from typing import Optional
import nextcord
from nextcord.ext import commands
from sqlalchemy import select
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
# pylint: disable=import-error
from cogs.submission.submission_views import SubmissionCreateButton
from highscores import CategoryGroup, session
from utilities.utils import autofill_groups, send_highscore_message
class MessageManagementCog(commands.Cog, name="Message Management"):
@@ -30,23 +32,42 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
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:
async def delete_channel_messages(
self,
interaction: nextcord.Interaction,
channel: Optional[nextcord.abc.GuildChannel] = nextcord.SlashOption(
description="Channel to clear of messages.",
required=False,
channel_types=[nextcord.ChannelType.text],
default=None,
),
) -> None:
"""
Deletes all table messages from highscores channel. Does not change data.
Does not delete highscores channel id.
Deletes all group messages from channel. Does not change data.
Does not delete highscores channel id. Uses current channel if not supplied a channel.
Args:
interaction (nextcord.Interaction): Interaction that invoked the command
channel (Optional[nextcord.abc.GuildChannel], optional): Channel to clear of messages.
Is a SlashOption. Limited to text channels. Defaults to None.
"""
guild = highscores_data.get_guild(interaction.guild_id)
highscores_channel: nextcord.TextChannel = interaction.guild.get_channel(guild.highscore_channel)
if not channel:
channel = interaction.channel
stmt = (
select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id)
.where(CategoryGroup.ChannelId == channel.id)
)
# TODO test when nothing in channel
groups = session.scalars(stmt)
await interaction.response.defer(ephemeral=True)
for table in guild.tables:
for group in groups:
try:
table_message: nextcord.Message = await highscores_channel.fetch_message(table.message_id)
table_message: nextcord.Message = await channel.fetch_message(group.MessageId)
await table_message.delete()
except nextcord.NotFound:
# silently ignore that messages are not found. They got deleted already.
@@ -69,62 +90,56 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
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)
stmt = select(CategoryGroup).where(CategoryGroup.GuildId == interaction.guild_id)
for group in session.scalars(stmt):
channel = interaction.guild.get_channel(group.ChannelId)
# 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)
message: nextcord.Message = await send_highscore_message(channel, group)
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)
await interaction.send(f"Posted all groups in <#{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(
async def refresh_group(
self,
interaction: nextcord.Interaction,
table_name: Optional[str] = nextcord.SlashOption(description="Name of table.", required=True),
group_id: Optional[int] = 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.
group_id (Optional[int]): Group id 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)
stmt = (
select(CategoryGroup)
.where(CategoryGroup.GuildId == interaction.guild_id)
.where(CategoryGroup.GroupId == group_id)
)
group = session.scalar(stmt)
message: nextcord.Message = await send_highscore_message(channel, guild, table)
channel = interaction.guild.get_channel(group.ChannelId)
message: nextcord.Message = await send_highscore_message(channel, group)
await message.edit(view=SubmissionCreateButton(self.bot))
await interaction.response.send_message(f"Updated table {table_name}!", ephemeral=True)
await interaction.response.send_message(f"Updated table {group.GroupName}!", ephemeral=True)
@refresh_table.on_autocomplete("table_name")
@refresh_group.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.
@@ -134,4 +149,4 @@ class MessageManagementCog(commands.Cog, name="Message Management"):
table_name (str): String to run autocomplete compared to.
"""
await interaction.response.send_autocomplete(autofill_tables(interaction.guild_id, table_name))
await interaction.response.send_autocomplete(autofill_groups(interaction.guild_id, table_name))
+1 -1
View File
@@ -269,7 +269,7 @@ async def send_highscore_message(channel: nextcord.TextChannel, group: CategoryG
Args:
channel (nextcord.TextChannel): Discord channel object where message is sent
group (group): group to post to the message
group (CategoryGroup): group to post to the message
Returns:
nextcord.Message: Message object that was posted with group contents