-
Notifications
You must be signed in to change notification settings - Fork 5
Feature/meme #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Feature/meme #9
Changes from all commits
f1c950d
6408fd0
7218586
564ceaa
3a7fd81
4c55cde
dea6592
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| .env | ||
| .env | ||
| .vscode/launch.json | ||
| .DS_Store |
| 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'} | ||||||
| response = requests.request("GET", zen_url, headers=headers) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 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) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
and import |
||||||
| if (now == '08:00'): | ||||||
| channel = client.get_channel(meme_channel_id) | ||||||
| await channel.send(f"Good morning! This is today's meme") | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||
| 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()) | ||||||
There was a problem hiding this comment.
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?