Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import discord #imports discord library which i'll be needing functions from
import json #imports pythons json library which will help me to use commands to openjson files
import urllib.request #used for opening links for api's
from discord.ext import commands #used for commands that will be needed to communicate with the bot on python
##############Section in which the youtube video helped me to connect the bot to discord.###############
###############YouTube video link https://www.youtube.com/watch?v=2YO96GFBSLw ####################
Client = discord.Client() #connects discord to the python program
token ="NTExMzM2MjE3OTUwODE0MjA5.Dspdvg.X-1PT7UBuUsbF6ihpzh1rPbOYCs" #serves as a login for the bot
client = commands.Bot(command_prefix = "!") #whatever prefix chosen is used to run the functions made
########################################################################################################
@client.event #on the event that the client does something run this function
async def on_ready(): #when the client is ready run this command
print("Type !test Commands into the discord client to get started.")
@client.command() #when the command prefix is pressed and function name is entered it runs this code section
async def test(msg):
if msg == "Commands": #if Commands is typed after the command test then welcome messaages are run
welcomeMsg1 = "Hello, welcome to my Chatbot. This bot can help you to find the weather for any city!"
welcomeMsg2 = "Please remember that all city names should be in lowercase!"
welcomeMsg3 = "The commands for the bot are as follows;"
welcomeMsg4 = "Type !Temp (City name in lowercase) for the temperature in the city."
welcomeMsg5 = "Type !cityMin (City name in lowercase) for the lowest temperature for the day."
welcomeMsg6 = "Type !cityMax (City name in lowercase) for the highest temperature for the day."
welcomeMsg7 = "Type !forecast (City name in lowercase) for the worded forecast for the day in the city."
await client.say(welcomeMsg1)
await client.say(welcomeMsg2)
await client.say(welcomeMsg3)
await client.say(welcomeMsg4)
await client.say(welcomeMsg5)
await client.say(welcomeMsg6)
await client.say(welcomeMsg7)
else:
await client.say(msg) #if user input is not Commands re run the function
@client.command()
async def forecast(msg): #when the commands prefix is followed by forecast and the city this function is run
cityName = msg #the user input is saved as the city name
stateP1 = locationIdFind(cityName) #the city name is sent to another function to find the location id and is returned
if stateP1 == "Please enter a valid city name!":
await client.say("Please enter a valid city name!")
else:
stateP2 = weatherState(stateP1) #location id is sent into another function and returned is the forecast
completedStatement = "The weather forecast in " + msg + " shows " + stateP2 + "'s ." #forms sentence
await client.say(completedStatement) #send full statement to be output on discord
@client.command()
async def cityMax(msg):
cityName = msg
stateP1 = locationIdFind(cityName)
if stateP1 == "Please enter a valid city name!":
await client.say("Please enter a valid city name!")
else:
stateP2 = maxTemp(stateP1)
completedStatement = "The highest possible temperature in " + msg + " will be " + str(stateP2) + "°c"
await client.say(completedStatement)
@client.command()
async def cityMin(msg):
cityName = msg
stateP1 = locationIdFind(cityName)
if stateP1 == "Please enter a valid city name!":
await client.say("Please enter a valid city name!")
else:
stateP2 = minTemp(stateP1)
completedStatement = "The lowest possible temperature in " + msg + " will be " + str(stateP2) + "°c"
await client.say(completedStatement)
@client.command()
async def Temp(msg):
cityName = msg
stateP1 = locationIdFind(cityName)
if stateP1 == "Please enter a valid city name!":
await client.say("Please enter a valid city name!")
else:
stateP2 = theTemp(stateP1)
completedStatement = "The temperature in " + msg + " is " + str(stateP2) + "°c"
await client.say(completedStatement)
##########Example of how i learnt and adapted the code from the video i watched##############
##################YouTube video link https://www.youtube.com/watch?v=19-LOqdI61k&t=130s##################
def locationIdFind(input): #this function gets the location id of all the cities the user asks information for and returns it
try:
url1 = "https://www.metaweather.com/api/location/search/?query="
city = input #the city which location id is needed
search_final = url1 + city #concatanates url with user input
search_data = urllib.request.urlopen(search_final).read() #uses methods from imported library to open link
dictJson = str(search_data, 'utf-8') #saves the information in the link as a readable json file
data1 = json.loads(dictJson) #opens json file dictionary
var2 = (data1[0]["woeid"]) #traverses disctionary looking for the key queried
return var2 #returns value at the key queried
except:
errorMsg = "Please enter a valid city name!"
return errorMsg
##############################################################################################
def weatherState(locatId): #uses the location id to search through json dictionary to find the weather forecast
if msg == "Please enter a valid city name!":
newMsg = "Please enter a valid city name!"
return newMsg
else:
url2 = "https://www.metaweather.com/api/location/" #the url need to find info on a location
strVar2 = str(locatId) #gets the location id and changes it from int to string
fullUrl2 = url2 + strVar2 + "/" #concatanates to form url
search_data2 = urllib.request.urlopen(fullUrl2).read() #opens link
dictJson = str(search_data2, 'utf-8') #saves json file in a readable format
data2 = json.loads(dictJson) #opens dictionary
var3 = (data2["consolidated_weather"][0]) #traverses dictionary using keys
var4 = (var3["weather_state_name"]) #uses a specific key to find the value for the query needed
return var4 #returns saved value
def maxTemp(locatId): #uses location id to find the maximum temperature for that location
if msg == "Please enter a valid city name!":
newMsg = "Please enter a valid city name!"
return newMsg
else:
url2 = "https://www.metaweather.com/api/location/"
strVar2 = str(locatId)
fullUrl2 = url2 + strVar2 + "/"
search_data2 = urllib.request.urlopen(fullUrl2).read()
dictJson = str(search_data2, 'utf-8')
data2 = json.loads(dictJson)
var3 = (data2["consolidated_weather"][0])
var5 = (var3["max_temp"])
roundvar5 = round(var5, 2)
return roundvar5
def minTemp(locatId): #uses the location id to find the minimum temperature for that location
if msg == "Please enter a valid city name!":
newMsg = "Please enter a valid city name!"
return newMsg
else:
url2 = "https://www.metaweather.com/api/location/"
strVar2 = str(locatId)
fullUrl2 = url2 + strVar2 + "/"
search_data2 = urllib.request.urlopen(fullUrl2).read()
dictJson = str(search_data2, 'utf-8')
data2 = json.loads(dictJson)
var3 = (data2["consolidated_weather"][0])
var6 = (var3["min_temp"])
roundvar6 = round(var6, 2)
return roundvar6
def theTemp(locatId): #uses the location id to find the temperature at a location
if msg == "Please enter a valid city name!":
newMsg = "Please enter a valid city name!"
return newMsg
else:
url2 = "https://www.metaweather.com/api/location/"
strVar2 = str(locatId)
fullUrl2 = url2 + strVar2 + "/"
search_data2 = urllib.request.urlopen(fullUrl2).read()
dictJson = str(search_data2, 'utf-8')
data2 = json.loads(dictJson)
var3 = (data2["consolidated_weather"][0])
var7 = (var3["the_temp"])
roundvar7 = round(var7, 2)
return roundvar7
client.run(token) #uses the login/token to run the client