dragon-bot/app/cogs/star_citizen.py

151 lines
4.9 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()
# self.poll_for_patch_notes.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")
@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 star_citizen(self, ctx: commands.Context, ship):
await ctx.defer()
embed = await star_citizen.get_ship(ship_name=ship)
await ctx.send_followup(embed=embed)
@tasks.loop(seconds=5)
async def poll_for_patch_notes(self):
# Wait until the bot is ready before we actually start executing code
await self.bot.wait_until_ready()
blog_url = "https://www.spaceloop.it/en/blog-en"
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/112.0"
}
response = requests.get(
blog_url,
headers=headers,
).text
soup = BeautifulSoup(response, "html.parser")
latest_post = soup.find("div", {"class": "posts-wrapper"}).findNext(
"h2", {"class": "blog-entry-title"}
)
print(latest_post)
if "Patch Notes" in latest_post.text:
patch_notes_page = requests.get(
latest_post.findNext("a")["href"],
headers=headers,
).text
soup = BeautifulSoup(patch_notes_page, "html.parser")
patch_number = soup.find("h2", {"class": "wp-block-heading"}).text
print(patch_number)
embed = discord.Embed(
description="-------", color=discord.Color.blue(), type="rich"
)
embed.set_thumbnail(
url="https://media.robertsspaceindustries.com/t0q21kbb3zrpt/source.png"
)
embed.set_author(name=patch_number)
for bullet in soup.find_all("h3"):
if bullet.text == "Patch News":
break
embed.add_field(
name=bullet.text,
value="\n".join([x for x in bullet.findNext("ul").find_all("li")]),
inline=False,
)
await self.bot.get_channel(932476007439552522).send(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):
if (
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))