|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 | import sys
|
3 | 3 | import traceback
|
| 4 | +import random |
| 5 | +import re |
| 6 | +import requests |
4 | 7 | from typing import List, Optional
|
5 | 8 | import xml.etree.ElementTree as ElementTree
|
6 | 9 |
|
|
11 | 14 |
|
12 | 15 | bot_intents = discord.Intents.default()
|
13 | 16 | bot_intents.members = True
|
| 17 | +bot_intents.message_content = True |
14 | 18 |
|
15 | 19 | bot = commands.Bot(command_prefix=commands.when_mentioned_or("§"), intents=bot_intents)
|
16 | 20 |
|
@@ -226,6 +230,71 @@ async def report(ctx, *, member):
|
226 | 230 | f"{found.mention} has been reported by {ctx.author.mention} — CC <@&315339680641974273>"
|
227 | 231 | )
|
228 | 232 |
|
| 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() + "```") |
229 | 298 |
|
230 | 299 | @bot.event
|
231 | 300 | async def on_command_error(ctx, error):
|
|
0 commit comments