From 087f75bfafb7e75c352b554a6bf01dc9f0247735 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Fri, 29 Sep 2023 08:26:21 -0700 Subject: [PATCH] Add request caching + autocomplete to the /trade function --- .gitignore | 3 +-- Makefile | 4 ++-- app/bot.py | 4 ++++ app/cogs/star_citizen.py | 29 +++++++++++++++++++++-------- app/requirements.txt | 1 + app/star_citizen.py | 6 ------ 6 files changed, 29 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 2d9272be..da01c0d5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,9 +1,8 @@ .DS_Store -dragon_bot_env *.pyc __pycache__ *.csv -*env *conflict* *syncthing* .env +*.sqlite \ No newline at end of file diff --git a/Makefile b/Makefile index 33cc3df3..f61b4669 100644 --- a/Makefile +++ b/Makefile @@ -10,5 +10,5 @@ build-test: docker build -f ./Dockerfile-test-env -t dragon-bot-test . DRAGON_ENV := "test" -test: clean format build-test - docker run -ti -v ${PWD}/app:/app --rm --name dragon-bot-test -e DRAGON_ENV=${DRAGON_ENV} -e discord_token=${discord_token} dragon-bot-test bash +test: format build-test + docker run -ti -v ${PWD}/app:/app --rm --name dragon-bot-test --env-file=./.env dragon-bot-test bash diff --git a/app/bot.py b/app/bot.py index 56e7928e..a8d7071d 100755 --- a/app/bot.py +++ b/app/bot.py @@ -20,6 +20,10 @@ cogfiles = [ for cogfile in cogfiles: bot.load_extension(cogfile) +import requests_cache + +requests_cache.install_cache(expire_after=180) + @bot.event async def on_ready(): diff --git a/app/cogs/star_citizen.py b/app/cogs/star_citizen.py index cb087386..761c4090 100644 --- a/app/cogs/star_citizen.py +++ b/app/cogs/star_citizen.py @@ -5,7 +5,6 @@ import discord import os import requests - import star_citizen @@ -13,7 +12,6 @@ class StarCitizen(commands.Cog): def __init__(self, bot): self.bot: commands.Bot = bot self.poll_status_page.start() - # self.poll_for_patch_notes.start() @commands.slash_command( guild_ids=None, @@ -38,16 +36,26 @@ class StarCitizen(commands.Cog): embed = await star_citizen.get_ship(ship_name=ship) await ctx.send_followup(embed=embed) + async def get_all_commodities(ctx: discord.AutocompleteContext): + """ + Query the API to get a list of all commodities in the game, + Then offer them as auto-complete options for the /trade function + """ + + headers = {"api_key": os.getenv("uexcorp_key")} + + response = requests.get( + "https://portal.uexcorp.space/api/commodities/", headers=headers + ) + + all_commodities = [x["name"] for x in response.json()["data"]] + return all_commodities + @commands.slash_command( guild_ids=None, name="trade", description="Calculates the most profitable route for a given commodity", ) - @option( - name="commodity", - description="The commodity you want to look up", - required=True, - ) @option( name="scu", description="Optinal how much SCU to fill", @@ -56,7 +64,12 @@ class StarCitizen(commands.Cog): max_value=98304, ) async def calculate_trade_profies( - self, ctx: commands.Context, commodity: str, scu: int + self, + ctx: commands.Context, + commodity: discord.Option( + str, autocomplete=discord.utils.basic_autocomplete(get_all_commodities) + ), + scu: int, ): await ctx.defer() embed = await star_citizen.calculate_trade_profies(commodity=commodity, scu=scu) diff --git a/app/requirements.txt b/app/requirements.txt index 5517d056..2cdb1ed5 100755 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -10,5 +10,6 @@ openai owotext pandas requests +requests-cache wolframalpha yfinance \ No newline at end of file diff --git a/app/star_citizen.py b/app/star_citizen.py index f18dd978..ce6fd977 100755 --- a/app/star_citizen.py +++ b/app/star_citizen.py @@ -226,12 +226,6 @@ async def calculate_trade_profies(commodity, scu=None): description="-------", color=discord.Color.blue(), type="rich" ) response = requests.get(url, headers=headers).json()["data"] - if not response: - embed.set_author( - name="❌ Couldnt find that commodity: %s\n\nDid you make a typo?" % commodity - ) - return embed - embed.set_author(name=commodity) embed.set_thumbnail( url="https://media.starcitizen.tools/thumb/b/bb/Shubin_logo_circle.png/120px-Shubin_logo_circle.png"