From 72692b029e49249153b6acade255fc62bb2a7e9b Mon Sep 17 00:00:00 2001 From: Daffy Chu Date: Sat, 1 May 2021 01:09:51 -0400 Subject: [PATCH 1/2] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 03a1298..130f0f2 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ To run PiazzaBot locally, do the following: ``` python3 -m venv ./env source ./env/bin/activate -pip3 install -r requirements +pip3 install -r requirements.txt ``` 2. Export your environment variables as such From 1ef51a60188f94993820eb92a2954f1b6bb85991 Mon Sep 17 00:00:00 2001 From: daffychuy Date: Tue, 15 Jun 2021 13:55:50 +0800 Subject: [PATCH 2/2] Update discord embes --- connect.py | 86 +++++++++++++++++++++++++++--------------------- piazza.py | 21 +++++++----- requirements.txt | 2 +- 3 files changed, 63 insertions(+), 46 deletions(-) diff --git a/connect.py b/connect.py index 6d41c6a..f748e4c 100644 --- a/connect.py +++ b/connect.py @@ -1,43 +1,55 @@ -***REMOVED*** -***REMOVED*** +import os, discord, piazza +import re +from dotenv import load_dotenv -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** +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() -***REMOVED*** +@client.event +async def on_message(message): + ''' + For every new message on the server, on_message() is called + with the messaged passed in. + ''' -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** + # Ignore the bots own messages to the server + if message.author == client.user: + return -***REMOVED*** -***REMOVED*** -***REMOVED*** + # If message is a piazza link, call piazza.piazza_parse() + # Then send piazza post to server - # If message is a piazza link, call test.piazza_parse() -***REMOVED*** - if "piazza.com" in message.content: - response = piazza.piazza_parse(message.content, EMAIL, PASSWD) - await message.channel.send(embed=response) - await message.delete() -***REMOVED*** -***REMOVED*** + # if "piazza.com" in message.content: + if urls := re.findall(REG_PATTERN, message.content): + 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() -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** -***REMOVED*** \ No newline at end of file + # ? 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) \ No newline at end of file diff --git a/piazza.py b/piazza.py index fb7d94d..7c61625 100644 --- a/piazza.py +++ b/piazza.py @@ -5,11 +5,11 @@ ZERO_WHITE_SPACE = "\u200b" -def piazza_parse(pi_url, email, passwd): -***REMOVED*** +def piazza_parse(pi_url, email, passwd, author): + ''' Called by connect.py to get post from Piazza. It will format the post json into a more readable message for server. -***REMOVED*** + ''' # Create Piazza object, login, and get the users classes data = {} @@ -66,13 +66,18 @@ def piazza_parse(pi_url, email, passwd): elif answer['type'] == 'i_answer': data['answer']['instructor'] = BeautifulSoup(answer['history'][0]['content'], features='lxml').text - return embed_creator(data, pi_url) + return embed_creator(data, pi_url, author) -def embed_creator(data, pi_url): +def embed_creator(data, pi_url, author): embed=discord.Embed(title=data['question'], url=pi_url, description=data['question_text'], color=0x00fffb) if data['answer']['student']: - embed.add_field(name="Student(s) Answer", value=data['answer']['student'], inline=False) + embed.add_field(name="Student(s) Answer:", value=data['answer']['student'], inline=False) if data['answer']['instructor']: - embed.add_field(name="Instructor(s) Answer", value=data['answer']['instructor'], inline=False) - embed.set_footer(text=f"Read Full Post: {pi_url}") + embed.add_field(name="Instructor(s) Answer:", value=data['answer']['instructor'], inline=False) + + pre_footer = f"""From: {author} + Read comments and full post: [here]({pi_url}) + """ + embed.add_field(name="More info:", value=pre_footer, inline=False) + embed.set_footer(text=f"Piazza bot by daffychuy, [source](https://github.com/daffychuy/piazza_bot)") return embed \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 4aeeb9e..0497aa5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,7 @@ beautifulsoup4==4.9.3 bs4==0.0.1 certifi==2020.12.5 chardet==4.0.0 -discord.py==1.6.0 +discord.py~=1.7.3 idna==3.1 lxml==4.6.2 multidict==5.1.0