From 72346ff2c4d1dea981eed3e7da5efd3aafadb8b9 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Tue, 10 Oct 2023 11:22:44 -0700 Subject: [PATCH] Re-doing the star citizen incident alert logic, it will now alert us to changes in the incident status --- app/cogs/star_citizen.py | 106 +++++++++++++++++++++++++++------------ app/star_citizen.py | 10 ++++ 2 files changed, 85 insertions(+), 31 deletions(-) diff --git a/app/cogs/star_citizen.py b/app/cogs/star_citizen.py index e7c50ec0..4151cefd 100644 --- a/app/cogs/star_citizen.py +++ b/app/cogs/star_citizen.py @@ -4,6 +4,7 @@ from discord.ext import commands, tasks import discord import os import requests +import json import star_citizen @@ -50,8 +51,6 @@ class StarCitizen(commands.Cog): ) embed.set_author(name="Ranking of drugs by sell price") - # embed.add_field(name="Details", value=details.text, inline=True) - for k, v in sorted_dict.items(): embed.add_field( name=k, @@ -250,41 +249,86 @@ class StarCitizen(commands.Cog): if current_status.text != "Operational": # Find the lastest incident latest_incident = current_status.findNext("h2") + current_incident_url = status_url + latest_incident.findNext("a")["href"] + details = latest_incident.findNext("div", {"class": "markdown"}).text - # 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 + if os.path.exists(rsi_incident_file): + file_contents = json.loads(open(rsi_incident_file).read()) + deets = file_contents["details"] + incident_link = file_contents["incident_link"] - f = open(rsi_incident_file, "w") - f.write(latest_incident.findNext("a")["href"]) + if current_incident_url == incident_link: + if deets != details: + embed = discord.Embed( + description="-------", + color=discord.Color.yellow(), + type="rich", + ) - 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="⚠️ THERE HAS BEEN AN UPDATE TO THE ONGOING INCIDENT ⚠️\n" + ) + # embed.set_image( + # url="https://media.giphy.com/media/WWIZJyXHXscn8JC1e0/giphy.gif" + # ) + embed.add_field(name="Details", value=deets, inline=True) - 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=incident_link, + inline=False, + ) - embed.add_field( - name="LINK", - value=status_url + latest_incident.findNext("a")["href"], - inline=False, - ) + await self.bot.get_channel(1097567909640929340).send( + embed=embed + ) - await self.bot.get_channel(1097567909640929340).send(embed=embed) + # Testing channel + # await self.bot.get_channel(932476007439552522).send( + # embed=embed + # ) + + star_citizen.write_incident_file( + file_path=rsi_incident_file, + url=current_incident_url, + details=details, + ) + else: + # Write the incident's body to a json file to read later + star_citizen.write_incident_file( + file_path=rsi_incident_file, + url=current_incident_url, + details=details, + ) + + 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="🚨 OH NO THERES AN INCIDENT 🚨") + embed.set_image( + url="https://media.giphy.com/media/WWIZJyXHXscn8JC1e0/giphy.gif" + ) + embed.add_field(name="Details", value=details, inline=True) + + embed.add_field( + name="LINK", + value=current_incident_url, + inline=False, + ) + + await self.bot.get_channel(1097567909640929340).send(embed=embed) + + # Testing channel + # await self.bot.get_channel(932476007439552522).send( + # embed=embed + # ) def setup(bot): diff --git a/app/star_citizen.py b/app/star_citizen.py index 6ef2e078..4066e531 100755 --- a/app/star_citizen.py +++ b/app/star_citizen.py @@ -2,6 +2,7 @@ from bs4 import BeautifulSoup import requests import discord import os +import json # prints for debug import pprint @@ -11,6 +12,15 @@ pp = pprint.PrettyPrinter(indent=4) wiki_url = "https://starcitizen.tools/" +def write_incident_file(file_path, url, details): + info_dict = { + "incident_link": url, + "details": details, + } + with open(file_path, "w") as out_file: + out_file.write(json.dumps(info_dict)) + + async def get_ship(ship_name): try: wiki_ship_name = "_".join(elem for elem in ship_name.split())