Add request caching + autocomplete to the /trade function

This commit is contained in:
Luke Robles 2023-09-29 08:26:21 -07:00
parent 9444f2621a
commit 45c7c40b96
6 changed files with 29 additions and 18 deletions

3
.gitignore vendored
View File

@ -1,9 +1,8 @@
.DS_Store
dragon_bot_env
*.pyc
__pycache__
*.csv
*env
*conflict*
*syncthing*
.env
*.sqlite

View File

@ -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

View File

@ -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():

View File

@ -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)

View File

@ -10,5 +10,6 @@ openai
owotext
pandas
requests
requests-cache
wolframalpha
yfinance

View File

@ -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"