From eec17930ea7f5bd02c26968e4fca533bc8cb1fe8 Mon Sep 17 00:00:00 2001 From: Yuto Takano Date: Sat, 28 Sep 2024 18:30:19 +0200 Subject: [PATCH] Add a uni service status checker in these trying times --- bot.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/bot.py b/bot.py index 49f3d23..ad03741 100755 --- a/bot.py +++ b/bot.py @@ -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 @@ -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) @@ -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):