Another re-write of the monitoring code for starcitizen. This time taking advantage of the cstate API

This commit is contained in:
Luke Robles 2023-10-13 11:35:12 -07:00
parent 8e9348fb24
commit d9a89939e0
3 changed files with 30 additions and 60 deletions

View File

@ -133,20 +133,4 @@ async def fix_tiktok_links(ctx):
return
@bot.event
async def on_command_completion(ctx):
channel = bot.get_channel(826547484632678453)
embed = discord.Embed(colour=discord.Color.green(), title="Command Executed")
embed.add_field(name="Command:", value=f"`{ctx.command}`")
embed.add_field(name="User:", value=f"`{ctx.author}`", inline=False)
embed.add_field(name="Channel:", value=f"{ctx.channel} **( <#{ctx.channel.id}> )**")
embed.add_field(
name="Link to Message:",
value="**(** [%s](%s) **)**" % (ctx.channel, ctx.message.jump_url),
)
embed.add_field(name="Server:", value=f"{ctx.guild} **( <#{ctx.channel.id}> )**")
if ctx.message.author.id != core_utils.my_id:
await channel.send(embed=embed)
bot.run(os.getenv("discord_token"))

View File

@ -8,6 +8,11 @@ import json
import star_citizen
if os.getenv("DRAGON_ENV") == "prod":
channel_id = 1097567909640929340
else:
channel_id = 932476007439552522
class StarCitizen(commands.Cog):
def __init__(self, bot):
@ -221,25 +226,19 @@ class StarCitizen(commands.Cog):
status_url = "https://status.robertsspaceindustries.com"
response = requests.get(status_url).text
soup = BeautifulSoup(response, "html.parser")
rsi_incident_file = "/tmp/rsi_incident"
current_status = soup.find("div", {"class": "summary"})
if current_status.text.strip() != "No issues detected":
# Find the lastest incident
latest_incident = current_status.findNext("h3")
if requests.get(status_url + "/index.json").json()["summaryStatus"] != "ok":
# Look at all the systems in the API and filter out the ones whos "unresolvedIssues" list is not empty,
# Then parse out the "permalink" value
current_incident_url = list(
filter(lambda x: len(x["unresolvedIssues"]), status_json["systems"])
)[0]["unresolvedIssues"][0]["permalink"].replace("'", "")
current_incident_url = (
latest_incident.find_next("div", class_="issue no-underline")
.get("onclick")
.split("=")[-1]
.replace("\\", "")
.replace("'", "")
)
response = requests.get(current_incident_url).text
incident_soup = BeautifulSoup(response, "html.parser")
details = latest_incident.findNext("span", {"class": "issue__content"}).text
details = incident_soup.find("div", class_="article__content").get_text()
if os.path.exists(rsi_incident_file):
file_contents = json.loads(open(rsi_incident_file).read())
@ -247,8 +246,11 @@ class StarCitizen(commands.Cog):
incident_link = file_contents["incident_link"]
if current_incident_url != incident_link:
print("Current incident url did not match wahts in the file")
print(
"Current incident url did not match whats in the file, must be a new incident, send alert"
)
embed = star_citizen.build_alert_embed(
color=discord.Color.red(),
thumbnail="https://media.robertsspaceindustries.com/t0q21kbb3zrpt/source.png",
author="🚨 OH NO THERES AN INCIDENT 🚨",
image="https://media.giphy.com/media/WWIZJyXHXscn8JC1e0/giphy.gif",
@ -261,36 +263,25 @@ class StarCitizen(commands.Cog):
details=details,
)
# # Testing channel
# await star_citizen.send_alert(
# self, channel=932476007439552522, embed=embed
# )
# Actual channel
await star_citizen.send_alert(
self, channel=1097567909640929340, embed=embed
)
await star_citizen.send_alert(self, channel=channel_id, embed=embed)
elif deets != details:
# The details we have in the json file do not match what was recently posted to the website
# Update the json file on disk and then send an update alert to the channel
star_citizen.write_incident_file(
file_path=rsi_incident_file,
url=current_incident_url,
details=details,
)
embed = star_citizen.build_alert_embed(
color=discord.Color.yellow(),
thumbnail="https://media.robertsspaceindustries.com/t0q21kbb3zrpt/source.png",
author="⚠️ THERE HAS BEEN AN UPDATE TO THE ONGOING INCIDENT ⚠️",
image="https://media.giphy.com/media/WWIZJyXHXscn8JC1e0/giphy.gif",
details=deets,
link=incident_link,
)
# # Testing channel
# await star_citizen.send_alert(
# self, channel=932476007439552522, embed=embed
# )
# Actual channel
await star_citizen.send_alert(
self, channel=1097567909640929340, embed=embed
)
await star_citizen.send_alert(self, channel=channel_id, embed=embed)
else:
# Write the incident's body to a json file to read later
star_citizen.write_incident_file(
@ -300,6 +291,7 @@ class StarCitizen(commands.Cog):
)
embed = star_citizen.build_alert_embed(
color=discord.Color.red(),
thumbnail="https://media.robertsspaceindustries.com/t0q21kbb3zrpt/source.png",
author="🚨 OH NO THERES AN INCIDENT 🚨",
image="https://media.giphy.com/media/WWIZJyXHXscn8JC1e0/giphy.gif",
@ -307,15 +299,7 @@ class StarCitizen(commands.Cog):
link=current_incident_url,
)
# # Testing channel
# await star_citizen.send_alert(
# self, channel=932476007439552522, embed=embed
# )
# Actual channel
await star_citizen.send_alert(
self, channel=1097567909640929340, embed=embed
)
await star_citizen.send_alert(self, channel=channel_id, embed=embed)
def setup(bot):

View File

@ -16,8 +16,10 @@ async def send_alert(self, channel, embed):
await self.bot.get_channel(channel).send(embed=embed)
def build_alert_embed(thumbnail=None, author=None, image=None, details=None, link=None):
embed = discord.Embed(description="-------", color=discord.Color.red(), type="rich")
def build_alert_embed(
color, thumbnail=None, author=None, image=None, details=None, link=None
):
embed = discord.Embed(description="-------", color=color, type="rich")
if thumbnail:
embed.set_thumbnail(url=thumbnail)