From 0a18d819afecade9a9812df4c9056ba19bb4d211 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Fri, 22 Dec 2023 16:32:32 -0500 Subject: [PATCH] Add command to view presences. --- cogs/commands/view.py | 45 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/cogs/commands/view.py b/cogs/commands/view.py index 2bd9175..26b2c76 100644 --- a/cogs/commands/view.py +++ b/cogs/commands/view.py @@ -7,7 +7,12 @@ from nextcord.ext import commands from sqlalchemy import select from sqlalchemy.orm import Session -from database_setup.sql_data_classes import ElectionInformation, Votes, engine +from database_setup.sql_data_classes import ( + ElectionInformation, + Presences, + Votes, + engine, +) class ViewCog(commands.Cog, name="View Commands"): @@ -20,12 +25,48 @@ class ViewCog(commands.Cog, name="View Commands"): Default constructor for ViewCog class. Does nothing. """ - @nextcord.slash_command() + @nextcord.slash_command(guild_ids=[1000247361639030804]) # TODO remove guild ids async def view(self, interaction: nextcord.Interaction): """ Parent slash command for set. Never called. """ + @view.subcommand(description="Show all possible presence choices.") + async def presences( + self, + interaction: nextcord.Interaction, + search: str = nextcord.SlashOption( + description="A word or string to search for in the presences.", + required=False, + ), + ): + """ + Gathers all presences from the database and responds with a message showing them. + + Args: + interaction (nextcord.Integration): User's interaction with the command. + """ + + # might take a while depending on number of presences + await interaction.response.defer(ephemeral=True) + + with Session(engine) as session: + stmt = select(Presences.Presence) + presences = session.scalars(stmt) + + message = "" + + # have 2 loops so that the "if search" check isn't done repeatedly + if search: + for presence in presences: + if search.lower() in presence.lower(): + message += f"{presence}\n" + else: + for presence in presences: + message += f"{presence}\n" + + await interaction.followup.send(content=message, ephemeral=True) + @view.subcommand( description="Shows all voters and who they voted for in a given election." )