From 4e8032823de93fcc948d3852649905d32c051c87 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Tue, 7 Nov 2023 14:44:07 -0500 Subject: [PATCH] Fix check if election exists. Scalars doesn't return None if the thing doesn't exists while scalar does. --- cogs/commands/view.py | 47 ++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/cogs/commands/view.py b/cogs/commands/view.py index 7d6f7ef..2bd9175 100644 --- a/cogs/commands/view.py +++ b/cogs/commands/view.py @@ -49,22 +49,23 @@ class ViewCog(commands.Cog, name="View Commands"): stmt = select(Votes).where(Votes.ElectionCycle == election_name) with Session(engine) as session: - try: - votes = session.scalars(stmt) - content = "" - - for vote in votes: - content += ( - f"<@{vote.VoterId}> voted for <@{vote.VotedForId}>.\n" - if vote.VotedForId is not None - else f"<@{vote.VoterId}> abstained from voted.\n" - ) - - await interaction.response.send_message(content=content, ephemeral=True) - except: + # Check if election exists. + if session.scalar(stmt) is None: await interaction.response.send_message( f"Invalid election: {election_name}.", ephemeral=True ) + return + + votes = session.scalars(stmt) + content = "" + for vote in votes: + content += ( + f"<@{vote.VoterId}> voted for <@{vote.VotedForId}>.\n" + if vote.VotedForId is not None + else f"<@{vote.VoterId}> abstained from voted.\n" + ) + + await interaction.response.send_message(content=content, ephemeral=True) @votes.on_autocomplete("election_name") async def election_autocomplete( @@ -82,14 +83,14 @@ class ViewCog(commands.Cog, name="View Commands"): stmt = select(ElectionInformation.ElectionCycle) elections = session.scalars(stmt) - # if empty compare string - if not election_name: - await interaction.response.send_autocomplete(elections) - return + # if empty compare string + if not election_name: + await interaction.response.send_autocomplete(elections) + return - autofilled_elections: "list[str]" = [ - election - for election in elections - if election.lower().startswith(election_name.lower()) - ] - await interaction.response.send_autocomplete(autofilled_elections[:25]) + autofilled_elections: "list[str]" = [ + election + for election in elections + if election.lower().startswith(election_name.lower()) + ] + await interaction.response.send_autocomplete(autofilled_elections[:25])