Create shell commands and autofill functions. Fix set initializer in utils.autofill_users.

This commit is contained in:
alwayscompiling
2022-11-22 17:12:26 -05:00
parent 03c7a9303a
commit 8a2108fc14
2 changed files with 101 additions and 20 deletions
+100 -19
View File
@@ -2,11 +2,16 @@
Commands for managing the tables/categories/scores for a Guild.
"""
from nextcord.ext import commands
import nextcord
from nextcord.ext import commands
from highscores import highscores_data # pylint: disable=import-error
from utilities.utils import send_highscore_message # pylint: disable=import-error
from utilities.utils import ( # pylint: disable=import-error
autofill_categories,
autofill_users,
autofill_tables,
send_highscore_message,
)
from .views.update_username_verify import UpdateUsernameVerifyView
@@ -35,9 +40,7 @@ class DataManagementCog(commands.Cog, name="Table Management"):
Parent slash command for deleting things. Never called.
"""
@delete.subcommand(
description="Deletes all scores for this guild. Used for testing. Do Not Use."
)
@delete.subcommand(description="Deletes all scores for this guild. Used for testing. Do Not Use.")
async def scores(self, interaction: nextcord.Interaction):
"""
Warning: This command only works for developers.
@@ -52,18 +55,22 @@ class DataManagementCog(commands.Cog, name="Table Management"):
for category in table.categories:
category.clear()
highscores_data.save_guild_data(highscores_data.get_guild(interaction.guild_id))
await interaction.response.send_message(
f"Cleared score data for {interaction.guild.name}.", ephemeral=True
)
await interaction.response.send_message(f"Cleared score data for {interaction.guild.name}.", ephemeral=True)
else:
await interaction.response.send_message(
"You shouldn't be using this command. Deletes everything.", ephemeral=True
)
@delete.subcommand(
@delete.subcommand()
async def user(self, interaction: nextcord.Interaction):
"""
Subcommand parent for deleting user information. Never called.
"""
@user.subcommand(
description="Removes all of a user's scores. Warning: data is destroyed, use with case.",
)
async def user(
async def from_all(
self,
interaction: nextcord.Interaction,
username: str = nextcord.SlashOption(
@@ -92,9 +99,7 @@ class DataManagementCog(commands.Cog, name="Table Management"):
table_updated = True
if table_updated:
await send_highscore_message(
interaction.guild.get_channel(guild.highscore_channel), guild, table
)
await send_highscore_message(interaction.guild.get_channel(guild.highscore_channel), guild, table)
if not user_was_found:
await interaction.send(f'User "{username}" was not found.', ephemeral=True)
@@ -102,16 +107,92 @@ class DataManagementCog(commands.Cog, name="Table Management"):
await interaction.send(f'User "{username}" was removed from all tables.')
highscores_data.save_guild_data(guild)
@user.subcommand(description="Deletes a user's scores from a category.")
async def from_category(
self,
interaction: nextcord.Interaction,
table_name: str = nextcord.SlashOption(description="Table to delete user from.", required=True),
category_name: str = nextcord.SlashOption(description="Category to delete user from.", required=True),
user_name: str = nextcord.SlashOption(description="User to delete.", required=True),
):
"""
_summary_
Args:
interaction (nextcord.Interaction): _description_
table_name (str, optional): _description_. Defaults to nextcord.SlashOption(description="", required=True).
category_name (str, optional): _description_. Defaults to nextcord.SlashOption(description="", required=True).
user_name (str, optional): _description_. Defaults to nextcord.SlashOption(description="", required=True).
"""
@user.subcommand(description="Deletes a user's scores from entire table.")
async def from_table(
self,
interaction: nextcord.Interaction,
table_name: str = nextcord.SlashOption(description="Table to delete user from.", required=True),
user_name: str = nextcord.SlashOption(description="User to delete.", required=True),
):
"""
_summary_
Args:
interaction (nextcord.Interaction): _description_
table_name (str, optional): _description_. Defaults to nextcord.SlashOption(description="Table to delete user from.", required=True).
user_name (str, optional): _description_. Defaults to nextcord.SlashOption(description="User to delete.", required=True).
"""
@from_category.on_autocomplete("table_name")
@from_table.on_autocomplete("table_name")
async def from_category_autocomplete_table(self, interaction: nextcord.Interaction, table_name: str):
"""
_summary_
Args:
interaction (nextcord.Interaction): _description_
table_name (str): _description_
"""
await interaction.response.send_autocomplete(autofill_tables(interaction.guild_id, table_name))
@from_category.on_autocomplete("category_name")
async def from_category_autocomplete_category(
self, interaction: nextcord.Interaction, category_name: str, table_name: str
):
"""
_summary_
Args:
interaction (nextcord.Interaction): _description_
category_name (str): _description_
table_name (str): _description_
"""
await interaction.response.send_autocomplete(
autofill_categories(interaction.guild_id, table_name, category_name)
)
@from_category.on_autocomplete("user_name")
@from_table.on_autocomplete("user_name")
async def from_category_autocomplete_user(
self, interaction: nextcord.Interaction, user_name: str, table_name: str, category_name: str = None
):
"""
_summary_
Args:
interaction (nextcord.Interaction): _description_
user_name (str): _description_
table_name (str): _description_
category_name (str): _description_
"""
await interaction.response.send_autocomplete(
autofill_users(interaction.guild_id, table_name, category_name, user_name)
)
@nextcord.slash_command(description="Request a username update. Admins must approve.")
async def update_username(
self,
interaction: nextcord.Interaction,
old_username: str = nextcord.SlashOption(
description="Username currently stored in highscores.", required=True
),
new_username: str = nextcord.SlashOption(
description="Username to update to.", required=True
),
old_username: str = nextcord.SlashOption(description="Username currently stored in highscores.", required=True),
new_username: str = nextcord.SlashOption(description="Username to update to.", required=True),
):
"""
Creates a request to send to admins for changing a username. Uses
+1 -1
View File
@@ -104,7 +104,7 @@ def autofill_users(guild_id: int, table_name: str, category_name: str, user_begi
# If no category name, get all users in table.
if category_name is None:
user_set: set
user_set: set = set()
for category in highscores_data.get_guild(guild_id).get_table(table_name).get_categories().values():
user_set = user_set | set(category.get_users())