Add command to view presences.
This commit is contained in:
+43
-2
@@ -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."
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user