-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.py
More file actions
82 lines (62 loc) · 3.11 KB
/
bot.py
File metadata and controls
82 lines (62 loc) · 3.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import pandas as pd
import requests
import asyncio
import discord
import random
import dotenv
import math
import os
import re
from markdownify import markdownify
from bs4 import BeautifulSoup
dotenv.load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')
GUILD = os.getenv('DISCORD_GUILD')
STEAM = os.getenv('STEAM_ROSTER_ID') # senior team roster ID
ITEAM = os.getenv('ITEAM_ROSTER_ID') # intermediate team roster ID
JTEAM = os.getenv('JTEAM_ROSTER_ID') # junior team roster ID
client = discord.Client()
congratulations = ['Fantastic', 'Well done', 'Good job', 'Nice going', 'Superb', 'Congratulations', 'Nice work', 'Nice job', 'Good work', 'Nicely done', 'Keep it going', 'Keep it up']
regular_phrases = ["{salute}, **{name}**! You scored a {score} on the contest {contest} {section}!",
"Looks like **{name}** scored a {score} on the contest {contest} {section}! {salute}!",
"Hey, **{name}**, looks like you scored a {score} on the contest {contest} {section}! {salute}!"]
perfect_phrases = ["{salute}, **{name}**! You earned a *perfect score* on the contest {contest} {section}!",
"Looks like **{name}** earned a *perfect score* on the contest {contest} {section}! {salute}!",
"Hey, **{name}**, looks like you received a *perfect score* on the contest {contest} {section}! {salute}!"]
def fetch(roster: int):
resp = requests.get(f"https://www.scores.acsl.org/roster/{roster}")
html = resp.content
soup = BeautifulSoup(html, features = 'lxml')
elem = soup.find("table", {"class": "display"})
html = str(elem)
data = pd.read_html(html)[0]
return data
async def update(roster):
await client.wait_until_ready()
channel = client.get_channel(id = 944738843029041242)
last = fetch(roster)
while not client.is_closed():
data = fetch(roster)
for contest in range(1, 5):
for section in ['Program', 'Shorts']:
for student in data['Student']:
new = float(data.loc[data['Student'] == student, section[0] + str(contest)].item())
old = float(last.loc[last['Student'] == student, section[0] + str(contest)].item())
if not math.isnan(new) and new != old:
score = int(new)
if score >= 4:
salute = random.choice(congratulations)
if score == 5:
message = random.choice(perfect_phrases).format(salute = salute, name = student, contest = contest, section = section.lower())
elif score == 4:
message = random.choice(regular_phrases).format(salute = salute, name = student, contest = contest, section = section.lower(), score = score)
await channel.send(message)
last = data
await asyncio.sleep(60)
@client.event
async def on_ready():
guild = discord.utils.get(client.guilds, id = GUILD)
client.loop.create_task(update(STEAM))
client.loop.create_task(update(ITEAM))
client.loop.create_task(update(JTEAM))
client.run(TOKEN)