· 5 years ago · Jun 21, 2020, 05:34 PM
1 @commands.command(aliases=["w"])
2 async def weather(self, ctx, *, varos):
3 if not ctx.message.author.bot:
4 try:
5 api_key = "your api key"
6 base_url = "http://api.openweathermap.org/data/2.5/weather?"
7 city_name = varos
8 complete_url = base_url + "appid=" + api_key + "&q=" + city_name
9 async with aiohttp.ClientSession() as session:
10 async with session.get(complete_url) as r:
11 response = await r.json()
12 x = response
13 if x["cod"] != "404":
14 import pytemperature
15 y = x["main"]
16 z = x["weather"]
17 current_temperature = y["temp"]
18 current_pressure = y["pressure"]
19 current_humidiy = y["humidity"]
20 weather_description = z[0]["description"]
21 celsius = pytemperature.k2c(current_temperature)
22 embed = discord.Embed(title="Weather informations", color=0x00ff00, timestamp=datetime.datetime.utcnow())
23 embed.add_field(name="**Temperature**", value=f"{int(celsius)}°C")
24 embed.add_field(name="**Pressure**", value=f"{current_pressure}hPa")
25 embed.add_field(name="**Humidity**", value=f"{current_humidiy}%")
26 embed.set_author(name="SwissPlus", icon_url=ctx.message.author.avatar_url)
27 embed.set_footer(text="SwissPlus", icon_url="https://swissdev.team")
28 await ctx.send(embed=embed)
29 else:
30 await ctx.send("<tick emoji> Cannot find this city!")
31 except Exception as e:
32 await ctx.send("<also tick emoji> Couldn't retrieve data!")