dragon-bot/app/cogs/star_citizen.py

167 lines
5.5 KiB
Python

from bs4 import BeautifulSoup
from discord import option
from discord.ext import commands, tasks
import discord
import os
import requests
import star_citizen
class StarCitizen(commands.Cog):
def __init__(self, bot):
self.bot: commands.Bot = bot
self.poll_status_page.start()
@commands.slash_command(
guild_ids=None,
name="medpens",
description="Posts an infographic about which medpens to use for which injuries in Star Citizen",
)
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",
)
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)
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="scu",
description="Optinal how much SCU to fill",
required=False,
min_value=1,
max_value=98304,
)
async def calculate_trade_profies(
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)
await ctx.send_followup(embed=embed)
@tasks.loop(seconds=5)
async def poll_status_page(self):
# Wait until the bot is ready before we actually start executing code
await self.bot.wait_until_ready()
status_url = "https://status.robertsspaceindustries.com"
response = requests.get(status_url).text
soup = BeautifulSoup(response, "html.parser")
current_status = soup.find("div", {"class": "global-status"}).findNext("span")
rsi_incident_file = "/tmp/rsi_incident"
if current_status.text != "Operational":
# Find the lastest incident
latest_incident = current_status.findNext("h2")
# Check if we've already notified
if (
os.path.exists(rsi_incident_file)
and open(rsi_incident_file, "r").read()
== latest_incident.findNext("a")["href"]
):
return
f = open(rsi_incident_file, "w")
f.write(latest_incident.findNext("a")["href"])
details = latest_incident.findNext("div", {"class": "markdown"})
embed = discord.Embed(
description="-------", color=discord.Color.red(), type="rich"
)
embed.set_thumbnail(
url="https://media.robertsspaceindustries.com/t0q21kbb3zrpt/source.png"
)
embed.set_author(
name="🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨\n🚨 OH NO THERES AN INCIDENT 🚨\n🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨"
)
embed.set_image(
url="https://media.giphy.com/media/WWIZJyXHXscn8JC1e0/giphy.gif"
)
embed.add_field(name="Details", value=details.text, inline=True)
embed.add_field(
name="LINK",
value=status_url + latest_incident.findNext("a")["href"],
inline=False,
)
await self.bot.get_channel(1097567909640929340).send(embed=embed)
def setup(bot):
bot.add_cog(StarCitizen(bot))