Advertisement
Guest User

Discord Bot | OpenweatherAPI

a guest
Aug 8th, 2021
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.94 KB | None | 0 0
  1. import discord
  2. import os
  3. import requests
  4. import json
  5.  
  6. client = discord.Client()
  7.  
  8. def GetWeather(city):
  9.   url = 'http://api.openweathermap.org/data/2.5/weather?q={}&appid=YourApiKey&units=metric'.format(city)
  10.  
  11.   res = requests.get(url)
  12.   data = res.json()
  13.  
  14.   temp = data ["main"] ["temp"]
  15.   humid = data ["main"] ["humidity"]
  16.   wind = data ["wind"] ["speed"]
  17.   desc = data ["weather"] [0] ["description"]
  18.  
  19.   weather = "\n\
  20.  Description: " + desc +"\nTemp: {}°C".format(temp)+ "\nWind speed: {}m/s".format(wind)+ "\nHumidity: {}%".format(humid)
  21.  
  22.   return weather
  23.  
  24.  
  25.  
  26. @client.event
  27. async def on_ready():
  28.   print('Logged in as {}'.format(client))
  29.  
  30. @client.event
  31. async def on_message(message):
  32.   if message.author == client.user:
  33.     return
  34.  
  35.   if message.content.startswith('$weather'):
  36.     wordlist = message.content.split()
  37.     city = wordlist[1:]
  38.     await message.channel.send(GetWeather("".join(city)))
  39.  
  40. client.run(your_token)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement