From c69adb8374c3264c9af8216f03e384ce38afd597 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Fri, 22 Dec 2023 21:23:18 -0500 Subject: [PATCH 1/6] Limit items sent back on autocomplete (small bug) --- cogs/commands/view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/commands/view.py b/cogs/commands/view.py index 3a36379..35d0c56 100644 --- a/cogs/commands/view.py +++ b/cogs/commands/view.py @@ -126,7 +126,7 @@ class ViewCog(commands.Cog, name="View Commands"): # if empty compare string if not election_name: - await interaction.response.send_autocomplete(elections) + await interaction.response.send_autocomplete(elections[:25]) return autofilled_elections: "list[str]" = [ From 39746123f15847e25f34087ba9831527ddd6c8ee Mon Sep 17 00:00:00 2001 From: imcompiling Date: Fri, 22 Dec 2023 21:23:29 -0500 Subject: [PATCH 2/6] Add beginning of remove presence. --- cogs/commands/remove.py | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 cogs/commands/remove.py diff --git a/cogs/commands/remove.py b/cogs/commands/remove.py new file mode 100644 index 0000000..2fedef2 --- /dev/null +++ b/cogs/commands/remove.py @@ -0,0 +1,79 @@ +""" +Commands for removing information in the database. +""" + +import nextcord +from nextcord.ext import commands +from sqlalchemy import select +from sqlalchemy.orm import Session + +from database_setup.sql_data_classes import ( + ElectionInformation, + Presences, + Votes, + engine, +) + + +class RemoveCog(commands.Cog, name="View Commands"): + """ + Holds the view commands. + """ + + def __init__(self) -> None: + """ + Default constructor for RemoveCog class. Does nothing. + """ + + @nextcord.slash_command( + guild_ids=[1000247361639030804], + default_member_permissions=nextcord.Permissions(administrator=True), + ) # TODO remove guild ids + async def remove(self, interaction: nextcord.Interaction): + """ + Parent slash command for set. Never called. + """ + + @remove.subcommand(description="Remove a presence from the database.") + async def presence( + self, + interaction: nextcord.Interaction, + presence: str = nextcord.SlashOption( + description="The presence to remove", required=True + ), + ): + """ + _summary_ + + Args: + interaction (nextcord.Interaction): Interaction that invoked the command. + presence (str): Is SlashOption. The presence to remove. + """ + + @remove.on_autocomplete("presence") + async def election_autocomplete( + self, interaction: nextcord.Interaction, presence: str + ): + """ + Collects all presences that start with the presence string. + + Args: + interaction (nextcord.Interaction): Interaction with command. + presence (str): String to run autocomplete on. + """ + + with Session(engine) as session: + stmt = select(Presences) + presences = session.scalars(stmt) + + # if empty compare string + if not election_name: + await interaction.response.send_autocomplete(elections[:25]) + return + + autofilled_presences: "list[str]" = [ + election + for election in elections + if election.lower().startswith(election_name.lower()) + ] + await interaction.response.send_autocomplete(autofilled_presences[:25]) From deb405f02b2355d29204efe18cf6029fe6bf71a4 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Fri, 22 Dec 2023 21:35:52 -0500 Subject: [PATCH 3/6] Might not have worked without list cast. --- cogs/commands/view.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/commands/view.py b/cogs/commands/view.py index 35d0c56..4f6183c 100644 --- a/cogs/commands/view.py +++ b/cogs/commands/view.py @@ -126,7 +126,7 @@ class ViewCog(commands.Cog, name="View Commands"): # if empty compare string if not election_name: - await interaction.response.send_autocomplete(elections[:25]) + await interaction.response.send_autocomplete(list(elections)[:25]) return autofilled_elections: "list[str]" = [ From 2f75f0cdcbe3b4958a4c14ec4852742d7145a573 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Fri, 22 Dec 2023 21:56:29 -0500 Subject: [PATCH 4/6] Finish up presence auto complete function. --- cogs/commands/remove.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/cogs/commands/remove.py b/cogs/commands/remove.py index 2fedef2..aab8bb6 100644 --- a/cogs/commands/remove.py +++ b/cogs/commands/remove.py @@ -7,12 +7,7 @@ from nextcord.ext import commands from sqlalchemy import select from sqlalchemy.orm import Session -from database_setup.sql_data_classes import ( - ElectionInformation, - Presences, - Votes, - engine, -) +from database_setup.sql_data_classes import Presences, engine class RemoveCog(commands.Cog, name="View Commands"): @@ -56,6 +51,7 @@ class RemoveCog(commands.Cog, name="View Commands"): ): """ Collects all presences that start with the presence string. + Sends autocomplete dict {Presence Name, Presence Id} Args: interaction (nextcord.Interaction): Interaction with command. @@ -64,16 +60,20 @@ class RemoveCog(commands.Cog, name="View Commands"): with Session(engine) as session: stmt = select(Presences) - presences = session.scalars(stmt) + presences_dict: "dict[str,str]" = { + pres.Presence: str(pres.Id) for pres in session.scalars(stmt) + } # if empty compare string - if not election_name: - await interaction.response.send_autocomplete(elections[:25]) + if not presence: + await interaction.response.send_autocomplete( + dict(list(presences_dict.items())[:25]) + ) return - autofilled_presences: "list[str]" = [ - election - for election in elections - if election.lower().startswith(election_name.lower()) - ] + autofilled_presences: "dict[str,str]" = { + pres: pres_id + for pres, pres_id in presences_dict.items() + if presence.lower() in pres.lower() + } await interaction.response.send_autocomplete(autofilled_presences[:25]) From 2632bb883f0d674dab2be768a27f5ad34376aa0d Mon Sep 17 00:00:00 2001 From: imcompiling Date: Fri, 22 Dec 2023 22:05:07 -0500 Subject: [PATCH 5/6] Filled out remove presence some more. Not currently working. --- cogs/commands/__init__.py | 4 +++- cogs/commands/remove.py | 28 ++++++++++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/cogs/commands/__init__.py b/cogs/commands/__init__.py index f161699..d71232f 100644 --- a/cogs/commands/__init__.py +++ b/cogs/commands/__init__.py @@ -4,6 +4,7 @@ Cog loader. Adds all command cogs to the bot. from nextcord.ext import commands +from cogs.commands.remove import RemoveCog from cogs.commands.request import RequestCog from cogs.commands.set import setCog from cogs.commands.test import TestCog @@ -17,7 +18,8 @@ def setup(bot: commands.Bot): Adds all command classes to bot. """ + bot.add_cog(RemoveCog()) bot.add_cog(RequestCog(bot)) bot.add_cog(TestCog(bot)) - bot.add_cog(setCog()) bot.add_cog(ViewCog()) + bot.add_cog(setCog()) diff --git a/cogs/commands/remove.py b/cogs/commands/remove.py index aab8bb6..c3980e4 100644 --- a/cogs/commands/remove.py +++ b/cogs/commands/remove.py @@ -4,7 +4,7 @@ Commands for removing information in the database. import nextcord from nextcord.ext import commands -from sqlalchemy import select +from sqlalchemy import delete, select from sqlalchemy.orm import Session from database_setup.sql_data_classes import Presences, engine @@ -33,21 +33,29 @@ class RemoveCog(commands.Cog, name="View Commands"): async def presence( self, interaction: nextcord.Interaction, - presence: str = nextcord.SlashOption( + presence_str: str = nextcord.SlashOption( description="The presence to remove", required=True ), ): """ - _summary_ + Deletes the given presence id from the database. Args: interaction (nextcord.Interaction): Interaction that invoked the command. - presence (str): Is SlashOption. The presence to remove. + presence (str): Is SlashOption. The presence to remove. Is the ID of the presence. """ - @remove.on_autocomplete("presence") - async def election_autocomplete( - self, interaction: nextcord.Interaction, presence: str + presence_id = int(presence_str) + with Session(engine) as session: + stmt = delete(Presences).where(Presences.Id == presence_id) + session.execute(stmt) + session.commit() + + await interaction.response.send_message("Presence deleted.", ephemeral=True) + + @remove.on_autocomplete("presence_str") + async def presence_autocomplete( + self, interaction: nextcord.Interaction, presence_str: str ): """ Collects all presences that start with the presence string. @@ -55,7 +63,7 @@ class RemoveCog(commands.Cog, name="View Commands"): Args: interaction (nextcord.Interaction): Interaction with command. - presence (str): String to run autocomplete on. + presence_str (str): String to run autocomplete on. """ with Session(engine) as session: @@ -65,7 +73,7 @@ class RemoveCog(commands.Cog, name="View Commands"): } # if empty compare string - if not presence: + if not presence_str: await interaction.response.send_autocomplete( dict(list(presences_dict.items())[:25]) ) @@ -74,6 +82,6 @@ class RemoveCog(commands.Cog, name="View Commands"): autofilled_presences: "dict[str,str]" = { pres: pres_id for pres, pres_id in presences_dict.items() - if presence.lower() in pres.lower() + if presence_str.lower() in pres.lower() } await interaction.response.send_autocomplete(autofilled_presences[:25]) From 2cde9f7109d3c91b5afef6e986d7f898f99f0843 Mon Sep 17 00:00:00 2001 From: imcompiling Date: Sat, 23 Dec 2023 14:48:12 -0500 Subject: [PATCH 6/6] Fix bug preventing remove from working. Remove guild ids. --- cogs/commands/remove.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/cogs/commands/remove.py b/cogs/commands/remove.py index c3980e4..1f86346 100644 --- a/cogs/commands/remove.py +++ b/cogs/commands/remove.py @@ -10,7 +10,7 @@ from sqlalchemy.orm import Session from database_setup.sql_data_classes import Presences, engine -class RemoveCog(commands.Cog, name="View Commands"): +class RemoveCog(commands.Cog, name="Remove Commands"): """ Holds the view commands. """ @@ -21,9 +21,8 @@ class RemoveCog(commands.Cog, name="View Commands"): """ @nextcord.slash_command( - guild_ids=[1000247361639030804], default_member_permissions=nextcord.Permissions(administrator=True), - ) # TODO remove guild ids + ) async def remove(self, interaction: nextcord.Interaction): """ Parent slash command for set. Never called. @@ -53,7 +52,7 @@ class RemoveCog(commands.Cog, name="View Commands"): await interaction.response.send_message("Presence deleted.", ephemeral=True) - @remove.on_autocomplete("presence_str") + @presence.on_autocomplete("presence_str") async def presence_autocomplete( self, interaction: nextcord.Interaction, presence_str: str ):