From 8aa186de075912b74d65ece82da21b6de6677bdd Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Fri, 29 Sep 2023 08:51:21 -0700 Subject: [PATCH] Adding autocomplete to /ship too --- app/cogs/star_citizen.py | 45 ++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/app/cogs/star_citizen.py b/app/cogs/star_citizen.py index 761c4090..77da3b62 100644 --- a/app/cogs/star_citizen.py +++ b/app/cogs/star_citizen.py @@ -21,17 +21,50 @@ class StarCitizen(commands.Cog): async def post_medpen_guide(self, ctx: commands.Context): await ctx.respond("https://i.redd.it/lfswlf5c13t71.png") + async def get_all_ships(ctx: discord.AutocompleteContext): + """ + returns a list of all ships in the game, which can then be passed to the /ship command for auto complete + """ + url = "https://starcitizen.tools/List_of_pledge_vehicles" + + response = requests.get(url).text + soup = BeautifulSoup(response, "html.parser") + + # Find the table with the "Name" row + table = soup.find("table", {"class": "wikitable"}) + name_column_index = None + + # Find the index of the "Name" column + header_row = table.find("tr") + headers = header_row.find_all("th") + for i, header in enumerate(headers): + if header.text.strip() == "Name": + name_column_index = i + break + + if name_column_index is not None: + # Extract all the items in the "Name" column + all_ships = [] + rows = table.find_all("tr") + for row in rows[1:]: # Skip the header row + columns = row.find_all("td") + if len(columns) > name_column_index: + name = columns[name_column_index].text.strip() + all_ships.append(name) + return all_ships + @commands.slash_command( guild_ids=None, name="ship", description="Query the star citizen database about a ship", ) - @option( - name="ship", - description="Ship you want info on, must be the exact name of the ship, eg Aegs Avenger", - required=True, - ) - async def get_ship(self, ctx: commands.Context, ship): + async def get_ship( + self, + ctx: commands.Context, + ship: discord.Option( + str, autocomplete=discord.utils.basic_autocomplete(get_all_ships) + ), + ): await ctx.defer() embed = await star_citizen.get_ship(ship_name=ship) await ctx.send_followup(embed=embed)