Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
55 views

Code

This document defines functions and event handlers to build a Discord bot that provides weather forecasts and jokes. The bot retrieves weather conditions for various UK cities, returns a formatted weather report table for a given location, and selects a random joke from a text file. It handles messages containing weather or joke requests and responds with the appropriate information.

Uploaded by

api-437334001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Code

This document defines functions and event handlers to build a Discord bot that provides weather forecasts and jokes. The bot retrieves weather conditions for various UK cities, returns a formatted weather report table for a given location, and selects a random joke from a text file. It handles messages containing weather or joke requests and responds with the appropriate information.

Uploaded by

api-437334001
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import discord

import asyncio
import datetime
import random

from weather import Weather, Unit


from discord.ext import commands
from prettytable import PrettyTable

TOKEN = "NTAzODQ0OTc4NDM1NDI0MjU2.DsH8wQ.qCmyacFwrPmqXw5wWElOdCiWOIk"

client = discord.Client()
CDT = datetime.datetime.now()

#location = geocoder.ip("me")
weather = Weather(unit=Unit.CELSIUS)
#weatherLookp = weather.lookup_by_latlng(location.lat, location.lng)
#weatherLookp = weather.lookup_by_location("")
#condation = weatherLookp.condition

cities = "Bath Birmingham Bradford Brighton Bristol Cambridge Canterbury


Carlisle Chester Chichester Coventry Derby Durham Ely Exeter Gloucester
Hereford Kingston Hull Lancaster Leeds Leicester Lichfield Lincoln
Liverpool London Manchester Newcastle Norwich Nottingham Oxford
Peterborough Plymouth Portsmouth Preston Ripon Salford Salisbury Sheffield
Southampton StAlbans Stoke-on-Trent Sunderland Truro Wakefield Westminster
Winchester Wolverhampton Worcester York"
location_city = cities.split(" ")
#location_city.lower()
conditions = {"Showers":"shower.png", "Partly
Cloudy":"parlycloudy.png","Sunny":"sunny.png", "Cloudy":"cloudy.png",
"Breezy":"breezy.png"}
@client.event
async def on_message(message):

content = message.content
channel = message.channel
author = message.author

if message.author == client.user:
return

if message.content.startswith('!hello'):
msg = 'Hello, how can I help...'.format(message)
await client.send_message(message.channel, msg)
showerImg = "hello.jpg"
await client.send_file(channel, showerImg)

# if ("sunny" or "cloudy" or "rain" or "weather" or "cold") in


content.lower():
# reply = "I dont know where you live, To know the weather tell me
where you live?".format(message)
#await client.send_message(message.channel, reply)

if content.lower() == "weather":
reply = "I dont know where you live, To know the weather tell me
where you live?".format(message)
await client.send_message(message.channel, reply)
# check the city name in userMessage and send the weather report
for value in location_city:
if ("forecast" or "weather forecast") in content.lower():
forcaste = weatherTable(value)
await client.send_message(message.channel,forcaste )
break
if ("weahter" and value.lower()) in content.lower():
image = ""
weather = weatherLookup(value)
temp = something(value)
msg = "its "+ weather + " in " + value
for k,v in conditions.items():
if k in msg:
image = v
break
await client.send_message(message.channel, msg)

await client.send_file(channel, image)


break

if ("joke" or "tell me a joke") in content.lower():


_jokes = Jokes("jokes.txt")
msg = random.choice(_jokes)
await client.send_message(message.channel, msg, tts=True)

def weatherTable(content):
"""
input: content as string
return: weather inforamtion tempreture, condition as a table
"""
weatherReport = PrettyTable()
weatherReport.field_names = ["conditions", "Date", "High", "Low"]
weather = Weather(unit=Unit.CELSIUS)
location = weather.lookup_by_location(content)
forecasts = location.forecast
for forecast in forecasts:
weatherReport.add_row([forecast.text,forecast.date,forecast.high +
"°",forecast.low+"°"])
weatherReport.add_row([" ", " ", " ", " "])
weatherReport.add_row([" ", " ", " ", " "])
return weatherReport

def Jokes(file_name):
"""
input: file name eg, text.txt
retun list with jokes
"""
file = open(file_name,'r')
jokes = []
for value in file:
value = value.strip('\n')
#dic[value] = dic.get(value)
jokes.append(value)
file.close()
return jokes

def weatherLookup(area):
"""
Input: Name of city
Output: weather condition depending on the location/city.
"""
weather = Weather(unit=Unit.CELSIUS)
weatherLookp = weather.lookup_by_location(area)
condition = weatherLookp.condition
return condition.text

@client.event
async def on_ready():
print(client.user.name)
print(client.user.id)
print("Is Ready & Logged On!")
await client.send_message(client.get_channel(id= '503843931771895810'),
'I am alive!')

client.run(TOKEN)

You might also like