Adding autocomplete to /ship too
This commit is contained in:
parent
9589c08814
commit
4b8322ebff
@ -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)
|
||||
|
Loading…
x
Reference in New Issue
Block a user