Skip to content

Commit eec1793

Browse files
committed
Add a uni service status checker in these trying times
1 parent e5cb275 commit eec1793

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

bot.py

+69
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#!/usr/bin/env python3
22
import sys
33
import traceback
4+
import random
5+
import re
6+
import requests
47
from typing import List, Optional
58
import xml.etree.ElementTree as ElementTree
69

@@ -11,6 +14,7 @@
1114

1215
bot_intents = discord.Intents.default()
1316
bot_intents.members = True
17+
bot_intents.message_content = True
1418

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

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

233+
@bot.event
234+
async def on_message(message: discord.Message):
235+
if message.author == bot.user:
236+
return
237+
238+
await message.channel.trigger_typing()
239+
240+
try:
241+
# Check if the message is a status check via a very simple:tm: regex
242+
# thx regex101.com
243+
if re.match(r"^(is +)?(my *ed|learn|[\/&\+])* +down( |\?|$)", message.content, re.IGNORECASE | re.MULTILINE):
244+
# check if myed is down
245+
try:
246+
response = requests.get("https://www.myed.ed.ac.uk/myed-progressive/", timeout=5)
247+
# check if the status code is 200
248+
myed_up = response.status_code == 200
249+
myed_down_reason = None if myed_up else "Status: " + str(response.status_code)
250+
except requests.exceptions.RequestException as e:
251+
# some connection error e.g. name not resolved
252+
myed_up = False
253+
myed_down_reason = str(e)
254+
255+
# check if learn is down
256+
try:
257+
response = requests.get("https://www.learn.ed.ac.uk/", timeout=5)
258+
# check if the status code is 200
259+
learn_up = response.status_code == 200
260+
learn_down_reason = None if learn_up else "Status: " + str(response.status_code)
261+
except requests.exceptions.RequestException as e:
262+
# some connection error e.g. name not resolved
263+
learn_up = False
264+
learn_down_reason = str(e)
265+
266+
random_response = [
267+
"I can answer that!",
268+
"Uni Service Status",
269+
"Maybe I can help!",
270+
"May I interest you in some service status?",
271+
]
272+
273+
# Response with the status of the services in a nice embed
274+
await message.channel.send(
275+
embeds=[
276+
discord.Embed(
277+
title=random.choice(random_response),
278+
description=f"Here's the current status of the University's services.\nFor accurate info, see https://alerts.is.ed.ac.uk/",
279+
color=discord.Color.green() if myed_up and learn_up else discord.Color.red(),
280+
fields=[
281+
discord.EmbedField(
282+
name="MyEd",
283+
value=("✅ Up" if myed_up else f"❌ Down ({myed_down_reason})") + "\n" + "https://www.myed.ed.ac.uk/myed-progressive/",
284+
inline=False,
285+
),
286+
discord.EmbedField(
287+
name="Learn",
288+
value=("✅ Up" if learn_up else f"❌ Down ({learn_down_reason})") + "\n" + "https://www.learn.ed.ac.uk/",
289+
inline=False,
290+
),
291+
]
292+
)
293+
],
294+
)
295+
except Exception:
296+
# Send any form of error message to the channel
297+
await message.channel.send("```" + traceback.format_exc() + "```")
229298

230299
@bot.event
231300
async def on_command_error(ctx, error):

0 commit comments

Comments
 (0)