Skip to content

Commit

Permalink
Add a uni service status checker in these trying times
Browse files Browse the repository at this point in the history
  • Loading branch information
yutotakano committed Sep 28, 2024
1 parent e5cb275 commit eec1793
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env python3
import sys
import traceback
import random
import re
import requests
from typing import List, Optional
import xml.etree.ElementTree as ElementTree

Expand All @@ -11,6 +14,7 @@

bot_intents = discord.Intents.default()
bot_intents.members = True
bot_intents.message_content = True

bot = commands.Bot(command_prefix=commands.when_mentioned_or("§"), intents=bot_intents)

Expand Down Expand Up @@ -226,6 +230,71 @@ async def report(ctx, *, member):
f"{found.mention} has been reported by {ctx.author.mention} — CC <@&315339680641974273>"
)

@bot.event
async def on_message(message: discord.Message):
if message.author == bot.user:
return

await message.channel.trigger_typing()

try:
# Check if the message is a status check via a very simple:tm: regex
# thx regex101.com
if re.match(r"^(is +)?(my *ed|learn|[\/&\+])* +down( |\?|$)", message.content, re.IGNORECASE | re.MULTILINE):
# check if myed is down
try:
response = requests.get("https://www.myed.ed.ac.uk/myed-progressive/", timeout=5)
# check if the status code is 200
myed_up = response.status_code == 200
myed_down_reason = None if myed_up else "Status: " + str(response.status_code)
except requests.exceptions.RequestException as e:
# some connection error e.g. name not resolved
myed_up = False
myed_down_reason = str(e)

# check if learn is down
try:
response = requests.get("https://www.learn.ed.ac.uk/", timeout=5)
# check if the status code is 200
learn_up = response.status_code == 200
learn_down_reason = None if learn_up else "Status: " + str(response.status_code)
except requests.exceptions.RequestException as e:
# some connection error e.g. name not resolved
learn_up = False
learn_down_reason = str(e)

random_response = [
"I can answer that!",
"Uni Service Status",
"Maybe I can help!",
"May I interest you in some service status?",
]

# Response with the status of the services in a nice embed
await message.channel.send(
embeds=[
discord.Embed(
title=random.choice(random_response),
description=f"Here's the current status of the University's services.\nFor accurate info, see https://alerts.is.ed.ac.uk/",
color=discord.Color.green() if myed_up and learn_up else discord.Color.red(),
fields=[
discord.EmbedField(
name="MyEd",
value=("✅ Up" if myed_up else f"❌ Down ({myed_down_reason})") + "\n" + "https://www.myed.ed.ac.uk/myed-progressive/",
inline=False,
),
discord.EmbedField(
name="Learn",
value=("✅ Up" if learn_up else f"❌ Down ({learn_down_reason})") + "\n" + "https://www.learn.ed.ac.uk/",
inline=False,
),
]
)
],
)
except Exception:
# Send any form of error message to the channel
await message.channel.send("```" + traceback.format_exc() + "```")

@bot.event
async def on_command_error(ctx, error):
Expand Down

0 comments on commit eec1793

Please sign in to comment.