-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
55 lines (44 loc) · 1.59 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# import discord
from discord.ext import commands
import os
from keep_alive import keep_alive
# allows HTTP requests from an API
import requests
# makes it easier to work with data that is returned
import json
keep_alive()
bot_token = os.environ['DISCORD_BOT_SECRET']
weather_api = os.environ['OPENWEATHER_API']
# provide a prefix for users to call
bot = commands.Bot(command_prefix='!')
def get_weather(zipcode: int):
# make a 'GET' request that takes an arg of the URL we want to make a request to
response = requests.get(f'https://api.openweathermap.org/data/2.5/weather?zip={zipcode},us&appid={weather_api}&units=imperial')
json_data = json.loads(response.text)
weather_description = json_data['weather'][0]['description']
weather_temp = json_data['main']['temp']
weather_report = f'I see {weather_description}. It is currently {weather_temp}ºF!'
return(weather_report)
@bot.event
async def on_ready():
print("I'm in")
print(bot.user)
@bot.event
async def on_message(message):
# prevents bot from responding to self in an endless loop
# if message.author == bot.user:
# return
await bot.process_commands(message)
# help command? help='Responds with the current weather given the user\'s zipcode'
@bot.command()
# function is named whatever the command is
async def weather(ctx, zipcode: int):
weather_report = get_weather(zipcode)
await ctx.send(weather_report)
# alternative way to name a command
@bot.command(name='testing')
async def call_test(ctx):
await ctx.send('1, 2, 3!')
bot.run(bot_token)
# check status code to see if request was successful
# print(response.json())