diff --git a/.gitignore b/.gitignore index 2eea525..9184ffe 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.env \ No newline at end of file +.env +.vscode/launch.json +.DS_Store diff --git a/Bot/assistant_bot.py b/Bot/assistant_bot.py index fc03943..9d1df6a 100644 --- a/Bot/assistant_bot.py +++ b/Bot/assistant_bot.py @@ -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'} + response = requests.request("GET", zen_url, headers=headers) json_data = json.loads(response.text) - 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) + 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') + if (now == '08:00'): + channel = client.get_channel(meme_channel_id) + await channel.send(f"Good morning! This is today's meme") + 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() + 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 \ No newline at end of file + # 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()) diff --git a/README.md b/README.md index 18c6547..aecdb39 100644 --- a/README.md +++ b/README.md @@ -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!* @@ -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