Finish up smells in the commands directory.

Add a discord max message length constant.
Move boolean argument to not be positional.
This commit is contained in:
2024-05-22 20:48:44 -04:00
parent 9331e7ba4f
commit b9621a67c9
9 changed files with 180 additions and 155 deletions
+34 -27
View File
@@ -1,6 +1,4 @@
"""
Commands for viewing information in the database.
"""
"""Commands for viewing information in the database."""
import nextcord
import pandas as pd
@@ -14,24 +12,18 @@ from database_setup.sql_data_classes import (
Votes,
engine,
)
from utils import VoteType
from utils import MAX_DISCORD_MESSAGE_LENGTH, VoteType
class ViewCog(commands.Cog, name="View Commands"):
"""
Holds the view commands.
"""
"""Holds the view commands."""
def __init__(self) -> None:
"""
Default constructor for ViewCog class. Does nothing.
"""
"""Default constructor for ViewCog class. Does nothing."""
@nextcord.slash_command()
async def view(self, interaction: nextcord.Interaction):
"""
Parent slash command for set. Never called.
"""
async def view(self, interaction: nextcord.Interaction) -> None:
"""Parent slash command for set. Never called."""
@view.subcommand(description="Show event points.")
async def event_points(
@@ -41,38 +33,49 @@ class ViewCog(commands.Cog, name="View Commands"):
description="The quarter to view",
required=False,
),
):
) -> None:
"""
View the event points.
Builds a message with the event points for the quarter that matches with quarter_name.
Args:
interaction (nextcord.Interaction): Interaction with the message.
quarter_name (str, optional): Same of quarter to autofill with. Is SlashOption.
"""
# just in case
await interaction.response.defer(ephemeral=True)
with Session(engine) as session:
if quarter_name:
df = pd.read_sql_query(
ppdf = pd.read_sql_query(
sql=session.query(ParticipationPoints)
.where(ParticipationPoints.Cycle.startswith(quarter_name))
.statement,
con=engine,
)
else:
df = pd.read_sql_query(
ppdf = pd.read_sql_query(
sql=session.query(ParticipationPoints).statement,
con=engine,
)
if df.empty:
if ppdf.empty:
await interaction.followup.send(f'Quarter "{quarter_name}" is invalid.', ephemeral=True)
return
cycles = df.groupby("Cycle")
cycles = ppdf.groupby("Cycle")
content = ""
for cycle in cycles.groups:
content += f"**{cycle}**\n"
unsorted_all = cycles.get_group(cycle).groupby("DiscordMember")
members = unsorted_all.get_group(True)
non_members = unsorted_all.get_group(False)
members = unsorted_all.get_group(name=True)
non_members = unsorted_all.get_group(name=False)
sorted_members = members.sort_values(
by="UserId",
key=lambda user_ids: ([interaction.guild.get_member(int(id)).display_name for id in user_ids]),
key=lambda user_ids: (
[interaction.guild.get_member(int(user_id)).display_name for user_id in user_ids]
),
)
sorted_non_members = non_members.sort_values(by="UserId")
sorted_all = pd.concat([sorted_members, sorted_non_members], ignore_index=True, sort=False)
@@ -83,7 +86,7 @@ class ViewCog(commands.Cog, name="View Commands"):
content += f"<@{userid}>: {points}\n" if discord_member else f"{userid}: {points}\n"
content += "\n"
if len(content) > 2000:
if len(content) > MAX_DISCORD_MESSAGE_LENGTH:
await interaction.followup.send("Message length is too long, please select search to limit results.")
return
@@ -97,12 +100,13 @@ class ViewCog(commands.Cog, name="View Commands"):
description="A word or string to search for in the presences.",
required=False,
),
):
) -> None:
"""
Gathers all presences from the database and responds with a message showing them.
Args:
interaction (nextcord.Integration): User's interaction with the command.
search (str): string to search for in presences. Is SlashOption.
"""
# might take a while depending on number of presences
await interaction.response.defer(ephemeral=True)
@@ -130,8 +134,10 @@ class ViewCog(commands.Cog, name="View Commands"):
description="The election type. Council, Discord Manager, or Clan Leader.",
choices=VoteType,
),
):
) -> None:
"""
View all voters and who they voted for.
Takes in an election name and sends a response message to the user with a list of people who voted in that
election and who they voted for. Notes those tho abstained. Tells a user if the election is invalid. Uses
autocompletion to assist with getting elections.
@@ -139,6 +145,7 @@ class ViewCog(commands.Cog, name="View Commands"):
Args:
interaction (nextcord.Interaction): User's interaction with the command.
election_name (str): Is SlashOption. The name of the election to view votes for.
election_type(str): Is SlashOption. The type of election to search for. Choice from StrEnum VoteType.
"""
stmt = select(Votes).where(Votes.ElectionCycle == election_name).where(Votes.Type == election_type)
@@ -160,7 +167,7 @@ class ViewCog(commands.Cog, name="View Commands"):
await interaction.response.send_message(content=content, ephemeral=True)
@votes.on_autocomplete("election_name")
async def election_autocomplete(self, interaction: nextcord.Interaction, election_name: str):
async def election_autocomplete(self, interaction: nextcord.Interaction, election_name: str) -> None:
"""
Collects all elections that start with the election string.
@@ -183,7 +190,7 @@ class ViewCog(commands.Cog, name="View Commands"):
await interaction.response.send_autocomplete(autofilled_elections[:25])
@event_points.on_autocomplete("quarter_name")
async def event_points_autocomplete(self, interaction: nextcord.Interaction, quarter_name: str):
async def event_points_autocomplete(self, interaction: nextcord.Interaction, quarter_name: str) -> None:
"""
Collects all elections that start with the election string.