bitbot-3.11-fork/modules/location.py
jesopo 670f682c62 Remove '"human"' key from location settings export - we dont use it and it's
using elements of 'location' that were removed
2019-05-07 10:59:24 +01:00

20 lines
803 B
Python

from src import ModuleManager, utils
URL_OPENCAGE = "https://api.opencagedata.com/geocode/v1/json"
class Module(ModuleManager.BaseModule):
def on_load(self):
self.exports.add("set", {"setting": "location",
"help": "Set your location", "validate": self._get_location})
def _get_location(self, s):
page = utils.http.request(URL_OPENCAGE, get_params={
"q": s, "key": self.bot.config["opencagedata-api-key"], "limit": "1"
}, json=True)
if page and page.data["results"]:
result = page.data["results"][0]
timezone = result["annotations"]["timezone"]["name"]
lat = result["geometry"]["lat"]
lon = result["geometry"]["lng"]
return {"timezone": timezone, "lat": lat, "lon": lon}