Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.env
.env
.vscode/launch.json
.DS_Store
107 changes: 86 additions & 21 deletions Bot/assistant_bot.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,122 @@
import asyncio
from datetime import datetime
import discord
from discord.ext import tasks
import os
import requests
import json
import random
from dotenv import load_dotenv

# load .env file from local
load_dotenv()
# instance of the client
Intents = discord.Intents.default()
# Intents.message_content = True
Intents.message_content = True
client = discord.Client(intents=Intents)

sad_words = ["sad", "depressed", "unhappy", "angry", "miserable", "depressing"]
meme_channel_id = int(os.getenv('MEME_CHANNEL_ID'))

# const
sad_words = ["sad", "depressed", "unhappy", "angry", "miserable", "depressing"]
starter_encouragements = [
"Cheer up!",
"Cheer up!",
"You can do it!",
"Hang in there!",
"You are a star!",
"You are a great person!"
]
zen_url = "https://zenquotes.io/api/random"
meme_url = "https://meme-api.herokuapp.com/gimme/1"

# method part


def get_quote():
"""
This function gets a random quote from the API
return value is {'q': ~~~, 'a': ~~~, 'h': ~~~}
"""
url = "https://zenquotes.io/api/random"
response = requests.get(url)
headers = {'user-agent': 'vscode-restclient'}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does this user agent come from?

response = requests.request("GET", zen_url, headers=headers)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might want to do some error checking here, like checking if the status code is between 200 and 299.
I see there is some error checking below, but if an error is going to occur it's likely that no json is going to be send at all, and would fail on line 42 when decoding the json file.

Take a look at this for more details on what I mean. https://stackoverflow.com/questions/16511337/correct-way-to-try-except-using-python-requests-module/16511493#16511493

json_data = json.loads(response.text)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be done with

json_data = response.json()

quote = json_data[0]['q'] + " - " + json_data[0]['a']
return quote
if not json_data[0]:
return "failed fetching data... Anyway, you can do it!!!"
return json_data[0]


def get_meme():
"""
This function gets a random meme from the API
get a jpg or png url
please refer to this below url
https://github.com/D3vd/Meme_Api
"""
response = requests.request("GET", meme_url)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing here

json_data_for_meme = json.loads(response.text)
url = json_data_for_meme['memes'][0]['url']
return url


@tasks.loop(minutes=1.0)
async def show_meme_daily():
"""
This function sends a meme at 8:00 am everyday.
This task is called every 1 minutes and if the time is 8:00, it will send meme.
"""
now = datetime.now().strftime('%H:%M')
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh oh, timezones.

This may work on your machine where time is in our current timezone, but if this is hosted somewhere we can't guarentee that.

We might want to specify the timezone like this

Suggested change
now = datetime.now().strftime('%H:%M')
now = datetime.now(tz=zoneinfo.ZoneInfo("Australia/Brisbane")).strftime('%H:%M')

and import zoneinfo at the top

if (now == '08:00'):
channel = client.get_channel(meme_channel_id)
await channel.send(f"Good morning! This is today's meme")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't need to be an fstring

await channel.send(get_meme())
return


@client.event
async def on_ready(): # when the bot is ready to use
async def on_ready(): # when the bot is ready to use
print(f'We have logged in as {client.user}')
# "Hey there! I'm your personal assistant. I can help you with anything you need. Just ask me!"

@client.event # this tells the library we are listening to a message event
async def on_message(message): # when the bot recieves a message
if message.author == client.user: # if the message is from the bot (ourselves)
return # we don't want to reply to ourselves

if message.content.startswith('!hello'): # if the message starts with !hello
await message.channel.send(f'Hello humans!') # reply with Hello!
@client.event # this tells the library we are listening to a message event
async def on_message(message): # when the bot recieves a message
# if the message is from the bot (ourselves)
if message.author == client.user:
return # we don't want to reply to ourselves

# if the message starts with !hello
if message.content.startswith('!hello'):
await message.channel.send(f'Hello humans!') # reply with Hello!
# await message.author.send('Hello there!') # send a message to the author

if message.content.startswith('!inspire'): # if the message starts with !inspire
author_name = message.author.name.split()[0] # get the author's name

# if the message starts with !inspire
if message.content.startswith('!inspire'):
author_name = message.author.name.split()[0] # get the author's name
quote = get_quote()
# await message.channel.send(quote)
await message.reply(author_name + ", " + quote)


# if the message contains negative words
if any(word in message.content for word in sad_words):
json_data = get_quote()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json_data is not a very helpful variable name here. quote_data is probably better

quote = json_data['q']
author = json_data['a']

await message.channel.send(quote + '- by ' + author)
await message.channel.send(random.choice(starter_encouragements))
# message.channel.send("I'm sorry to hear that. I'm always here to help!")

Token = os.getenv('AUTH_TOKEN')
client.run(Token) # run the bot with the token
# identify that as meme channel
if message.channel.id == meme_channel_id and message.content == "!meme":
# send a meme to meme channel
await client.get_channel(meme_channel_id).send(get_meme())


async def main():
async with client:
show_meme_daily.start()
Token = os.getenv('AUTH_TOKEN')
await client.start(Token) # run the bot with the token

# Takao implemented this line by using asyncio library insted of client.start(token)
# because show_meme_daily method should be called every 1 minute.
asyncio.run(main())
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Code Network Discord Bot version 2.0 member developed.
@Steve-Hun
@Kriswill72
@draykophoenix
@Takao-Mizuno

##Feature Requests
*add your name next to features you are working on!*
Expand All @@ -17,7 +18,7 @@ Code Network Discord Bot version 2.0 member developed.
- When2Meet meeting scheduling assistant - using a service or custom algorithm create a scheduling assistant that can provide best meeting time suggestions based on those who will be required for the meeting
- Custom notification cards (like how MEE6 command cards are) for notification messages. These can be used to schedule meetings and or standups from each team.
- Lofi and music integration
- Witty conversation AI response if anyone of the team members in the general chat sound sad - it should try and provide motivation
- meme of the day post in the meme's channel
- Witty conversation AI response if anyone of the team members in the general chat sound sad - it should try and provide motivation (Takao Mizuno)
- meme of the day post in the meme's channel (Takao Mizuno)
- RSS Feed integration / News integration to keep everyone upto date on tech news
- Implement daily 'code challenges' into Discord bot to improve member engagment