-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeather.py
More file actions
93 lines (86 loc) · 4.29 KB
/
Weather.py
File metadata and controls
93 lines (86 loc) · 4.29 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import requests
import discord
from discord.ext import commands
from log import log
import utils
KEY = "INSERT OPENWEATHER API KEY"
forecastLength = [0, 4]
class Weather(commands.Cog):
def __init__(self, client):
log(1, "Weather cog loaded")
# if not os.path.exists("./resources"):
# os.mkdir("./resources")
self.client = client
@staticmethod
async def updateMessage(ctx, message):
nMessage = await ctx.fetch_message(message.id)
return nMessage
@staticmethod
async def changeReactions(day, message, ctx):
nMessage = await Weather.updateMessage(ctx, message)
reactions = list(map(lambda reaction: str(reaction), nMessage.reactions))
if not 0 < len(reactions) <= 2:
await message.clear_reactions()
reactions = ["X", "X"]
if day == forecastLength[0]:
if "➡" != reactions[0]:
await message.clear_reactions()
await message.add_reaction("➡")
elif day == forecastLength[1]:
if "⬅" != reactions[0]:
await message.clear_reactions()
await message.add_reaction("⬅")
await message.clear_reaction("➡")
else:
if "⬅" != reactions[0]:
await message.clear_reactions()
await message.add_reaction("⬅")
await message.add_reaction("➡")
elif len(reactions) < 2:
await message.add_reaction("➡")
@staticmethod
async def createEmbed(response, city, country, day):
embed = discord.Embed(title=f"Weather - {city}, {country} - Day {(day+1)}", color=discord.Colour.blue())
actualTemp = response['main']['temp'] - 273.15 #converting Kelvin to Celsius
embed.add_field(name="Temperature", value=f"{round(actualTemp)} C")
embed.add_field(name="Conditions", value=response["weather"][0]["description"])
embed.add_field(name="Humidity", value=f"{response['main']['humidity']}%", inline=True)
embed.add_field(name="Clouds", value=f"{response['clouds']['all']}%", inline=True)
embed.add_field(name="Wind", value=f"{response['wind']['speed']}M/s at {response['wind']['deg']} degrees", inline=True)
embed.set_thumbnail(url=f"http://openweathermap.org/img/wn/{str(response['weather'][0]['icon'])}@2x.png")
return embed
@commands.command(aliases=["forecast"])
async def weather(self, ctx, *args):
def weatherCheck(reaction, user, day, message):
if day == forecastLength[0]:
condition = str(reaction) == "➡"
elif day == forecastLength[1]:
condition = str(reaction) == "⬅"
else:
condition = str(reaction) == "➡" or str(reaction) == "⬅"
return condition and user == ctx.author and reaction.message.id == message.id
location = " ".join(args)
# response = requests.get(f"http://api.openweathermap.org/data/2.5/forecast?q={location}&appid={KEY}")
# response = response.json()
response = await utils.requestJSON(f"http://api.openweathermap.org/data/2.5/forecast?q={location}&appid={KEY}", 'get')
if response['cod'] == str(200):
day = 0
embed = await self.createEmbed(response["list"][day], response["city"]["name"], response["city"]["country"], day)
message = await ctx.send(embed=embed)
await self.changeReactions(day, message, ctx)
while(True):
try:
reaction, user = await self.client.wait_for('reaction_add', timeout=360, check=lambda reaction, user: weatherCheck(reaction, user, day, message))
except:
await message.delete()
break
else:
if str(reaction) == "➡":
day += 1
elif str(reaction) == "⬅":
day -= 1
await message.edit(embed=await self.createEmbed(response["list"][day*8], response["city"]["name"], response["city"]["country"], day))
await self.changeReactions(day, message, ctx)
await message.remove_reaction(reaction, user)
else:
await ctx.send("The city your specified was not found.")