From f1c950dd5656744fa3905adfafafa9b57d8553c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B0=B4=E9=87=8E=E5=96=AC=E5=A4=AB?= Date: Wed, 14 Sep 2022 18:31:34 +1000 Subject: [PATCH 1/6] my first commit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 18c6547..5d7fa14 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!* From 6408fd06bb4a6edc92517771954d15ba2e742784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B0=B4=E9=87=8E=E5=96=AC=E5=A4=AB?= Date: Wed, 14 Sep 2022 20:35:44 +1000 Subject: [PATCH 2/6] add takao to witty conversation --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5d7fa14..e637b22 100644 --- a/README.md +++ b/README.md @@ -18,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 +- Witty conversation AI response if anyone of the team members in the general chat sound sad - it should try and provide motivation (TakaoMizuno) - meme of the day post in the meme's channel - RSS Feed integration / News integration to keep everyone upto date on tech news - Implement daily 'code challenges' into Discord bot to improve member engagment From 564ceaafe31b5df8a591968e856d85d5d0028a03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B0=B4=E9=87=8E=E5=96=AC=E5=A4=AB?= Date: Fri, 16 Sep 2022 16:03:20 +1000 Subject: [PATCH 3/6] create motivator bot --- .gitignore | 4 ++- Bot/assistant_bot.py | 58 ++++++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 22 deletions(-) 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..ee35a7a 100644 --- a/Bot/assistant_bot.py +++ b/Bot/assistant_bot.py @@ -3,55 +3,71 @@ import requests import json import random +from dotenv import load_dotenv # instance of the client Intents = discord.Intents.default() -# Intents.message_content = True +Intents.message_content = True client = discord.Client(intents=Intents) +# 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" + 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] + @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 +load_dotenv() +Token = os.getenv('AUTH_TOKEN') +client.run(Token) # run the bot with the token From 3a7fd812878fbe1fec696179ea615c20dac8c940 Mon Sep 17 00:00:00 2001 From: Takao Mizuno Date: Wed, 21 Sep 2022 14:27:04 +1000 Subject: [PATCH 4/6] create sending meme function --- Bot/assistant_bot.py | 55 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) diff --git a/Bot/assistant_bot.py b/Bot/assistant_bot.py index ee35a7a..84769e6 100644 --- a/Bot/assistant_bot.py +++ b/Bot/assistant_bot.py @@ -1,15 +1,22 @@ +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 client = discord.Client(intents=Intents) +meme_channel_id = int(os.getenv('MEME_CHANNEL_ID')) + # const sad_words = ["sad", "depressed", "unhappy", "angry", "miserable", "depressing"] starter_encouragements = [ @@ -20,6 +27,9 @@ "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(): @@ -35,6 +45,33 @@ def get_quote(): 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 print(f'We have logged in as {client.user}') @@ -68,6 +105,18 @@ async def on_message(message): # when the bot recieves a message await message.channel.send(quote + '- by ' + author) await message.channel.send(random.choice(starter_encouragements)) -load_dotenv() -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: + # 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()) From 4c55cde626a0d0d514432aa70036df1227120ee2 Mon Sep 17 00:00:00 2001 From: Takao Mizuno Date: Wed, 21 Sep 2022 14:29:14 +1000 Subject: [PATCH 5/6] add my name to readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e637b22..aecdb39 100644 --- a/README.md +++ b/README.md @@ -18,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 (TakaoMizuno) -- 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 From dea6592ccfe9cfcd0c852e4f5333fca535358057 Mon Sep 17 00:00:00 2001 From: Takao Mizuno Date: Wed, 21 Sep 2022 16:39:31 +1000 Subject: [PATCH 6/6] fix bug: if users put any texts in the meme channel, this bot send a meme. I fixed this problem.If users enter "!meme", this bot can send a meme. --- Bot/assistant_bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Bot/assistant_bot.py b/Bot/assistant_bot.py index 84769e6..9d1df6a 100644 --- a/Bot/assistant_bot.py +++ b/Bot/assistant_bot.py @@ -106,7 +106,7 @@ async def on_message(message): # when the bot recieves a message await message.channel.send(random.choice(starter_encouragements)) # identify that as meme channel - if message.channel.id == meme_channel_id: + 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())