-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.py
56 lines (49 loc) · 1.82 KB
/
connect.py
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
import os, discord, piazza
import re
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.environ['DISCORD_TOKEN']
GUILD = os.environ['GUILD']
EMAIL = os.environ['EMAIL']
PASSWD = os.environ['PASSWD']
CUR_CLASS = os.environ['CUR_CLASS']
REG_PATTERN = "(?:(?:https|http):\/\/(www\.)*piazza\.com\/class\/(.*?)\?cid=(\d+))"
client = discord.Client()
@client.event
async def on_message(message):
'''
For every new message on the server, on_message() is called
with the messaged passed in.
'''
# Ignore the bots own messages to the server
if message.author == client.user:
return
# If message is a piazza link, call piazza.piazza_parse()
# Then send piazza post to server
# if "piazza.com" in message.content:
urls = re.findall(REG_PATTERN, message.content)
if urls:
for url in urls:
full_url = url[0]
class_hash = url[1]
_ = url[2] # post id
if class_hash == CUR_CLASS:
response = piazza.piazza_parse(full_url, EMAIL, PASSWD, message.author)
await message.channel.send(embed=response)
# Remove ability for messages to be deleted as it might contain other information
# await message.delete()
# ? Todo: Implment with @number
elif message.content == 'raise-exception':
raise discord.DiscordException
@client.event
async def on_ready():
'''
Prints to console when bot is connected to Discord and server.
'''
print(f'{client.user} has connected to Discord!\n')
for guild in client.guilds:
if guild.name == GUILD:
break
print(f'{client.user} has connected to the following guild: {guild.name}(id: {guild.id})')
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name="Piazza's Code"))
client.run(TOKEN)